1 客户端发送请求;
2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin)
3 接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action。FilterDispatcher的功能如下:
4 如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy
5 ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类
6 ActionProxy创建一个ActionInvocation的实例。
7 ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。
8 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可 能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2 框架中继承的标签。在这个过程中需要涉及到ActionMapper
自定义拦截器类
public class MyInterceptor implements Interceptor{ //方法 public String intercept(ActionInvocation invocation) throws Exception { System.out.println("对象"+invocation); Object action = invocation.getAction(); System.out.println(action); String value; //判定session里面key值对应的value是否存在 Map<String, Object> session = ActionContext.getContext().getSession(); Object name = session.get("uname"); String actionName = invocation.getProxy().getActionName(); System.out.println(actionName+"====================actionName"); if (actionName.equals("login")) { System.out.println("11122121212121212121212121212"); //user/login.action value= invocation.invoke(); }else if(name!=null) { //已经登录了 value= invocation.invoke(); String method = invocation.getProxy().getMethod(); System.out.println("方法:"+method); }else{ System.out.println("11111111111111111111111111111111"); value="login"; } System.out.println("逻辑视图名"+value); return value; } public void destroy() { } public void init() { System.out.println("拦截器已经成功初始化....."); } }action执行的action类
public class LoginAction implements Action{ private User user; public String execute() throws Exception { System.out.println("execute()"); Map<String, Object> session = ActionContext.getContext().getSession(); if (user!=null){ if (user.getUsername().equals("1")&&user.getPassword().equals("1")) { //省略一个步骤,记录session session.put("uname",user.getUsername()); return SUCCESS; }else { return LOGIN; } }else { if (session!=null&&session.get("uname")!=null) { return SUCCESS; }else { System.out.println("Action中的自定义代码"); return LOGIN; } } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }struts.xml
<struts> <!--修改该文件,tomcat不用重启--> <constant name="struts.devMode" value="true"></constant> <package name="strutss" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myInter" class="cn.demo.actionday07.MyInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myInter"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <!--<default-action-ref name="first"></default-action-ref>--> <global-results> <result name="login">/WEB-INF/myinter/loginguohua.jsp</result> </global-results> </package> <!--<include file="struts_day03.xml"/> <include file="struts_day04.xml"/> <include file="struts_day05.xml"/> <include file="struts_day06.xml"/>--> <include file="struts_day07.xml"/> </struts>strutsday07.xml(包级别继承struts.xml)
<struts> <!--修改该文件,tomcat不用重启--> <constant name="struts.devMode" value="true"></constant> <package name="day07" namespace="/" extends="strutss"> <action name="login" class="cn.demo.actionday07.LoginAction"> <result name="success">/WEB-INF/myinter/success.jsp</result> <result name="login">/WEB-INF/myinter/loginguohua.jsp</result> </action> <action name="success" class="cn.demo.actionday07.Success"> <result name="success">/WEB-INF/myinter/success.jsp</result> </action> </package> </struts>