Struts2-Convention 拦截器配置

来源:互联网 发布:燕雀焉知鸿鹄之志 编辑:程序博客网 时间:2024/06/08 20:03


Java代码  收藏代码
  1.  package com.longzhun.interceptor;    
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;  
  4. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  5.   
  6. /**   
  7.  * @Title: MyInterceptor.java  
  8.  * @Package com.xxxxxxx.interceptor  
  9.  * @Description: TODO(添加描述)  
  10.  * @author longzhun 
  11.  * @date 2011-9-1 下午11:02:02  
  12.  * @version V1.0  
  13.  */  
  14. public class MyInterceptor extends AbstractInterceptor {  
  15.   
  16.     @Override  
  17.     public String intercept(ActionInvocation invocation) throws Exception {  
  18.           
  19.         System.out.println("default interceptor start");  
  20.         String s  = invocation.invoke();  
  21.         System.out.println("default interceptor end");  
  22.         return s;  
  23.   
  24.   
  25.     }  
  26.   
  27. }  

 

 

Java代码  收藏代码
  1.  package com.longzhun.interceptor;    
  2.   
  3. import com.opensymphony.xwork2.ActionInvocation;  
  4. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  5.   
  6. /**   
  7.  * @Title: OtherInterceptor.java  
  8.  * @Package com.xxxxxxxx.interceptor  
  9.  * @Description: TODO(添加描述)  
  10.  * @author longzhun 
  11.  * @date 2011-9-1 下午11:20:58  
  12.  * @version V1.0  
  13.  */  
  14. public class OtherInterceptor extends AbstractInterceptor {  
  15.   
  16.     @Override  
  17.     public String intercept(ActionInvocation invocation) throws Exception {  
  18.   
  19.         System.out.println("other interceptor start");  
  20.         String s  = invocation.invoke();  
  21.         System.out.println("other interceptor end");  
  22.         return s;  
  23.   
  24.     }  
  25.   
  26. }  
  27.   
  28.       

 

struts.xml

Java代码  收藏代码
  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.   
  5.   <constant name="struts.convention.package.locators" value="longzhun"/>  
  6.   <constant name="struts.convention.default.parent.package" value="curd-store"/>  
  7.   <constant name="struts.convention.result.path" value="/WEB-INF/jsp/"/>  
  8.     
  9.   <package name="curd-store" extends="convention-default">  
  10.     <interceptors>  
  11.         <interceptor-stack name="defaultInterceporStack">  
  12.             <interceptor-ref name="defaultInterceptor"></interceptor-ref>  
  13.         </interceptor-stack>  
  14.         <interceptor name="defaultInterceptor" class="com.xxxxxxx.interceptor.MyInterceptor"/>  
  15.         <interceptor name="otherInterceptor" class="com.xxxxx.interceptor.OtherInterceptor"></interceptor>  
  16.     </interceptors>  
  17.     <default-interceptor-ref name="defaultInterceporStack"></default-interceptor-ref>  
  18.   </package>  
  19.     
  20. </struts>      

 

HelloWorld.java

Java代码  收藏代码
  1.  package com.longzhun;    
  2.   
  3. import org.apache.struts2.convention.annotation.InterceptorRef;  
  4. import org.apache.struts2.convention.annotation.InterceptorRefs;  
  5. import org.apache.struts2.convention.annotation.Namespace;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. /**   
  10.  * @Title: HelloWorld.java  
  11.  * @Package 
  12.  * @Description: TODO(添加描述)  
  13.  * @author longzhun 
  14.  * @date 2011-8-29 下午10:22:50  
  15.  * @version V1.0  
  16.  */  
  17. @Namespace("/helloworld")  
  18. @InterceptorRefs({  
  19.     @InterceptorRef("otherInterceptor"),  
  20.     @InterceptorRef("defaultInterceporStack")  
  21. })  
  22. public class HelloWorld extends ActionSupport{  
  23.   
  24.     @Override  
  25.     public String execute() throws Exception {  
  26.           
  27.         System.out.println("execute ok");  
  28.         return SUCCESS;  
  29.               
  30.     }  
  31.     public String haha(){  
  32.         System.out.println("haha ok");  
  33.         return "haha";  
  34.     }  
  35. }  
原创粉丝点击