Struts2自定义拦截器

struts.xml  (拦截器必须放进拦截器站(interceptor-stack)中方便调用)

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4     <package name="base" extends="struts-default">
 5     <interceptors>
 6         <interceptor name="permission" class="neusoft.interceptor.WebInterceptor"/>
 7         <interceptor-stack name="permissionStack">
 8             <interceptor-ref name="defaultStack"/>
 9             <interceptor-ref name="permission"/>
10         </interceptor-stack>
11     </interceptors>
12         <global-results>
13             <result name="fail">login.jsp</result>
14         </global-results>    
15                 
16         <action name="Index" class="neusoft.action.LoginAction.Users" method="index">
17             <interceptor-ref name="permissionStack"/>
18             <result name="success">index.jsp</result>
19         </action>
20         
21     </package>
22 </struts>    


 

 

WebInterceptor.java (实现Interceptor借口,“invocation.invoke()”执行Action类方法,并返回一个值)

 1 package neusoft.interceptor;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 import com.opensymphony.xwork2.ActionInvocation;
 5 import com.opensymphony.xwork2.interceptor.Interceptor;
 6 
 7 public class WebInterceptor implements Interceptor {
 8 
 9     public void destroy() {
10         // TODO Auto-generated method stub
11         
12     }
13 
14     public void init() {
15         // TODO Auto-generated method stub
16         
17     }
18 
19     public String intercept(ActionInvocation invocation) throws Exception {
20         String admin=(String) ActionContext.getContext().getSession().get("user");
21         System.out.println(admin+"...........");
22         if(admin!=null){
23             return invocation.invoke();
24         }    
25         return "fail";
26     }
27 
28 }

 

 

 

posted @ 2012-11-06 16:21  hlb  阅读(266)  评论(0编辑  收藏  举报