简介
SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自己定义一些拦截器来实现特定的功能。
过滤器
- servlet规范中的一部分,任何java web工程都可以使用
- 在url-pattern中配置了/*之后,可以对所有要访问的资源进行拦截
拦截器
- 拦截器是SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
- 拦截器只会拦截访问的控制器方法, 如果访问的是jsp/html/css/image/js是不会进行拦截的
过滤器与拦截器的区别:
拦截器是AOP思想的具体应用。
拦截器初体验
1.新建一个项目,添加web支持,在IDEA导入该项目依赖的lib包。
2.配置web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version = "4.0" >
< servlet >
< servlet-name >springmvc</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
< init-param >
< param-name >contextConfigLocation</ param-name >
< param-value >classpath:springmvc-servlet.xml</ param-value >
</ init-param >
< load-on-startup >1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >springmvc</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
< filter >
< filter-name >encoding</ filter-name >
< filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class >
< init-param >
< param-name >encoding</ param-name >
< param-value >UTF-8</ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name >encoding</ filter-name >
< url-pattern >/*</ url-pattern >
</ filter-mapping >
</ web-app >
|
3.配置springmvc-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:mvc = "http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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">
<!-- 扫描指定包,让指定包下的注解生效 -->
< context:component-scan base-package = "controller" />
< mvc:annotation-driven />
<!-- JSON解决乱码-->
< mvc:annotation-driven >
< mvc:message-converters register-defaults = "true" >
< bean class = "org.springframework.http.converter.StringHttpMessageConverter" >
< constructor-arg value = "UTF-8" />
</ bean >
< bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
< property name = "objectMapper" >
< bean class = "org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" >
< property name = "failOnEmptyBeans" value = "false" />
</ bean >
</ property >
</ bean >
</ mvc:message-converters >
</ mvc:annotation-driven >
<!-- 视图解析器 -->
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" id = "internalResourceViewResolver" >
< property name = "prefix" value = "/WEB-INF/jsp/" />
< property name = "suffix" value = ".jsp" />
</ bean >
</ beans >
|
4.编写自定义的拦截器。(实现HandlerInterceptor接口)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
//在请求处理的方法之前执行
//如果返回true执行下一个拦截器
//如果返回false就不执行下一个拦截器
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println( "------------处理前------------" );
return true ;
}
//在请求处理方法执行之后执行
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println( "------------处理后------------" );
}
//在dispatcherServlet处理后执行,做清理工作.
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println( "------------清理------------" );
}
}
|
事实上,只要重写preHandle方法就可以。
5.在springmvc-servlet.xml文件中配置拦截器
1
2
3
4
5
6
7
8
9
10
11
|
<!--关于拦截器的配置-->
< mvc:interceptors >
< mvc:interceptor >
<!--/** 包括路径及其子路径-->
<!--/admin/* 拦截的是/admin/add等等这种 , /admin/add/user不会被拦截-->
<!--/admin/** 拦截的是/admin/下的所有-->
< mvc:mapping path = "/**" />
<!--bean配置的就是拦截器-->
< bean class = "interceptor.MyInterceptor" />
</ mvc:interceptor >
</ mvc:interceptors >
|
6.编写controller
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InterceptorController {
@RequestMapping ( "/interceptor" )
public String test(){
System.out.println( "InterceptorController" );
return "ok" ;
}
}
|
7.配置Tomcat,进行测试
初体验:自定义拦截器实现了HandlerInterceptor接口,重写了preHandle方法。在preHandle方法中,返回值决定了是否拦截,当返回值为true时,不拦截;反之则拦截。
8.结果:
- 返回值为true,拦截器不拦截,跳转
- 返回值为false,拦截器拦截,不跳转
拦截器再体验-登录验证
实现思路
- 有一个登陆页面,需要写一个controller访问页面。
- 登陆页面有一提交表单的动作。需要在controller中处理。判断用户名密码是否正确。如果正确,向session中写入用户信息。返回登陆成功。
- 拦截用户请求,判断用户是否登陆。如果用户已经登陆。放行, 如果用户未登陆,跳转到登陆页面
登录界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ page contentType= "text/html;charset=UTF-8" language= "java" %>
<html>
<head>
<title>Title</title>
</head>
<h1>登录页面</h1>
<hr>
<body>
<form action= "${pageContext.request.contextPath}/user/login" >
用户名:<input type= "text" name= "username" > <br>
密码:<input type= "password" name= "pwd" > <br>
<input type= "submit" value= "提交" >
</form>
</body>
</html>
|
controller处理请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping ( "/user" )
public class UserController {
//跳转到登陆页面
@RequestMapping ( "/jumplogin" )
public String jumpLogin() throws Exception {
return "login" ;
}
//跳转到成功页面
@RequestMapping ( "/jumpSuccess" )
public String jumpSuccess() throws Exception {
return "success" ;
}
//登陆提交
@RequestMapping ( "/login" )
public String login(HttpSession session, String username, String pwd) throws Exception {
// 向session记录用户身份信息
System.out.println( "接收前端===" +username);
session.setAttribute( "user" , username);
return "success" ;
}
//退出登陆
@RequestMapping ( "logout" )
public String logout(HttpSession session) throws Exception {
// session 过期
session.invalidate();
return "login" ;
}
}
|
登录成功页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType= "text/html;charset=UTF-8" language= "java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功页面</h1>
<hr>
${user}
<a href= "${pageContext.request.contextPath}/user/logout" >注销</a>
</body>
</html>
|
index页面
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page contentType= "text/html;charset=UTF-8" language= "java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>首页</h1>
<hr>
<%--登录--%>
<a href= "${pageContext.request.contextPath}/user/jumplogin" >登录</a>
<a href= "${pageContext.request.contextPath}/user/jumpSuccess" >成功页面</a>
</body>
</html>
|
index页面
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html >
< head >
< title >$Title$</ title >
</ head >
< body >
< h1 >首页</ h1 >
< hr >
<%--登录--%>
< a href = "${pageContext.request.contextPath}/user/jumplogin" >登录</ a >
< a href = "${pageContext.request.contextPath}/user/jumpSuccess" >成功页面</ a >
</ body >
</ html >
|
编写控制器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getRequestURI().contains( "login" )) {
return true ;
}
HttpSession session = request.getSession();
// 如果用户已登陆,不拦截
if (session.getAttribute( "user" ) != null ) {
return true ;
}
// 用户没有登陆,拦截,并跳转到登陆页面
request.getRequestDispatcher( "/WEB-INF/jsp/login.jsp" ).forward(request, response);
return false ;
}
}
|
在springmvc-servlet.xml配置拦截器
1
2
3
4
|
< mvc:interceptor >
< mvc:mapping path = "/**" />
< bean id = "loginInterceptor" class = "interceptor.LoginInterceptor" />
</ mvc:interceptor >
|
配置Tomcat,测试。
结果:没有登录就无法直接访问登陆成功页面。
以上就是SpringMVC 中文乱码的解决方案的详细内容,更多关于SpringMVC 中文乱码的资料请关注服务器之家其它相关文章!
原文链接:https://juejin.cn/post/6944667529717481485