配置struts2攔截器

              配置struts2攔截器

 

1.       編寫攔截器的類

如:com.wfhms.system.filter.LoginInterceptor

   //繼承AbstractInterceptor

publicclass LoginInterceptorextends AbstractInterceptor {

         //重寫intercept方法

         @Override

public String intercept(ActionInvocation invocation)throws Exception {

//驗證session中是否有登陸的user

Map<String, Object> session = invocation.getInvocationContext().getSession();

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

            //不存在返回的路徑

            return"login";

         }

            return invocation.invoke();

         }

 

}

             

 

2.struts.xml添加如下

<packagename="default"extends="json-default">

      <interceptors>

      <!—引入編寫的類,name和背景紅色對應 -->

         <interceptorname="loginInterceptor"class="com.wfhms.system.filter.LoginInterceptor">

         </interceptor>

         <interceptor-stackname="defaultStack">

            <interceptor-refname="exception"/>

            <interceptor-refname="alias"/>

            <interceptor-refname="prepare"/>

            <interceptor-refname="i18n"/>

            <interceptor-refname="chain"/>

            <interceptor-refname="fileUpload"/>

            <interceptor-refname="params"/>

            <interceptor-refname="conversionError"/>

            <interceptor-refname="loginInterceptor"/>

         </interceptor-stack>

      </interceptors>

      <!—-默認跳轉的action -->

      <default-action-refname="login"/>

      <global-results>

<resultname="login"type="redirectAction">login</result>

      </global-results>

</package>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章