Struts2自定义拦截器Interceptor以及拦截器登录实例

1.在Struts2自定义拦截器有三种方式:

  -->实现Interceptor接口

public class QLInterceptorAction implements  Interceptor{
    private static final long serialVersionUID = 1L;
    public void destroy() {
    }
    public void init() {}
    public String intercept(ActionInvocation arg0) throws Exception {
        return null;
    }
}

  -->继承AbstractInterceptor类

    public class QLInterceptorAction extends AbstractInterceptor{
    public String intercept(ActionInvocation arg0) throws Exception {
        return null;
    }}

  -->继承MethodFilterInterceptor类

  public class QLInterceptorAction extends MethodFilterInterceptor {}

---------------------------------------------------------------------------------------------------------------------------------

登录实例:

      获取username等                                                                                                        (判断是否有session)                                   

  1.login.jsp--------------->2.Action类(将用户登录的userName设置在Session中)----------------->3.实现拦截器------------->4.配置struts.xml(配置拦截器,看那些action需要拦截,那些不需要拦截)

执行流程:action请求--->看是执行自定义拦截器--->假如执行,到拦截器中判断.---->如果通过,继续执行,否则,返回一个结果,例如:session中username==null;则返回登录页面

                      --->假如不执行,到Action类,执行具体的业务请求.

具体代码如下:

1.Action类:

public String execute() throws Exception {
        System.out.println("LoginAction.execute()");
        if(username!=null){
            //将username设置到Session中
            ActionContext.getContext().getSession().put("username", username);
        }
        return SUCCESS;
    }

2.拦截器类的具体实现:

public class QLInterceptorAction extends MethodFilterInterceptor {
    private static final long serialVersionUID = 1L;
    String name123;

    //获取配置里设置的参数,启动服务器就会加载
    //这是的方法是struts.xml配置里的param里name属性的setter方法
    public void setExcluedeMethodNames(String names) {
        //此处的names为userinfo,不需要拦截
        /*String[] split = names.split(",");
        System.out.println(Arrays.toString(split));
        cludeActionNames = Arrays.asList(names.split(","));
        System.out.println("------"+cludeActionNames);*/
        this.name123=names;
    }

    protected String doIntercept(ActionInvocation ac) throws Exception {
        //通过代理获取到ActionName
        String actionName = ac.getProxy().getActionName();
        //获取到session里的username
        String username = (String) ac.getInvocationContext().getSession().get("username");
        //如果actionName=userinfo,那么继续执行
        if(name123.equals(actionName)){
            return ac.invoke();
        }
        //如果username==null,那么返回到登录页面
        if (username == null || "".equals(username)) {
            return "notlogin";
        }
        return ac.invoke();
    }
}

3.struts.xml配置:

<struts>
    <include file="struts-default.xml"></include>
    
    <package name="interceptorencode" extends="struts-default">
        
         <!-- 设置拦截器 -->
         <interceptors>
             <interceptor name="userinfos" class="com.struts.interceptoraction.QLInterceptorAction">
                <!-- 通过配置的形式向拦截器中传递参数 -->
                <param name="excluedeMethodNames">userinfo</param> 
             
             </interceptor>
             <interceptor-stack name="myStack">
                 <interceptor-ref name="defaultStack"></interceptor-ref>
                 <interceptor-ref name="userinfos"></interceptor-ref>
             </interceptor-stack>
         </interceptors>
             <!-- 默认执行拦截器 -->
         <default-interceptor-ref name="myStack"></default-interceptor-ref>
             <!-- 全局视图结果 -->
         <global-results>
             <result name="notlogin">/login.jsp</result>
         </global-results>
        <action name="userinfo" class="com.struts.interceptoraction.LoginAction" >
            <result>/JSP/NewFile.jsp</result>
        </action>
        
        
    </package>
</struts>     
   

 

posted on 2017-03-11 21:25  超人不会飞丿  阅读(152)  评论(0编辑  收藏  举报

导航