Struts2中是用拦截器拦截黑名单的demo

1.interceptor的实例,重要的是如何获取请求的参数

package com.sid.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.sid.domain.User;

public class LoginInterceptor extends MethodFilterInterceptor {

    private Map<String,Object> request;
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        //此方法下可以调用service层访从而访问数据库
        request = (Map<String, Object>) ActionContext.getContext().get("request"); 
        User user = (User) request.get("user");
        if(user.getUsername().equals("pxc")){
            return "errorName"; //action中相应方法的返回结果
        }
        else{
            return invocation.invoke();
        }
    }

}

    <interceptors>
        <interceptor name="secondinter" class="com.sid.interceptor.SecondInterceptor"></interceptor>
        <interceptor name="firstinter" class="com.sid.interceptor.FirstInterceptor"></interceptor>
        <interceptor name="thridinter" class="com.sid.interceptor.ThridInterceptor">
            <!-- <param name="excludeMethods">add,update</param> -->   <!--不包括这两个方法,这两个方法不拦截,其他的拦截 -->
            <param name="includeMethods">add,update</param>    <!--包括这两个方法,对这两个方法拦截 -->
        </interceptor>
        <interceptor name="checkLogin" class="com.sid.interceptor.LoginInterceptor">
            <param name="includeMethods">checkLogin</param> 
        </interceptor>
        <interceptor-stack name="my">
        <!--系统默认的拦截器  -->
           <interceptor-ref name="defaultStack"></interceptor-ref>
           <interceptor-ref name="secondinter"></interceptor-ref>
           <interceptor-ref name="firstinter"></interceptor-ref>
           <interceptor-ref name="thridinter"></interceptor-ref>
           <interceptor-ref name="checkLogin"></interceptor-ref>

    </interceptor-stack>    
    </interceptors>
    <action name="checklogin" class="com.sid.web.action.LoginAction" method="checkLogin">
       <!--此处哪个拦截器配在前面,先执行哪个.若引用了自定义的拦截器,必须引用系统默认的拦截器-->
        <!-- <interceptor-ref name="firstinter"></interceptor-ref>
        <interceptor-ref name="secondinter"></interceptor-ref> 
        <interceptor-ref name="defaultStack"></interceptor-ref> -->
        <interceptor-ref name="my"></interceptor-ref>

        <result name="success">/success.jsp</result>
        <result name="errorName">/error.jsp</result>
    </action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值