目录
2, 在web.xml中配置核心控制器StrutsPrepareAndExecuteFilter
废话不多,步骤如下:
一、导入struts2 jar包中的系统运行最小包
导入将ssh包中目录下struts/app/blank.war解压获得运行struts最小包。web-inf下lib内容导入eclipse中相同目录下。
2, 在web.xml中配置核心控制器StrutsPrepareAndExecuteFilter
任何MVC框架需要与Web应用整合时都需要借助web.xml配置文件,由于StrutsPrepareAndExecuteFilter本质上是一个过滤器,在web.xml中用< filter>以及< filter-mapping>进行配置。而Web应用加载了StrutsPrepareAndExecuteFilter之后就有了Struts2的基本功能。
此处可以将E:\jar包\ssh包\struts-2.3.24\src\apps\blank\src\main\webapp\WEB-INF目录下的web.xml中filer以及filter-mapping标签向工程中xml文件添加。
配置核心控制器StrutsPrepareAndExecuteFilter就是用其实现类过滤所有的请求。
web.xml文件编写如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 前端主控制器-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>7_23Struts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3,编写jsp视图文件
这里不多赘述,打出我要用到的视图login.jsp。
注意:action=" 表单参数提交到的地址.action" 输入框中name和type写清楚。这里的.action称为链接后缀,在后面struts架构详情中我会再详细说明。
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="person.action" method="post">
<table border="1" width="200px">
<tr>
<td>姓名:</td>
<td><input type="text" name="pname"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
show.jsp(用于测试利用框架实现数据的传输):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello:${pname};
</body>
</html>
4,编写Action文件。
这里简单说明一下这个Action的具体作用及优势:
完全为POJO,提高代码重用率。
无需与Servlet API耦合,便于测试和应用。
业务处理方法execute()将String作为返回值可以映射到任何视图上。
这个Action的作用类似于Bean的作用,这个类和一个继承来的Action格式相同。方法execute必须有,并且需要两个字符串返回值,sussess和Fail。
5 在src下创建struts.xml配置文件
找到strut.xml中有个链接,这个链接用于配置xml UserSpecified Entries.
http://struts.apache.org/dtds/struts-2.3.dtd
注意:若使用MyEclispe开发,则struts.xml在第一行会报错,主要是因为MyEclipse没有找到对应的dtd文件,但这完全不会影响运行。强迫症可以把struts-2.3.31\src\core\src\main\resources目录下的dtd文件导入到MyElipse中在ssh中搜索struts-2.3.dtd,并将文件放入WebContent。
在strut.xml文件中有异常,需要配置选项,不能点出action标签。
在地址中选择到dtd文件之后,key type选择URL,Key中填写地址链接,保存。
在下面添加action标签,内容为form表单中action名字保持相同,后面class用法如下图,为Action的类地址。
<action name="person" class="com.demo.PersonAction.PersonAction"><!--包的类名称 -->
<result name="success">show.jsp</result><!-- 结果-->
<result name="fail" type="redirect">login.jsp</result><!-- 重定向 -->
</action>
MyEclipse环境中,在src下创建的struts.xml在部署时会自动发布到WEB-INF/classes目录下。
当Struts2生成ActionProxy代理时,需要访问Struts2的配置文件,有struts.xml(配置Action相关信息)与struts.properties(配置Struts2全局属性)两种。
如下的struts.xml,用< constant>元素设置Struts2的全局属性。
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 常量 -->
<constant name="struts.devMode" value="true" />
在< package>中定义了一个名为register的Action,并指定了实现类以及< result>元素用来指定execute()方法返回值与视图资源之间的映射关系。 package包中的内容下action标签根据不同的结果集进行不同页面的跳转,这在我们不同实现的jsp可实现多个跳转。不同的package下都有不同的action url。
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="person" class="com.demo.PersonAction.PersonAction"><!--包的类名称 -->
<result name="success">show.jsp</result><!-- 结果-->
<result name="fail" type="redirect">login.jsp</result><!-- 重定向 -->
</action>
</package>
而struts.properties则以key=value的形式存储全局属性。
result 成功后跳转show.jsp。
Demo运行流程分析:
-
用户在输入数据后,发送的请求核心控制器过滤器StrutsPrepareAndExecuteFilter过滤。
-
StrutsPrepareAndExecuteFilter在调用ActionMapper,根据表单中地址来确定名为register的Action类处理该请求。
-
然后Struts2框架读取配置文件struts.xml信息生成ActionProxy
-
ActionProxy根据package中action元素中的name和class属性确定Action实现类为RegisterAction,并调用表单中的数据用setter()方法赋值给RegisterAction对象
-
ActionProxy根据execute()返回值以及action元素中的result元素来确定返回哪个视图资源给用户。