SpringMVC(5)
1.案例一:解决中文乱码问题(character-encoding-filter)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cskaoyan</groupId>
<artifactId>demo1-character-encoding-filter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
</dependencies>
</project>
BaseRespVo
package com.cskaoyan.bean;
import lombok.Data;
@Data
public class BaseRespVo<T> {
T data;
String message;
int errno;
public static BaseRespVo ok(){
BaseRespVo baseRespVo = new BaseRespVo();
baseRespVo.setMessage("成功");
baseRespVo.setErrno(0);
return baseRespVo;
}
public static BaseRespVo ok(Object data){
BaseRespVo baseRespVo = new BaseRespVo();
baseRespVo.setData(data);
baseRespVo.setMessage("成功");
baseRespVo.setErrno(0);
return baseRespVo;
}
}
User
package com.cskaoyan.bean;
import lombok.Data;
@Data
public class User {
String username;
String password;
}
UserController
package com.cskaoyan.controller;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("login")
public BaseRespVo login(User user){
return BaseRespVo.ok(user);
}
}
application.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cskaoyan"/>
<mvc:annotation-driven/>
</beans>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="login" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="text" name="password"><br>
<input type="submit">
</form>
</body>
</html>
web.xml
1 CharacterEncodingFilter
CharacterEncodingFilter的doFilter → doFilterInternal方法 → setCharacterEncoding
setEncoding → utf-8
<?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">
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--调用set方法-->
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
结果:
2.案例二-SpringMVC中的拦截器(handler-interceptor)
2.1 原理
功能类似于filter
通过HandlerMapping会生成一个HandlerExecutionChain(执行链) → List Handler
底层代码;
在执行到handler之前就会按照HandlerInterceptorList中的HandlerInterceptor的顺序去执行里面的方法
HandlerInterceptor中的方法
2.1.1 preHandle(预处理)
返回值为boolean → 返回值决定是否能继续流程
2.1.2 postHandle(后处理)
handler方法执行完响应的ModelAndView,如果响应的是json,那么这里的modelAndView就是null,可以对ModelAndView做额外的处理
2.1.3 afterCompletion
Exception是handler方法抛出的异常
handler方法抛出异常,仍有可能执行到afterCompletion
如果当前的HandlerInterceptor的preHandle方法返回值结果为true,一定可以执行到对应的afterCompletion
pom.xml(和上面一样)
HelloController
//@RestController("hello")//组件id
@Controller
@RequestMapping("hello")//窄化请求
public class HelloController {
//localhost:8080/hello/world
@RequestMapping("world")
@ResponseBody
public String hello(){//因为有@RestController,所以响应的是字符串而不是视图名
System.out.println("hello world");
return "helloworld";
}
//localhost:8080/hello/world2
@RequestMapping("world2")
public String hello2(){//因为有@RestController,所以响应的是字符串而不是视图名
System.out.println("hello world");
//return "helloworld"; //hello/helloworld
return "/helloworld.jsp"; //helloworld.jsp
}
}
CustomHandlerInterceptor
@Component
public class CustomHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (modelAndView != null){
modelAndView.setViewName("/postHandle.jsp");
}
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
application.xml
2.2HandlerInterceptor的作用范围
2.2.1 全局
2.2.2 局部
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cskaoyan"/>
<mvc:annotation-driven/>
<!--mvc:interceptors-->
<mvc:interceptors>
<!--
如果使用bean标签或ref标签的话,作用范围是全局
全局:DispatcherServlet作用范围下的全局
-->
<!--<ref bean="customHandlerInterceptor"/>-->
<!--<bean class="com.cskaoyan.interceptor.CustomHandlerInterceptor"/>-->
<mvc:interceptor>
<!--
/hello → hello这个请求
/hello* → helloxxx请求
/hello/* → hello/xxx请求
/hello/** → hello以及hello的所有多级请求 /hello/aaa/bbb/ccc
-->
<mvc:mapping path="/hello/**"/>
<ref bean="customHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
helloworld.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Hello world页面</h1>
</body>
</html>
postHand.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>之后处理了</h1>
</body>
</html>
web.xml
<?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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
结果:
3.案例三(many-handler-interceptor)
3.1多个interceptor的执行顺序
(1)如果其中的某个preHandle返回值为false,则中断流程;
(2)如果当前interceptor的preHandle返回值为true,一定可以执行到对应的afterCompletion
示例:
pom.xml(和案例2一样)
HelloController(和案例2一样)
HandlerInterceptor1
@Component
public class HandlerInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle1");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle1");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion1");
}
}
HandlerInterceptor2
@Component
public class HandlerInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle2");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion2");
}
}
HandlerInterceptor3
@Component
public class HandlerInterceptor3 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle3");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle3");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion3");
}
}
application.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cskaoyan"/>
<mvc:annotation-driven/>
<mvc:interceptors>
<!--顺序和书写顺序相关-->
<ref bean="handlerInterceptor1"/>
<ref bean="handlerInterceptor2"/>
<ref bean="handlerInterceptor3"/>
</mvc:interceptors>
</beans>
web.xml(和案例2一样)
结果:
4.案例四(handler-exception-resolver)
4.1 异常处理
4.1.1 HandlerExceptionResolver(全局处理)
只要异常抛出就进入到该处理器,通用
只需要注册到容器中,该组件就生效
pom.xml不变
HelloController
@Controller
public class HelloController {
@RequestMapping("hello/{username}")
public String hello(@PathVariable("username")String username) throws SensitiveWordException {
if ("dawa".equals(username)){
//int i = 1 / 0;
} else if ("erwa".equals(username) || "sanwa".equals(username)) {
throw new SensitiveWordException(username, "小金刚");
}
return "/hello.jsp";
}
}
CustomHandlerExceptionResolver
- 只需要增加@Component则生效(只需要注册到容器中)
- handler方法向上抛出异常,则会执行到resovleException方法
@Component
public class CustomHandlerExceptionResolver implements HandlerExceptionResolver {
/**
* @param handler → Handler方法
* @param exception → 向上抛出的异常对应
* @return ModelAndView → 最后处理后的ModelAndView
*/
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception exception) {
ModelAndView modelAndView = new ModelAndView("/WEB-INF/exception.jsp");
//根据抛出的异常不同,做个性化的处理 → 异常类型的不同
if (exception instanceof SensitiveWordException) {
SensitiveWordException sensitiveWordException = (SensitiveWordException) exception;
String word = sensitiveWordException.getWord();//从handler方法中抛出的异常对象封装的
String message = sensitiveWordException.getMessage();
modelAndView.setViewName("/WEB-INF/sensitive.jsp");
modelAndView.addObject("word", word);
modelAndView.addObject("messagez", message);
}
return modelAndView;
}
}
SensitiveWordException
public class SensitiveWordException extends Exception{
String word;
public SensitiveWordException(String word, String message) {
super(message);
this.word = word;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
}
application.xml
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
exception.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>正在维护,,,,,</h1>
</body>
</html>
sensitive.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1> ${word}举起手来,${messagez}已经被包围了</h1>
</body>
</html>
web.jsp
<?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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
结果:
5.案例五(exception-handler)
BaseRespVo
@Data
public class BaseRespVo<T> {
T data;
String message;
int errno;
public static BaseRespVo fail(String message){
BaseRespVo baseRespVo = new BaseRespVo();
baseRespVo.setMessage(message);
baseRespVo.setErrno(500);
return baseRespVo;
}
}
ExceptionController
异常和Handler之间的映射,灵活 → 个性化的处理
根据异常的类型不同直接映射到不同的方法上 → ModelAndView还是json
@RestControllerAdvice
@ControllerAdvice //使用该注解,才能够使用ExceptionHandler
//@RestControllerAdvice
public class ExceptionController {
//方法的写法和HandlerMethod写法一毛一样
@ExceptionHandler(ArithmeticException.class)
public String exception(ArithmeticException exception){ //ModelAndView
return "/WEB-INF/exception.jsp";
}
@ExceptionHandler(SensitiveWordException.class)
@ResponseBody
public BaseRespVo sensitive(SensitiveWordException exception){ //json
String message = exception.getMessage();
return BaseRespVo.fail(message);
}
}