struts2拦截器的使用

来源:互联网 发布:企业办公网络设计方案 编辑:程序博客网 时间:2024/05/09 18:32

首先配置struts.xml文件

<package name="intercepts" extends="struts-default">

     <interceptors>

            <!--这个是自定义的拦截器-->

            <interceptor name="login" class="com.test.intercept.Login"></interceptor>

            <!--定义一个拦截器栈,包含默认的拦截器和自定义拦截器-->

            <interceptor-stack name="myStack">

                  <interceptor-ref name="login"></interceptor-ref>

                  <interceptor-ref name="defaultStack"></interceptor-ref>

            </interceptor-stack>

     <interceptors>

     <default-interceptor-ref name="myStack" />

</package>

 

<package name="test" extends="intercepts" namespace="/test">

        <!---->

       <action  name="login" class="com.test.Action.TestAction">

              <result name="success">/aaa.jsp</result>

       </action>

</package>

 

public class Login extends AbstractInterceptor

{

      public String interceptor (ActionInvocation invocate) throws Exception

      {

            System.out.println("这个是拦截器");

            ActionContext ctx = ActionContext.getContext() ;

            Map session = ctx.getSession();

            String url = actionInvocation.getInvocationContext().getName();

            if(url.equals(login.action)){

                  invocate.invoke();

            }

            if(null==session.get("name")){

                   return "login" ;

            }else{

                   return invocate.invoke();

            }           

       }

}

然后在地址栏里输入:http://localhost:8080/ceload/login.action

原创粉丝点击