<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
导入servlet和jsp的jar依赖
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
dependency>
新建Moudle,添加web支持
确定导入了SpringMVC的依赖
配置web.xml注册DispatcherServlet(web.xml)
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextClassparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
编写SpringMVC的配置文件,名称springmvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
beans>
添加处理器映射器
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
添加处理器适配器
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
添加视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
编写Conteroller,
public class helloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
// ModerLAndView 模型和视图
ModelAndView mv = new ModelAndView();
// 封装对象,放在ModelAndView中, Model
mv.addObject("msg","HelloSpringMVC"!)
mv.setView("hello");
return mv;
}
public String value() {
return null;
}
public Class<? extends Annotation> annotationType() {
return null;
}
}
将自己的类交给Springioc容器,注册bean
<bean id="/hello" class="com.bdqn.controller.helloController"/>
要跳转到的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${massage}
实线表示MVC框架提供的技术,不需要开发者实现;虚线表示需要开发者实现
简单分析执行流程
DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心,用户发出请求 DispathcherServlet接受请求并拦截请求
HandlerMapping为处理器映射,DispatcherServlet调用HandlerMapping.HandlerMapping根据请求url查找Handler
HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为hello
HandlerExecution将解析后的信息传递给DispatcherServlet 如解析控制器映射等
HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler
Handler上有具体的Controller执行
Controller系那个具体的执行信息返回给HandlerAdpter,如ModeriAndView
HandlerAdapter调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名
DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。
视图解析器将解析的逻辑视图名传递给DispacthcherServlet。
DispatcherServlet根据视图解析器解析的视图结果,调用具体的试图。
最终视图呈现给用户
新建一个Moudle,添加web支持!
由于Maven可能存在资源过滤问题, 我们将配置完善
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
配置web.xml
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:Springmvc-Servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
在resource目录下添加springmvc-servlet.xml配置文件
<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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bdqn.controller"/>
<mvc:default-servlet-handler />
<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>
创建Controller
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping("/hello")
public String hello(Model model){
// 封装数据
model.addAttribute("massage","Hello,SpringMVCAnnotation!");
return "hello"; // 会被视图解析器处理
}
}
创建视图层
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${massage}
</body>
</html>
配置Tomcat运行
实现步骤
SpringMVC必须配置的三大件:
通常只需要手动配置视图解析器,
处理器映射器和处理器适配器,只需要开启注解驱动即可,
实现Controller接口(接口只有一个方法)
//实现该接口的类就可以获得控制器的功能
public class HelloController implements Controller {
// 返回ModelAndView
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("massage","Controller1");
modelAndView.setViewName("hello");
return modelAndView;
}
缺点:一个控制器只能写一个方法
使用注解@Controller
@Controller // 代表这个类会被Spring接管 类下所有的方法如果返回值是String 并且有具体的页面可以跳转,就会被视图解析器解析
public class HelloController2 {
// 映射访问路径
@RequestMapping("/t2")
public String test1(Model model){
model.addAttribute("mas","Controller2");
return "hello";
}
}
配置类
<context:component-scan base-package="com.bdqn.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
RestFul就是资源定位和资源操作的风格,不是标准也不是协议,只是风格。这个风格可以让软件更简洁,更有层次,更易于实现缓存等机制
// 原来:http://localhost:8080/add?a=1&b=2
// RestFul:http://localhost:8080/add/a/b
@GetMapping(value = "/{a}/{b}")
public String test3(@PathVariable Integer a,@PathVariable Integer b, Model model){
Integer c = a + b;
model.addAttribute("message",c);
return "admin/text";
}
可以脱离视图解析器【非人类】
把视图解析器注释掉
在Controller类中定义转发
return "forward:/WEB-INF/jsp/admin/text.jsp";
在Controller类中定义重定向
return "redirect:/index.jsp";
第一种:通过ModelAndView
// 返回ModelAndView
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("massage","Controller1");
modelAndView.setViewName("hello");
return modelAndView;
}
第二种:通过Model
@RequestMapping("/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int c = a + b;
model.addAttribute("mas",c);
return "hello";
}
第三种:通过ModelMap
对比
鸡汤:请使用80%的时间打好基础,剩下18%的时间研究框架,2%的时间去学点英文,框架的官方文档永远是最好的教程。
测试步骤:
在首页编写一个提交的表单
在Controller编写处理类
@RequestMapping("/e/t1")
public String test(String name, Model model) {
System.out.println(name);
model.addAttribute("massage",name);
return "test";
}
测试运行Tomcat
处理方法:
修改tomcat配置文件:设置编码!
<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改web.xml文件(需要重新启动Tomcat)
<filter>
<filter-name>encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
自定义过滤器【终极】
package com.bdqn.Encoding;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 解决get和post请求 全部乱码的过滤器
*/
public class GenericEncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//处理response的字符编码
HttpServletResponse myResponse=(HttpServletResponse) response;
myResponse.setContentType("text/html;charset=UTF-8");
// 转型为与协议相关对象
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 对request包装增强
HttpServletRequest myrequest = new MyRequest(httpServletRequest);
chain.doFilter(myrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//自定义request对象,HttpServletRequest的包装类
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
//是否编码的标记
private boolean hasEncode;
//定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰
public MyRequest(HttpServletRequest request) {
super(request);// super必须写
this.request = request;
}
// 对需要增强方法 进行覆盖
@Override
public Map getParameterMap() {
// 先获得请求方式
String method = request.getMethod();
if (method.equalsIgnoreCase("post")) {
// post请求
try {
// 处理post乱码
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else if (method.equalsIgnoreCase("get")) {
// get请求
Map<String, String[]> parameterMap = request.getParameterMap();
if (!hasEncode) { // 确保get手动编码逻辑只运行一次
for (String parameterName : parameterMap.keySet()) {
String[] values = parameterMap.get(parameterName);
if (values != null) {
for (int i = 0; i < values.length; i++) {
try {
// 处理get乱码
values[i] = new String(values[i]
.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
//取一个值
@Override
public String getParameter(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if (values == null) {
return null;
}
return values[0]; // 取回参数的第一个值
}
//取所有值
@Override
public String[] getParameterValues(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
<filter>
<filter-name>encodingfilter-name>
<filter-class>com.bdqn.Encoding.GenericEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
JSON键值对:
{"name": "QinJiang"}
{"age": "3"}
{"sex": "男"}
JSON 是 JavaScript 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。
var obj = {a: 'Hello', b: 'World'}; //这是一个对象,注意键名也是可以使用引号包裹的
var json = '{"a": "Hello", "b": "World"}'; //这是一个 JSON 字符串,本质是一个字符串
JSON 和 JavaScript 对象互转
JSON字符串转换为JavaScript 对象,使用 JSON.parse() 方法:
var obj = JSON.parse('{"a": "Hello", "b": "World"}');
//结果是 {a: 'Hello', b: 'World'}
JavaScript 对象转换为JSON字符串,使用 JSON.stringify() 方法:
var json = JSON.stringify({a: 'Hello', b: 'World'});
//结果是 '{"a": "Hello", "b": "World"}'
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Aarontitle>
head>
<body>
<script type="text/javascript">
//编写一个js的对象
var user = {
name:"Aaron",
age:3,
sex:"男"
};
//将js对象转换成json字符串
var str = JSON.stringify(user);
console.log(str);
//将json字符串转换为js对象
var user2 = JSON.parse(str);
console.log(user2.age,user2.name,user2.sex);
script>
body>
html>
导入依赖
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.11.3version>
dependency>
前端控制器(web,xml)
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:Springmvc-jsons.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>encodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
Spring配置文件
<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
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">
<context:component-scan base-package="com.bdqn.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
Controller
@Controller
public class jsonController {
@RequestMapping("/j1")
@ResponseBody // 不让它走视图解析器
public String json1() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
User user = new User(1,"陈赛飞",19);
return mapper.writeValueAsString(user);
}
}
测试(添加lib)
乱码解析!(解决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>
json返回时间
原生态java【推荐】
@RequestMapping("/j3")
public String json3() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Date date = new Date();
// 设置格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
return mapper.writeValueAsString(simpleDateFormat.format(date));
}
使用ObjectMapper
@RequestMapping("/j4")
public String json4() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// 关闭时间戳
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
// 自定义时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
// 注入
mapper.setDateFormat(simpleDateFormat);
Date date = new Date();
return mapper.writeValueAsString(simpleDateFormat.format(date));
}
创建json工具类
采用方法重写,传递一个参数(对象或集合)转成json格式,传入两个参数(时间)转成json格式
工具类
public class JsonUtils {
public static String getJson(Object o) {
return getJson(o,"yyyy-mm-dd HH:mm:ss");
}
public static String getJson(Object o,String dateFormat){
ObjectMapper mapper = new ObjectMapper();
// 关闭时间戳
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
// 自定义时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
// 注入
mapper.setDateFormat(simpleDateFormat);
try {
return mapper.writeValueAsString(o);
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
}
时间转换json格式时调用时
// 使用ObjectMapper获取时间并返回
@RequestMapping("/j4")
public String json4() throws JsonProcessingException {
Date date = new Date();
return JsonUtils.getJson(date);
}
}
对象或集合转换json格式时
@RequestMapping("/j1")
public String json1() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
User user = new User(1,"Aaron",19);
return mapper.writeValueAsString(user);
}
fastjson的pom的依赖!
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.60version>
dependency>
fastjsoon 三个主要的类
代码测试
导入依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.60version>
dependency>
编写代码 返回值用JSON.toJSONString(list);
@RequestMapping("/j5")
public String json5() throws JsonProcessingException {
User user1 = new User(1,"Aaron",19);
User user2 = new User(2,"Aaron",19);
User user3 = new User(3,"Aaron",19);
User user4 = new User(4,"Aaron",19);
User user5 = new User(5,"Aaron",19);
List<User> list = new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
list.add(user5);
String s = JSON.toJSONString(list);
return s;
}
导入依赖
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
在index.jsp页面编写表单(上传文件)
<form action="" enctype="multipart/form-data" method="post">
<input type="file" name="file"/>
<input type="submit">
form>
Spring配置文件中配置bean
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="10485760"/>
<property name="maxInMemorySize" value="40960"/>
bean>
编写Controller类
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
@Controller
public class FileController {
//@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
//批量上传CommonsMultipartFile则为数组即可
@RequestMapping("/upload")
public String fileUpload(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request) throws IOException {
//获取文件名 : file.getOriginalFilename();
String uploadFileName = file.getOriginalFilename();
//如果文件名为空,直接回到首页!
if ("".equals(uploadFileName)){
return "redirect:/index.jsp";
}
System.out.println("上传文件名 : "+uploadFileName);
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if (!realPath.exists()){
realPath.mkdir();
}
System.out.println("上传文件保存地址:"+realPath);
InputStream is = file.getInputStream(); //文件输入流
OutputStream os = new FileOutputStream(new File(realPath,uploadFileName)); //文件输出流
//读取写出
int len=0;
byte[] buffer = new byte[1024];
while ((len=is.read(buffer))!=-1){
os.write(buffer,0,len);
os.flush();
}
os.close();
is.close();
return "redirect:/index.jsp";
}
}
方式二:采用file.Transto 来保存上传的文件
/*
* 采用file.Transto 来保存上传的文件
*/
@RequestMapping("/upload2")
public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
File realPath = new File(path);
if (!realPath.exists()){
realPath.mkdir();
}
//上传文件地址
System.out.println("上传文件保存地址:"+realPath);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));
return "redirect:/index.jsp";
}
下载
@RequestMapping(value="/download")
public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception {
//要下载的图片地址
String path = request.getServletContext().getRealPath("/upload");
String fileName = "timg.jpg";
//1、设置response 响应头
response.reset(); //设置页面不缓存,清空buffer
response.setCharacterEncoding("UTF-8"); //字符编码
response.setContentType("multipart/form-data"); //二进制传输数据
//设置响应头
response.setHeader("Content-Disposition",
"attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
File file = new File(path, fileName);
//2、 读取文件--输入流
InputStream input = new FileInputStream(file);
//3、 写出文件--输出流
OutputStream out = response.getOutputStream();
byte[] buff = new byte[1024];
int index = 0;
//4、执行 写出操作
while ((index = input.read(buff)) != -1) {
out.write(buff, 0, index);
out.flush();
}
out.close();
input.close();
return null;
}
在Springmvc配置文件中配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.kuang.interceptor.MyInterceptor"/>
mvc:interceptor>
mvc:interceptors>
编写拦截器(实现HandlerInterceptor 接口)
public class MyInterceptor implements HandlerInterceptor {
//在请求处理的方法之前执行
//如果返回true执行下一个拦截器
//如果返回false就不执行下一个拦截器
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("------------处理前------------");
return true;
}
//在请求处理方法执行之后执行
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("------------处理后------------");
}
//在dispatcherServlet处理后执行,做清理工作.
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("------------清理------------");
}
}
步骤
编写自定义异常类,保存异常信息
public class MyExection extends Exception {
private String message; // 用来保存异常信息
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public MyExection() {
}
public MyExection(String message) {
this.message = message;
}
}
编写异常处理,实现HandlerExceptionResolver接口
public class MyExectionHandler implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o,
Exception e) {
// 判断异常
MyExection ex = null;
if(e instanceof MyExection){ // 判断发生异常是否和自定义异常类的异常相同,
ex = (MyExection)e; // 如果相同就保存到自定义异常中
}else{
ex = new MyExection("系统错误,请联系管理员"); // 不同就创建异常
}
ModelAndView mv = new ModelAndView();
mv.addObject("message",ex.getMessage());
mv.setViewName("error");
return mv;
}
}
配置异常处理器,跳转error页面
<bean class="com.bdqn.exection.MyExectionHandler" id="handler"/>