乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

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

      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的參數(shù) 
      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要帶參數(shù)的話就要相應(yīng)的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();  


      監(jiān)聽器 
      首先要實現(xiàn)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. }  


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

      監(jiān)聽器是在這個攔截器完成別的攔截器之后調(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 ();   

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

      更好一點的IOC方式 
      action類實現(xiàn)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類實現(xiàn)SessionAware接口,并創(chuàng)建一個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

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多