2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

Struts2攔截器和監(jiān)聽(tīng)器

 Ethan的博客 2011-05-23
第一種方法: 
直接implements實(shí)現(xiàn)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 
這種方法能對(duì)Action里面的方法級(jí)進(jìn)行控制,是否執(zhí)行這個(gè)方法 

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就用這個(gè)default的 

extends="struts-default"表示這個(gè)XML文件是extends 另一個(gè)xml的,名字叫struts-default 

在這個(gè)XML中,配置了一個(gè)常用的interceptor可以去Core包中找到這個(gè)XML 

interceptor配置很靈活的。如果要寫(xiě)自己的interceptor就要把<interceptor-ref name="defaultStack"></interceptor-ref>默認(rèn)的給加上,當(dāng)然也可以自己配置。 

interceptor的參數(shù) 
Xml代碼 
  1. <interceptor-stack name="myInterceptorStack">  
  2.                 <interceptor-ref name="myInterceptor3"></interceptor-ref>                 
  3.                 <interceptor-ref name="defaultStack"></interceptor-ref>               
  4.             </interceptor-stack>  

這個(gè)是因?yàn)镸ethodFilterInterceptor 里有這兩個(gè)成員變量,如果你自己的interceptor要帶參數(shù)的話就要相應(yīng)的Action里面寫(xiě)到成員變量,并加上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();  


監(jiān)聽(tīng)器 
首先要實(shí)現(xiàn)com.opensymphony.xwork2.interceptor.PreResultListener類 
并重寫(xiě)里面的方法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. }  


然后再在攔截器里面調(diào)用 
Java代碼 
  1. invocation.addPreResultListener(new MyListener());  

監(jiān)聽(tīng)器是在這個(gè)攔截器完成別的攔截器之后調(diào)用的 


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 ();   

主要是這兩個(gè)類com.opensymphony.xwork2.ActionContext和org.apache.struts2.ServletActionContext都對(duì)request等進(jìn)行了大量的封裝,直接調(diào)用方法就可以獲和 

更好一點(diǎn)的IOC方式 
action類實(shí)現(xiàn)ServletRequestAware接口,并新建一個(gè)HttpServletRequest request 
Java代碼 
  1. public class UserLoginAction extends ActionSupport implements ServletRequestAware{  
  2.    public void setServletRequest(HttpServletRequest request) {  
  3.      this.request=request;  
  4.   }  
  5.  然后可以生成的request得到對(duì)象,如request.getRemoteAddr()  
  6. action類實(shí)現(xiàn)SessionAware接口,并創(chuàng)建一個(gè)MAP對(duì)象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等對(duì)象需要implments的接口都在 
org.apache.struts2.interceptor下面 
如Apllication的是ApplicationAware 
如HttpSession的是SessionAware(struts2的Session都被封裝成Map了) 
如HttpServletRequest的是ServletRequestAware 
如HttpServletResponse的是ServletResponseAware

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多