自定义拦截器获取不到action中的数据

拦截器代码片段:

	public String intercept(ActionInvocation ai) throws Exception{
		
		Object object = ai.getAction();
		if (object != null && object instanceof LoginAction) {
			
			LoginAction loginaction = (LoginAction) object;
			//取得姓名
			String username = loginaction.getUsername();
			if("DLuffy".equals(username)){
				ActionContext ac = ai.getInvocationContext();
				ac.put("exist", "此账号已经被注册");
				return Action.LOGIN;
			}else{
				return ai.invoke();
			}
		}
		return null;
	}

如果struts配置文件里默认拦截器defaultStack在自定义拦截器loginInterceptor的后面,如下
	 	<action name="loginAction" class="com.demo1.action.LoginAction" method="checkLogin">
	 		<result name="success">/index.jsp</result>
	 		<result name="login">/login.jsp</result>
                        <interceptor-ref name="loginInterceptor"/><!--自定义拦截器-->
	 		<interceptor-ref name="defaultStack"/><!--默认拦截器-->
	 	</action>
则自定义拦截器里取得姓名username为null。这是因为默认拦截器将自定义拦截器覆盖。

正确配置应为默认拦截器defaultStack在自定义拦截器loginInterceptor的前面,如下图

	 	<action name="loginAction" class="com.demo1.action.LoginAction" method="checkLogin">
	 		<result name="success">/index.jsp</result>
	 		<result name="login">/login.jsp</result>
			<interceptor-ref name="defaultStack"/><!--默认拦截器-->

                                                    <interceptor-ref name="loginInterceptor"/><!--自定义拦截器--></span>  

                                  </action>

此时因为defaultStack中拦截器params可以解析客户端请求参数。

<interceptor name="params">

<param name="excludeParams">dojo\..*</param>

</interceptor>

所以<interceptor-ref name="defaultStack"/>还可替换成<interceptor-ref name="params"/>

一个action配置了自定义拦截器,如果不显示配置默认拦截器,默认拦截器就不会再起作用。若想继续使用defaultStack就需手动引入。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值