• springmvc拦截器的配置、使用


    1.自定义拦截器,实现HandlerInterceptor接口

    package com.bybo.aca.web.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 Login implements HandlerInterceptor {
    
        @Override
        public void afterCompletion(HttpServletRequest httpRequest,
                HttpServletResponse httpResponse, Object arg2, Exception arg3)
                throws Exception {
             
        }
    
        @Override
        public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                Object arg2, ModelAndView arg3) throws Exception {
            
    
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                Object object) throws Exception {
            /*HttpServletRequest httpRequest = (HttpServletRequest) request;  
             HttpServletResponse httpResponse = (HttpServletResponse) response;*/
            String urlString = request.getRequestURI();
                
            ///olForum/forumList.html模拟登录页
            if(urlString.endsWith("forumList.html")){
                return true;
            }
            //请求的路径
            String contextPath=request.getContextPath();
           
            /*httpRequest.getRequestDispatcher("/olForum/forumList").forward(httpRequest, httpResponse);*/
            /*response.sendRedirect(contextPath+"/olForum/forumList.html");*/
            response.sendRedirect(contextPath + "/olForum/forumList.html?login=aaa");  
            return false;
            /*httpResponse.sendRedirect(httpRequest.getContextPath()+"/olForum/forumList.html");
            return;*/
            
    
        }
    
    }

    2、springmvc配置文件中配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!-- 扫描controller(controller层注入) -->
        <context:component-scan base-package="com.bybo.aca.web.controller"/>
      
    <mvc:interceptors>  
       <!--  使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求   -->
        <!-- <bean class="com.bybo.aca.web.interceptor.Login"/> -->   
        <mvc:interceptor>  
            <!-- 进行拦截:/**表示拦截所有controller -->
            <mvc:mapping path="/**" />
           <!-- 不进行拦截 -->
            <mvc:exclude-mapping path="/index.html"/>
           
            <bean class="com.bybo.aca.web.interceptor.Login"/>  
        </mvc:interceptor>  
    </mvc:interceptors>     
        
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
        
    </beans>

    原文出处:

    [1] super超人, springmvc拦截器的配置、使用, https://www.cnblogs.com/super-chao/p/6496428.html

    Reference:

    [2] jinnianshilongnian, 五章 处理器拦截器详解——跟着开涛学SpringMVC, http://jinnianshilongnian.iteye.com/blog/1670856/

  • 相关阅读:
    标签和过滤器
    【android】简单的布局和控件&简单的练习作品
    【android】配置模拟器以及第一个“Hello World!”
    【android】sdk安装及环境变量配置、android studio的安装及新建项目
    【javaweb】库存物资管理系统思路与总结
    【java】关于异常处理的思考
    【作业】对于对象的课程作业
    【作业】神奇的代码,包装类Integre,100==100,129!=129
    【作业】随机数+参数可变的方法+实验任务(输出素数+使用递归,判断是否为回文+统计一篇英语问斩单词出现频率)
    【作业】三个关于java的探索和两个实验题
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104054.html
Copyright © 2020-2023  润新知