springmvc拦截器的配置、使用
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拦截器的配置、使用的更多相关文章
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...
- springMVC 拦截器简单配置
在spring 3.0甚础上,起来越多的用到了注解,从前的拦截器在配置文件中需要这样配置 <beans...> ... <bean id="measurementInter ...
- springMVC拦截器简单配置
<!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截所 ...
- 【Spring】14、SpringMVC拦截器的配置
拦截器: com.zk.interceptors.MyInterceptor 实现了 HandlerInterceptor接口,可以拦截@RequestMapping注解的类和方法 第一种方式 < ...
- spring boot配置springMVC拦截器
spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...
- Springboot中SpringMvc拦截器配置与应用(实战)
一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...
- springmvc拦截器和概念,配置!!!
用于拦截请求,过滤后再拦截 实现HandlerInterceptor接口 配置拦截器 package cn.zys.lanjieqi; import javax.servlet.http.HttpSe ...
- springmvc自定义的拦截器以及拦截器的配置
一.自定义拦截器 Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口. 二.HandlerIn ...
- SpringMVC拦截器的使用
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
随机推荐
- 基于HTTP在互联网传输敏感数据的消息摘要、签名与加密方案
基于HTTP在互联网传输敏感数据的消息摘要.签名与加密方案 博客分类: 信息安全 Java 签名加密AESMD5HTTPS 一.关键词 HTTP,HTTPS,AES,SHA-1,MD5,消息摘要,数 ...
- Linux内核(15) - 方法论
什么是方法论 方法论给人的第一感觉就是它是一个玄之又玄的很朦胧的东西,显然,学习本身就是一件很玄乎的事情,有些人整天潇潇洒洒没见怎么用心就能够获得很好的成绩,而有些人则相反,投悬梁锥刺骨也还是成绩平平 ...
- Vue 最传统的新增行,删除行,提交的数据整合
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 如何在virtualenv虚拟环境中安装mysql-python
接触过virtualenv后,想在这个虚拟环境中安装独立的开发环境.在安装MySQLdb时遇到错误 pc 09:09:30 File "/home/pc/work/VENV/py3/loca ...
- python 例程的一个好例子
用例程来写一个求平均值的算法 #!/usr/local/python/bin/python3 def FunCore(): total=0 counter=0 average=None tmp=yie ...
- RUBY Error: Please update your PATH to include build tools or download the DevKit
出错的原因是安装XXXXX的时候,需要build tools,但系统中没有.出错信息中同时也给出了解决的法案: 1. 到 http://rubyinstaller.org/downloads/ 去下载 ...
- angularJS核心原理
一.angularJS优点和缺点 优点 1.1免去重复劳动-获取元素.给元素加事件.创建元素 1.2接管UI:angularJS根据数据创建UI元素,免去手工创建UI元素. 1.3自动同步:根据数据个 ...
- 判断js对象是否拥有某一个属性的js代码
js对象是否拥有某一个属性的判断方法有很多. 本文分享一个简单的方法,如下: <script> /** * 判断js对象是否具有某属性 * by www.jbxue.com */ var ...
- Photoshop制作Android UI: 怎样将图片背景变为透明
看烦了代码.今天玩玩PS吧.本人是PS小白.Android开发中不可避免的要做一些图片,但我发现居然没有相似的专门教程.真想拜个美工为师.还记得2012年去宁波实习时为了将图片缩小我还matlab写个 ...
- Linux系统中 Sublime Text 中文 GBK 文件乱码问题
Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就 ...