Struts(三,上)拦截器的使用

拦截器的使用

回顾:过滤器 监听器

1. 什么是拦截器? 为什么使用拦截器?

拦截请求的目标方法,在执行action之前,进行拦截操作。

action有很多的默认拦截器,比如:action给属性和方法赋值,如何实现该功能?

使用了struts2框架中的预处理拦截器:调用set方法给参数赋值;

   2.拦截器 过滤器区别?

   3.如何使用拦截器?

说明拦截器需要继承父类?

methodfilterInterceptor 和 abstractInterceptor 拦截器父类

 

methodfilterInterceptor 底层继承了abstractInterceptor 类,同时:methodfilterInterceptor 里面定义了两个参数,分别是:excludeMethods 和 includeMethods;

 

abstractInterceptor:类不能指定拦截方法,但是可以通过初始化参数 实现放行/拦截 操作;

 

实现步骤:

1. 定义一个拦截器类(必须继承父类:methodfilterInterceptor 和 abstractInterceptor)

package com.zgq.interceptor;

import java.util.Map;
import java.util.Set;

import javax.websocket.Decoder.Text;



import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.zgq.model.User;

public class Myabsinterceptor extends AbstractInterceptor{
          
	private String excludeActionName;
	private User seesionName;
	
	 
	public String getExcludeActionName() {
		return excludeActionName;
	}
	public void setExcludeActionName(String excludeActionName) {
		this.excludeActionName = excludeActionName;
	}
	public User getSeesionName() {
		return seesionName;
	}
	public void setSeesionName(User seesionName) {
		this.seesionName = seesionName;
	}
	
	
	
	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		        
		String actionName = arg0.getProxy().getActionName();
		String actionMethod = arg0.getProxy().getMethod();
		
		System.out.println("代理类=="+actionName+"代理方法=="+actionMethod);
		
		Set<String>  set =TextParseUtil.commaDelimitedStringToSet(excludeActionName);
		
		if(set.contains(actionName)){
			       System.out.println("这个"+actionName+"被放行了");
			     return arg0.invoke();
			}else{
				 
				Map<String, Object>  map = arg0.getInvocationContext().getSession();
				User userEnt = (User)map.get("userEnt");
				
				if(userEnt == null){
					return "error";
				}else{
					arg0.invoke();
				}
			}
		return null;
	}
             
}

 

2. 从写父类的方法 intercept()方法。(在执行action目标方法之前执行)

3. 配置当前拦截器,在struts2中启用[自定义拦截器后,默认拦截器 不会被执行,在拦截器栈中的第一行必须加入,struts2框架的默认拦截器];

 <package name="pk" extends="json-default,struts-default" namespace="/">
          <!-- 配置自定义拦截器 -->
          <interceptors>
         <!--  引入拦截器类 -->
               <interceptor name="myinterceptor" class="com.zgq.interceptor.Myabsinterceptor"></interceptor>
               <!-- 定义拦截器栈 -->
               <interceptor-stack name="myStack">
                <!--   引入默认拦截器和自定义拦截器 -->
                  <interceptor-ref name="defaultStack"></interceptor-ref>
                  <interceptor-ref name="myinterceptor">
                    <!--  参数设置 -->
                     <param name="excludeActionName">lrx_login</param>
                  </interceptor-ref>
               </interceptor-stack>
          </interceptors>
          
        <!--  启动拦截器 -->
        <default-interceptor-ref name="myStack"></default-interceptor-ref>
          <action name="lrx_*" class="com.zgq.action.LoginAction" method="{1}">
                 <result name="success">/index.jsp</result>
                 <result name="error">/login.jsp</result>
          </action>
      </package>

4. 拦截器 的拦截业务逻辑;[自己写]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值