zoukankan      html  css  js  c++  java
  • Struts2 拦截器—拦截action

    对于拦截器的基本使用这里我就懒得打字了,我这里就讲下如何用 Struts2 拦截器 拦截action。这是我个人的想法,如果有什么不对的,或者你们有什么更好的方法。请多多留言!

        

           拦截器的默认拦截的方法参数是:includeMethods(要拦截的方法) 和 excludeMethods(不需要拦截的方法),多个的时候,用逗号分开;

           但,现实中,有些时候。我们还是需要设置有3 个类的所有方法都不需要拦截和4个方法不需要拦截,那么如果使用excludeMethods 的话,就得把 3 个类中的所有方法都列出来。当然如果少就列列就算了,如果多呢。不就麻烦了。所以。下面我们将讨论如何拦截 类 这个级别的请求。

    一、在配置文件中修改下:

    Xml代码 复制代码 收藏代码
    1. <interceptors>  
    2.         <interceptor name="mainSession" class="net.zy.interceptor.MainSessionInterceptor"></interceptor>  
    3.         <interceptor-stack name="mainSessionStack">  
    4.             <interceptor-ref name="mainSession">  
    5.                 <param name="excludeMethods">login,logOut</param>  
    6.                 <!-- 自定义参数 excludeActions -->  
    7.                 <param name="excludeActions">sms_,main_</param>  
    8.             </interceptor-ref>  
    9.             <!-- 默认拦截器 -->  
    10.             <interceptor-ref name="defaultStack"></interceptor-ref>  
    11.         </interceptor-stack>    
    12.     </interceptors>  
    13.     <!-- 设置未默认的拦截器 -->  
    14.     <default-interceptor-ref name="mainSessionStack"></default-interceptor-ref>  
    		<interceptors>
     			<interceptor name="mainSession" class="net.zy.interceptor.MainSessionInterceptor"></interceptor>
     			<interceptor-stack name="mainSessionStack">
     				<interceptor-ref name="mainSession">
     					<param name="excludeMethods">login,logOut</param>
     					<!-- 自定义参数 excludeActions -->
     					<param name="excludeActions">sms_,main_</param>
     				</interceptor-ref>
     				<!-- 默认拦截器 -->
     				<interceptor-ref name="defaultStack"></interceptor-ref>
     			</interceptor-stack> 
     		</interceptors>
     		<!-- 设置未默认的拦截器 -->
     		<default-interceptor-ref name="mainSessionStack"></default-interceptor-ref>

     在配置文件中,添加自定义参数。excludeActions

    二、在拦截器类中,添加 excludeActions  成员变量,并且提供get,set方法,struts2会自定给我们set参数进来。

      

    Java代码 复制代码 收藏代码
    1. package net.zy.interceptor;    
    2.   
    3. import javax.servlet.http.HttpServletRequest;   
    4. import javax.servlet.http.HttpSession;    
    5. import net.zy.models.SysUsers;   
    6. import net.zy.models.VipBean;    
    7. import org.apache.struts2.ServletActionContext;    
    8. import com.opensymphony.xwork2.ActionInvocation;   
    9. import com.opensymphony.xwork2.interceptor.Interceptor;   
    10.   
    11. /**  
    12.  * 拦截器  
    13.  *   
    14.  * @author 妞见妞爱  
    15.  */  
    16. public class MainSessionInterceptor implements Interceptor{   
    17.   
    18.     private String excludeActions;   
    19.     private String excludeMethods;     
    20.         
    21.        
    22.     public String getExcludeMethods() {   
    23.         return excludeMethods;   
    24.     }   
    25.   
    26.     public void setExcludeMethods(String excludeMethods) {   
    27.         this.excludeMethods = excludeMethods;   
    28.     }   
    29.   
    30.     public String getExcludeActions() {   
    31.         return excludeActions;   
    32.     }   
    33.   
    34.     public void setExcludeActions(String excludeActions) {   
    35.         this.excludeActions = excludeActions;   
    36.     }   
    37.   
    38.     public void destroy() {   
    39.         // TODO Auto-generated method stub   
    40.            
    41.     }   
    42.   
    43.     public void init() {   
    44.         // TODO Auto-generated method stub   
    45.            
    46.     }   
    47.   
    48.         
    49.     public String intercept(ActionInvocation invocation) throws Exception {    
    50.            
    51.         HttpServletRequest request = ServletActionContext.getRequest();   
    52.         HttpSession session = request.getSession();   
    53.             
    54.         // 获取请求的action名称   
    55.         String actionName = invocation.getInvocationContext().getName();       
    56.         // 获取action后附带参数       
    57.         //Map parameters = invocation.getInvocationContext().getParameters();       
    58.   
    59.         //配置文件中 排除的 action   
    60.         String [] excludeAction = excludeActions.split(",");   
    61.         for (int i = 0; i < excludeAction.length; i++) {   
    62.             if (actionName.startsWith(excludeAction[i])) {   
    63.                 return invocation.invoke();    
    64.             }   
    65.         }   
    66.            
    67.         //配置文件中 排除的 Method   
    68.         String [] excludeMethod = excludeMethods.split(",");   
    69.         for (int i = 0; i < excludeMethod.length; i++) {   
    70.             if (actionName.endsWith(excludeMethod[i])) {   
    71.                 return invocation.invoke();    
    72.             }   
    73.         }   
    74.            
    75.         SysUsers  users = (SysUsers) session.getAttribute("user");   
    76.         if (users != null) {   
    77.             return invocation.invoke();   
    78.         }   
    79.             
    80.                    
    81.         return "Timeout";   
    82.     }   
    83.   
    84.         
    85.   
    86. }  
    package net.zy.interceptor; 
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession; 
    import net.zy.models.SysUsers;
    import net.zy.models.VipBean; 
    import org.apache.struts2.ServletActionContext; 
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.Interceptor;
    
    /**
     * 拦截器
     * 
     * @author 妞见妞爱
     */
    public class MainSessionInterceptor implements Interceptor{
    
    	private String excludeActions;
    	private String excludeMethods;  
    	 
    	
    	public String getExcludeMethods() {
    		return excludeMethods;
    	}
    
    	public void setExcludeMethods(String excludeMethods) {
    		this.excludeMethods = excludeMethods;
    	}
    
    	public String getExcludeActions() {
    		return excludeActions;
    	}
    
    	public void setExcludeActions(String excludeActions) {
    		this.excludeActions = excludeActions;
    	}
    
    	public void destroy() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void init() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	 
    	public String intercept(ActionInvocation invocation) throws Exception { 
    		
    		HttpServletRequest request = ServletActionContext.getRequest();
    		HttpSession session = request.getSession();
    		 
    		// 获取请求的action名称
    		String actionName = invocation.getInvocationContext().getName();    
    		// 获取action后附带参数    
            //Map parameters = invocation.getInvocationContext().getParameters();    
    
    		//配置文件中 排除的 action
    		String [] excludeAction = excludeActions.split(",");
    		for (int i = 0; i < excludeAction.length; i++) {
    			if (actionName.startsWith(excludeAction[i])) {
    				return invocation.invoke(); 
    			}
    		}
    		
    		//配置文件中 排除的 Method
    		String [] excludeMethod = excludeMethods.split(",");
    		for (int i = 0; i < excludeMethod.length; i++) {
    			if (actionName.endsWith(excludeMethod[i])) {
    				return invocation.invoke(); 
    			}
    		}
    		
    		SysUsers  users = (SysUsers) session.getAttribute("user");
    		if (users != null) {
    			return invocation.invoke();
    		}
    		 
    		 		
    		return "Timeout";
    	}
    
    	 
    
    }
    

       嘿嘿。。。就这样了....简单点。。。。。

  • 相关阅读:
    windows下区块链,私有链搭建详细教程(图文详解)
    MySQL 5.7 mysqldump的Bug导致复制异常
    关于MySQL 5.6 DDL阻塞DML的问题!
    mysqldump 根据时间字段导出数据的问题
    MySQL undo redo
    InnoDB undo, redo,binlog,data什么时候写?
    MySQL master 宕机导致slave数据比master多的case
    Jboss配置自动重连数据库
    星爷001正式开始写blog啦
    本地及远程二级缓存
  • 原文地址:https://www.cnblogs.com/is1988/p/3210542.html
Copyright © 2011-2022 走看看