• SpringMVC框架六:拦截器


    拦截器的概念就不介绍了:

    SpringMVC中拦截器的执行规则:

    一个小示例展示:

    我搞两个拦截器:

    第一个:

    package org.dreamtech.springmvc.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class Interceptor1 implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                throws Exception {
            System.out.println("页面渲染后1");
    
        }
    
        @Override
        public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                throws Exception {
            System.out.println("方法后1");
    
        }
    
        @Override
        public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
            System.out.println("方法前1");
            return true;
        }
    
    }

    第二个:

    package org.dreamtech.springmvc.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class Interceptor2 implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                throws Exception {
            System.out.println("页面渲染后2");
    
        }
    
        @Override
        public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                throws Exception {
            System.out.println("方法后2");
    
        }
    
        @Override
        public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
            System.out.println("方法前2");
            return true;
        }
    
    }

    然后把它配置到SpringMVC.xml中:

        <!-- 配置拦截器 -->
        <mvc:interceptors>
            <!-- 配置多个拦截器 -->
            <mvc:interceptor>
                <!-- 拦截所有 -->
                <mvc:mapping path="/**" />
                <!-- 自定义拦截器类 -->
                <bean class="org.dreamtech.springmvc.interceptor.Interceptor1" />
            </mvc:interceptor>
            <mvc:interceptor>
                <!-- 拦截所有 -->
                <mvc:mapping path="/**" />
                <!-- 自定义拦截器类 -->
                <bean class="org.dreamtech.springmvc.interceptor.Interceptor2s" />
            </mvc:interceptor>
        </mvc:interceptors>

    执行之后:

    然而我们如果把拦截器一的方法前不放行:

        @Override
        public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
            System.out.println("方法前1");
            return false;
        }

    这时候,发现控制台只是打印了:方法前1

    但是,我们如果把拦截器二的方法前不放行:

    打印的却是:

    到这里就可以看出SpringMVC的执行规则了:

    为了方便,我借用某博主的图片:

    文字解释:

    preHandler按拦截器定义顺序调用,

    postHandler按拦截器定义逆序调用,

    afterCompletion按拦截器定义逆序调用,

    postHandler在所有的拦截器返回成功时候才会调用,

    afterCompletion在preHandler返回true才会调用

    应用:

    登录功能:只有登录才能查看其他信息

    限制:在非登陆状态下无法访问该网站任何一处

    随意写一个登录页面:

    <body>
        <form action="${pageContext.request.contextPath }/login.action" method="post">
            用户名:<input type="text" name="username"> 
    <input type="submit" value="提交"> </form> </body>

    只用一个拦截器:

        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**" />
                <bean class="org.dreamtech.springmvc.interceptor.Interceptor2" />
            </mvc:interceptor>
        </mvc:interceptors>

    拦截器:

    package org.dreamtech.springmvc.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    public class Interceptor2 implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
                throws Exception {
    
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
                throws Exception {
    
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
            String requestURI = request.getRequestURI();
            /*
             * URI:/login.action
             * URL:http://xxx/xxx/login.action
             */
            if(!requestURI.contains("/login")){
                String username = (String) request.getSession().getAttribute("USER_SESSION");
                if(username==null){
                    response.sendRedirect(request.getContextPath()+"/login.action");
                    return false;
                }
            }
            return true;
        }
    
    }

    实现:

        @RequestMapping(value = "/login.action", method = RequestMethod.GET)
        public String login() {
            return "login";
    
        }
    
        @RequestMapping(value = "/login.action", method = RequestMethod.POST)
        public String login(String username, HttpSession httpSession) {
            httpSession.setAttribute("USER_SESSION", username);
            return "redirect:/item/itemlist.action";
    
        }

    这里注意:我直接返回字符串:login是因为提前做了配置:

        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
  • 相关阅读:
    ABAP-创建客户
    Windows 10 上的 Git 如何清除密码? Git Credential Manager for Windows
    FastAdmin 是如何利用 Git 管理插件代码的?
    关于 ThinkPHP5 使用 getBy 字段名方式获取数据
    如何开始一个电子硬件项目?(思维导图)
    随笔:关于 FastAdmin ueditor 插件 中的 rand mt_rand mt_getrandmax 问题
    Chrome 的应用功能越来越强大
    FastAdmin Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考)
    随笔教程:FastAdmin 如何打开新的标签页
    odoo 数据库选择的随笔
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/9458564.html
Copyright © 2020-2023  润新知