Struts2拦截器和监听器

关键字: struts2 拦截器
第一种方法: 
直接implements实现com.opensymphony.xwork2.interceptor.Interceptor 
Java代码 
  1. public class MyInterceptor implements Interceptor {  
  2.   
  3.     public void destroy() {  
  4.         System.out.println("destroy()");  
  5.     }  
  6.   
  7.     public void init() {  
  8.         System.out.println("init()");  
  9.     }  
  10.   
  11.     public String intercept(ActionInvocation invocation) throws Exception {  
  12.         System.out.println("Interceptor");  
  13.         String result = invocation.invoke();  
  14.         return result;  
  15.     }  
  16.   
  17. }  


第二种方法 
直接extends com.opensymphony.xwork2.interceptor.AbstractInterceptor 
这种方法是少了destroy,init方法 
Java代码 
  1. public class MyInterceptor2 extends AbstractInterceptor {  
  2.   
  3.     @Override  
  4.     public String intercept(ActionInvocation invocation) throws Exception {  
  5.         System.out.println("MyInterceptor2222");  
  6.         String result = invocation.invoke();  
  7.         return result;  
  8.     }  
  9.   
  10. }  


第三种方法 
直接extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor 
这种方法能对Action里面的方法级进行控制,是否执行这个方法 

Java代码 
  1. public class MyInterceptor3 extends MethodFilterInterceptor {  
  2.   
  3.     @Override  
  4.     protected String doIntercept(ActionInvocation invocation) throws Exception {  
  5.         System.out.println("use MethodFilterInterceptor");  
  6.         String result = invocation.invoke();  
  7.         return result;  
  8.     }  
  9.   
  10. }  


然就是配置 
Xml代码 
  1. <interceptors>  
  2.             <interceptor name="myInterceptor" class="com.langhua.interceptor.MyInterceptor">                
  3.             </interceptor>  
  4.               
  5.             <interceptor name="myInterceptor2" class="com.langhua.interceptor.MyInterceptor2">  
  6.             </interceptor>  
  7.               
  8.             <interceptor name="myInterceptor3" class="com.langhua.interceptor.MyInterceptor3">  
  9.                 <param name="excludeMethods">execute</param>  
  10.                 <param name="includeMethods">addmessage</param>  
  11.             </interceptor>  
  12.               
  13.             <interceptor-stack name="myInterceptorStack">  
  14.                 <interceptor-ref name="myInterceptor3"></interceptor-ref>                 
  15.                 <interceptor-ref name="defaultStack"></interceptor-ref>               
  16.             </interceptor-stack>  
  17.         </interceptors>  

里面分interceptor和interceptor-stack 
stack也能引用stack 

default-interceptor-ref表示Action如果不指定interceptor就用这个default的 

extends="struts-default"表示这个XML文件是extends 另一个xml的,名字叫struts-default 

在这个XML中,配置了一个常用的interceptor可以去Core包中找到这个XML 

interceptor配置很灵活的。如果要写自己的interceptor就要把<interceptor-ref name="defaultStack"></interceptor-ref>默认的给加上,当然也可以自己配置。 

interceptor的参数 
Xml代码 
  1. <interceptor-stack name="myInterceptorStack">  
  2.                 <interceptor-ref name="myInterceptor3"></interceptor-ref>                 
  3.                 <interceptor-ref name="defaultStack"></interceptor-ref>               
  4.             </interceptor-stack>  

这个是因为MethodFilterInterceptor 里有这两个成员变量,如果你自己的interceptor要带参数的话就要相应的Action里面写到成员变量,并加上get,set方法 
Java代码 
  1. public abstract class MethodFilterInterceptor extends AbstractInterceptor {  
  2.     protected transient Logger log = LoggerFactory.getLogger(getClass());  
  3.       
  4.     protected Set<String> excludeMethods = Collections.emptySet();  
  5.     protected Set<String> includeMethods = Collections.emptySet();  


监听器 
首先要实现com.opensymphony.xwork2.interceptor.PreResultListener类 
并重写里面的方法beforeResult 
Java代码 
  1. public class MyListener implements PreResultListener {  
  2.   
  3.     public void beforeResult(ActionInvocation invocation, String resultCode) {  
  4.         System.out.println(resultCode);  
  5.     }  
  6.   
  7. }  


然后再在拦截器里面调用 
Java代码 
  1. invocation.addPreResultListener(new MyListener());  

监听器是在这个拦截器完成别的拦截器之后调用的 


struts2 Action获得HttpSession,HttpServletRequest,HttpSevletResponse的方法 
非IOC方式 
这种方式主要是利用了com.opensymphony.xwork2.ActionContext类以及org.apache.struts2.ServletActionContext类 
Java代码 
  1. ActionContext ctx = ActionContext.getContext();          
  2.          
  3.   HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);          
  4.          
  5.   HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);          
  6.            
  7.   //ServletActionContext.APPLICATION;          
  8.   //ServletActionContext.SESSION;          
  9.   //ServletActionContext.PAGE_CONTEXT;     
  10. //或者  
  11. HttpServletRequest request = ServletActionContext.getRequest ();   

主要是这两个类com.opensymphony.xwork2.ActionContext和org.apache.struts2.ServletActionContext都对request等进行了大量的封装,直接调用方法就可以获和 

更好一点的IOC方式 
action类实现ServletRequestAware接口,并新建一个HttpServletRequest request 
Java代码 
  1. public class UserLoginAction extends ActionSupport implements ServletRequestAware{  
  2.    public void setServletRequest(HttpServletRequest request) {  
  3.      this.request=request;  
  4.   }  
  5.  然后可以生成的request得到对象,如request.getRemoteAddr()  
  6. action类实现SessionAware接口,并创建一个MAP对象session  
  7. public class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{  
  8.    public void setServletRequest(HttpServletRequest request) {  
  9.      this.request=request;  
  10.   }  
  11. public void setSession(Map session) {  
  12.   this.session=session;    



这些获得HttpServletRequest等对象需要implments的接口都在 
org.apache.struts2.interceptor下面 
如Apllication的是ApplicationAware 
如HttpSession的是SessionAware(struts2的Session都被封装成Map了) 
如HttpServletRequest的是ServletRequestAware 
如HttpServletResponse的是ServletResponseAware
    posted @ 2009-06-23 18:35  刘阳  阅读(850)  评论(0编辑  收藏  举报