Struts1(MVC)
WebWork2(MVC)--xwork(内核)-->Struts2
- springmvc
- struts2
/hello.action-->Filter控制器-->HelloAction-->Result-->/hello.jsp
-
创建工程、引入jar包和xml配置文件
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.14.1</version> </dependency> </dependencies>
在src/main/resources添加struts.xml配置文件
-
编写流程中主要组件
package cn.xdl.action;
public class HelloAction {
//默认方法名为execute,无参 public String execute(){ System.out.println("进入HelloAction处理"); return "success";//与<result>配置对应 }
}
-
按流程配置组件
-
配置Filter控制器
<filter> <filter-name>strutsmvc</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> <!-- 默认找src/struts.xml配置文件 --> </filter>
<filter-mapping> <filter-name>strutsmvc</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
-
配置HelloAction和Result
<package name="demo1" extends="struts-default">
<action name="hello" class="cn.xdl.action.HelloAction"> <result name="success" type="dispatcher"> /WEB-INF/hello.jsp </result> </action>
</package>
-
/list.action-->Filter控制器-->ListAction-->DeptDao-->Result-->/WEB-INF/list.jsp
默认只有.action或没有扩展名请求能进入action,如果需要修改,按以下方法
-
在web.xml中将Filter配置改为/*
<filter> <filter-name>strutsmvc</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> <!-- 默认请求类型为.action或没有扩展名 --> <!-- 默认找src/struts.xml配置文件 --> </filter>
<filter-mapping> <filter-name>strutsmvc</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
在struts.xml中定义
<constant name="struts.action.extension" value="do"> </constant>
/day01/hello.do 请求名为hello,namespace为/day01,需要通过< package>元素的namespace属性指定。
<package name="demo1" namespace="/day01" extends="struts-default">
<!--所有action请求都需要加上/day01 --> <action name="hello" class=""></action
</package>
/user/tologin.do-->Filter控制器-->ActionSupport-->Result-->/WEB-INF/login.jsp
-
编写login.jsp
-
追加action配置
<!-- class属性默认值为ActionSupport --> <action name="tologin"> <!-- name默认值为success、type默认值为dispatcher --> <result> /WEB-INF/login.jsp </result> </action>
/user/login.do-->Filter控制器-->LoginAction-->Result-->成功ok.jsp/失败login.jsp
-
编写LoginAction
利用同名属性接收请求参数
public class LoginAction {
private String username;//对应<input type="text" name="username"> private String password; public String execute(){ //检查用户名和密码 if("scott".equalsIgnoreCase(username) &&"123456".equals(password)){ return "success"; } return "login"; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
}
-
配置LoginAction和Result
<action name="login" class="cn.xdl.action.LoginAction"> <result name="success" type="dispatcher">/WEB-INF/ok.jsp</result> <result name="login" type="dispatcher">/WEB-INF/login.jsp</result> </action>
Struts2底层对request、session、application进行了封装,对应Map类型为RequestMap、SessionMap、ApplicationMap。
-
通用方法ActionContext
-
获取Map类型的
ActionContext.getSession()
ActionContext.getApplication()
ActionContext.get("request")
-
获取Servlet类型的
ServletActionContext.getRequest()
ServletActionContext.getServletContext()
ServletActionContext.getResponse()
-
-
Action专用方法
Action类实现Aware接口,底层注入对象.
-
注入Map类型
RequestAware
SessionAware
ApplicationAware
-
注入Servlet类型
ServletRequestAware
ServletResponseAware
ServletContextAware
-