struts攔截器

1.在struts xml配置文件中添加:

<package name="forum_main" extends="struts-default">
  <interceptors>
   <!-- 定義一個包含權限控制的攔截器棧 -->
   <interceptor name="authority" class="whtyweb.common.AuthorizationInterceptor" />
 
   <!-- 定義一個包含權限控制的攔截器棧 -->
   <interceptor-stack name="mydefault">
    <interceptor-ref name="defaultStack" />
    <interceptor-ref name="authority" />
   </interceptor-stack>
  </interceptors>
 
  <!-- 定義默認攔截器 -->
  <default-interceptor-ref name="mydefault" />
 
  <!-- 定義全局處理結果 -->
  <global-results>
   <result name="login">/page/currentwork/login.jsp</result>
  </global-results>
  
  <action name="logon">
   <result>/page/currentwork/login.jsp</result>
  </action>
  <action name="sms" class="whtyweb.actions.SmsAction">
            <result name="view">/sms/basic.jsp</result>
            <result name="list">/sms/list.jsp</result>     
            <result name="view2">/sms/basic2.jsp</result>
            <result name="noaudit">/sms/noaudit.jsp</result>          
        </action>  
  
  <action name="smsManage" class="smsManage">
   <result name="smssubmit">/sms/smssubmit.jsp</result>
   <result name="smsaudit">/sms/smsaudit.jsp</result>
  </action>
  
    </package>

 

 

 

2.把要攔截的action添加到攔截器的  <package>中。

 

 

3.AuthorizationInterceptor 攔截器處理類。

package whtyweb.common;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* 登錄權限攔截
*
*/
public class AuthorizationInterceptor extends AbstractInterceptor {

 private static final long serialVersionUID = 2575538469018873724L;

 @SuppressWarnings("unchecked")
 public String intercept(ActionInvocation invocation) throws Exception {
  Map session = invocation.getInvocationContext().getSession();
  String phoneNo = (String) session.get("phone_no");
  if (null != phoneNo && !phoneNo.equals("")) {
   // 合法用戶
   return invocation.invoke();
  } else {
   System.out.println("攔截器:用戶未登錄---");
   return Action.LOGIN;//全局處理結果
  }
 }
}
 

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