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>
 

来源:https://www.cnblogs.com/super-chao/p/6496428.html

springmvc拦截器的配置、使用的更多相关文章

  1. SpringMVC拦截器的配置与使用详解

         一.SpringMVC拦截器简介      Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...

  2. springMVC 拦截器简单配置

    在spring 3.0甚础上,起来越多的用到了注解,从前的拦截器在配置文件中需要这样配置 <beans...> ... <bean id="measurementInter ...

  3. springMVC拦截器简单配置

    <!-- 拦截器 -->    <mvc:interceptors>        <mvc:interceptor>            <!-- 拦截所 ...

  4. 【Spring】14、SpringMVC拦截器的配置

    拦截器: com.zk.interceptors.MyInterceptor 实现了 HandlerInterceptor接口,可以拦截@RequestMapping注解的类和方法 第一种方式 < ...

  5. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  6. Springboot中SpringMvc拦截器配置与应用(实战)

    一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...

  7. springmvc拦截器和概念,配置!!!

    用于拦截请求,过滤后再拦截 实现HandlerInterceptor接口 配置拦截器 package cn.zys.lanjieqi; import javax.servlet.http.HttpSe ...

  8. springmvc自定义的拦截器以及拦截器的配置

    一.自定义拦截器 Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口. 二.HandlerIn ...

  9. SpringMVC拦截器的使用

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

随机推荐

  1. jsp地址栏传中文显示乱码解决方法

    格式一: 地址栏显示格式:http://localhost:8081/Jsp2/ahref2.jsp?id=32&name=%E7%8E%8B%E4%BA%91%E9%B9%8F 1.修改To ...

  2. MVC中JSON字符长度超出限制

    本文导读:在MVC中通过JsonResult返回JSON字符串时,如果字符串长度过长,会抛出使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 ...

  3. python中的三引号

    在python中,三引号支持字符串跨多行.包含换行符号.制表符号.以及其它特殊字符 >>> hi = ''' ... this ... is a ... test ... ''' & ...

  4. Hadoop启动过程分析

    先把客户端修好,后续慢慢写.  菊子曰:体验离线写博的乐趣

  5. 单例设计模式-java

    在实际项目中单例模式常见应用场景列举如下: 1.servlet编程中,每个servlet就是单例 2.网站计数器,和Application(servlet中涉及) 3.Strucs1框架中,控制器对象 ...

  6. Spring Cloud Eureka自我保护机制(服务无法剔除)

    说明 自我保护背景 首先对Eureka注册中心需要了解的是Eureka各个节点都是平等的,没有ZK中角色的概念, 即使N-1个节点挂掉也不会影响其他节点的正常运行. 默认情况下,如果Eureka Se ...

  7. 【C语言】构造长度可变的二维数组

    #include <stdio.h> #include <malloc.h> #include <memory.h> int getArray(int ***p,i ...

  8. posix多线程--三种基本线程编程模型

    本文介绍了三种构建线程解决方案的方式. 一.流水线:每个线程执行同一种操作,并把操作结果传递给下一步骤的线程. 代码示例如下:终端输入一个int值,每个线程将该值加1,并将结果传给下一个线程. #in ...

  9. FZU Problem 2105 Digits Count

    Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: Operati ...

  10. CPAN镜像使用帮助

    https://lug.ustc.edu.cn/wiki/mirrors/help/cpan ************************************************** 使用 ...