struts自定义拦截器及其执行流程


1 客户端发送请求;
2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin)
3 接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action。FilterDispatcher的功能如下:

        (1)执行Actions
        (2)清除ActionContext
        (3)维护静态内容

        (4)清除request生命周期内的XWorkinterceptors

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值