• 通俗易懂之SpringMVC&Struts2前端拦截器详解


    直接进入主题吧!
    一,配置Struts2的拦截器分两步走
    1配置对应的拦截器类:
    2在配置文件Struts.xml中进行配置拦截器
    同时在Strust2中配置拦截器类有三种方法
    1实现Interceptor接口
    2继承AbstractInterceptor
    3通过继承MethodFilterInterceptor类
    区别:
    是否支持方法过滤性:
    使用第1,2 种其实都差不多,都会拦截Action中所有的方法,但是第3种就不一样了,因为第3种支持方法过滤性,可以指定我们想要拦截的方法,(使用includeMethods指明需要拦截的方法-,使用excludeMethods指明拦截器不需要拦截的方法,所以我们通常采用第3种来配置struts2的拦截器。
    3通过继承MethodFilterInterceptor类,重写doIntercept方法
    -------该例子的主要作用: 校验用户是否登录,(通过判断该session中用户名是否为空)若用户登录则跳转到管理界面,若未登录则跳转到登录界面。

    public class RockStar extends MethodFilterInterceptor{
    //重写doInterceptor来实现我们的拦截配置-----Action Invocation是Action的调用者, 该实例代表一个action的执行状态,持有拦截器和将要执行的action的实例。
    protected String doIntercept(ActionInvocation invocation) throws Exception {
    //获得ActionCotext实例(ActionContext中包含了Session)
    ActionContext actionContext=ActionContext.getContext();
    //获得对应的Session,ActionContext其实就是一个很大的map映射结构,使用key来映射value
    Map<String,Object> session=actionContext.getSession();
    //获得session中对应的属性(该属性置为类对象,进行强制转换)
    String username=(String)session.get("username");
    //判断该session中是否存在对应的数据,来知道该用户是否登录
    if(username!=null&&sername.equals("cchh")
    {
    //存在则放行
    System.out.println("该用户合法...."+username);
    //接下来的这个方法的作用是通知Struts2接着去干该干的事情,比如调用下一个拦截器或者执行下一个Action,相当于退出了当前这个interceptor了
    return invocation.invoke();
    }
    else {
    System.out.println("该用户未登录...");
    //若校验失败,则返回一个login字符串,进入登录界面
    return "login";
    }
    }
    }
    有很多人,可能对与ActionContext很疑惑,对Map也很疑惑,接着来看,你就会明白了,一切都有它的道理!
    重点讲解一下,ActionContext----action的上下文,
    简化版:
    Struts----根据request---->创建ActionContext{Session,parameter等},以map形式,key/value存放数据对象------通过getContext---->获得当前ActionContext
    详细版:
    1Strusts2会根据Request给每个执行对应Http请求的线程来创建对应的ActionContext
    ------->即一个Request线程有一个唯一的ActionContext
    ------->我们可以使用静态方法getContext()
    ------->来获得当前线程的ActionContext(Sturts只会给request请求创建的线程创建一个ActionContext)

    2同时该ActionContext中保存了一些在Action执行过程中所需要的对象和数据,比如Session,parameters等,并使用Map集合的形式,这也就是为什么我们可以通过Action Context来获得对应的session,原来是因为其被存储在了Action上下文中

    二在配置文件Struts.xml中进行配置拦截器
    步骤:
    1注册我们所敲的拦截器类
    2定义一个拦截器栈,并在该栈中配置对应的拦截器,同时可以在对应的拦截器中配置拦截哪些方法,不拦截哪些方法
    3通常我们会配置<default-interceptor-ref name="拦截器栈名"/>来自动拦截所的action
    4也可以通过<interceptor-ref name="拦截器栈名"/>

    <package name=default" extends="struts-default" namespace"/">
    <interceptors>
    <!--1注册拦截器-->
    <interceptor name="rockStar" class="com.ali.demo.Rockstar”>
    </intertceptor>
    <!--配置拦截器栈-->
    <interceptor-stack name="myStack">
    <!--Struts默认执行的拦截器-->
    <interceptor-ref name="defaultStack"></interception-ref>
    <!--注意这个地方,要将默认的拦截器放置在最前面,这样struts2才会默认加载必须要加载的拦截器-->
    <interceptor-ref name="rockstar">
    <!--指定哪些方法不拦截,exclude,排除的意思-->
    <param name="excludeMethods">login</param>
    </intercepor-ref>
    </interceptor-stack>
    </interceptors>
    //为所有的Action自动调用拦截器栈
    //<default-interceptor-ref name="myStack"></default-interceptor-ref>


    ------------------------以上完成了拦截器的注册和配置
    1若方法被拦截,则会直接从拦截器处返回,不会接着继续执行。
    2若该方法未被拦截,那么会接着往下进行执行,
    同时可以使用标签<interceptor-ref name="myStack" />来给对应的类指定拦截器栈

    <!--通过Action访问后台管理页面-->
    <action name="auth">
    <result>/WEB-INF/jsp/manage.jsp</result>
    <!--校验失败的返回路径,对应拦截器中的字符串login-->
    <result name="login">/login.jsp</result>
    <!--在该action中引用对应的拦截器栈-->
    <interceptor-ref name="myStack" ></interceptor-ref>
    </action>


    <action name="manager" class="userAction" method={1}>
    <result name="name">/WEB-INF/jsp/manage.jsp</result>
    <result name="error">/login.jsp</result>
    </action>
    </package>

    以上就是对于Struts2中拦截器的解答了!

    SpringMVC拦截器配置
    接下来再看看SpringMVC的拦截器配置,比起Struts2会简单很多
    首先了解SpringMVC的拦截器中,主要通过实现HandlerInterceptor接口类,并重写其三个方法,分别是前preHandler(在请求到达Controller之前,进行拦截) 中postHandler(在请求达到了Controller中,且ModelandView参数未返回之前)后afterHandler(在执行完该方法之后)
    同样是两步:
    1编写拦截器类:

    public class SpMvc implements HandlerInterceptor{
    **//请求到达Controller之前**
    public boolean preHandle (HttpServletRequest request,HttpServletResponse reponse,Object obj) throws Exception{
    **//获得Session,**
    HttpSession session=request.getSession();
    **//从session中取得对应的值**
    String name=(String) session.getAttribute("name");
    **//判断该值是否存在**
    if(name!=null&&name.equals("cchh"){
    System.out.println("用户合法,通过拦截");
    return true;//**这里true代表通过拦截器**
    }
    else
    response.sendRedirect("index");//将页面跳转到index页面
    return false;//**这里 false代表被拦截**
    }
    //**在请求达到了Controller中,且ModelandView参数未返回之前**
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav)
    throws Exception{
    System.out.println("此时正在执行方法中....");
    }
    **//在请求方法执行完毕之后,对象中就没有对应的ModelAndView参数了**
    public class afterHandle(HttpServletRequest request,httpServletResponse response,Object obj) throws exception{
    System.out.println("执行方法之后....");
    }
    }
    2在SpringMVC中配置对应的拦截器
    SpringMVC拦截器的配置相对于Struts2来说,相对简单很多
    主要有
    1配置拦截器类的拦截器
    2设置要拦截的请求<mvc:mapping/>struts2中是<param name="includeMethods"/>
    3设置不拦截的请求<mvc:exclude-mapping/>struts2中是<param name="excludeMethods">

    <!--配置拦截器-->
    <mvc:interceptors>
    <mvc:interceptor>
    <!--拦截器所在的类路径-->
    <bean class="com.ali.demo.SpMvc"/></bean>
    <!--拦截的请求,拦截的请求-->
    <mvc:mapping path="/**"/>
    <!--不拦截的请求-->
    <mvc:exclude-mapping path="/loginController/login.do"/>
    <./mvc:interceptor>
    </mvc:interceptors>

  • 相关阅读:
    图片压缩后,依然很大的解决方案
    怎么使用javascript实现类的功能
    javascript实现像java、c#之类的sleep暂停的函数功能
    用ajax和asp.net实现智能搜索功能
    insert into 和insert into select性能比较
    百度编辑器
    document.selection.createRange()
    CSS设置透明效果
    class中一个小技巧
    asp.net中 解析JSON
  • 原文地址:https://www.cnblogs.com/ly570/p/10961069.html
Copyright © 2020-2023  润新知