我们在写代码的时候经常有运行时异常,这种异常是在编译期并不能被编译器标注出来的,而是在运行后出现异常信息。当我们写表现层时我们不对运行时异常做一定的处理,就会导致如果功能有异常,那么返回异常信息到用户页面会非常的不友好。如下:
异常信息密密麻麻导致页面非常的不好看,那我们需要对异常信息做一定的处理。那怎么对异常进行处理才能得到友好点的页面呢?SpringMVC为我们提供了HandlerException这个父类,我们需要编写异常类,以及异常处理类,最后通知SpringMVC有异常处理类,让它将异常处理类变为异常处理器。那么我们来实现一下吧。步骤如下:
①编写异常类
②编写异常处理器类
③通知SpringMVC
环境搭建
Maven配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version> <scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
</dependencies>
web.xml
<!--前端控制器-->
<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:SpringMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--静态资源不拦截-->
<mvc:resources location="/js/" mapping="/js/**/"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**/"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**/"></mvc:resources>
<!--开启mvc注解-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
</head>
<body>
<h3>异常处理</h3>
<a href="user/testException">异常处理</a>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
</head>
<body>
<h3>执行成功</h3>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<head>
</head>
<body>
${errMsg}
</body>
</html>
编写自定义异常类
package com.exception;
//自定义异常类
public class PrintException extends Exception{
//存储提示信息
private String message;
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public PrintException(String message) {
this.message = message;
}
}
编写异常处理器类
package com.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//异常处理器
public class PrintExceptionResolver implements HandlerExceptionResolver {
//处理异常业务逻辑
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
//获取异常对象
PrintException printException = null;
if(e instanceof PrintException){
printException = (PrintException)e;
}else{
e = new PrintException("系统正在维护");
}
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("errMsg" , e.getMessage());
modelAndView.setViewName("error");
return modelAndView;
}
}
告诉SpringMVC有异常处理器,则我们打开SpringMVC.xml
<!--异常处理器-->
<bean id="printExceptionResolver" class="com.itheima.exception.PrintExceptionResolver"></bean>
将异常处理器使用容器注入的方式,告诉SpringMVC我们的异常处理器在哪即可。
当我们方法出现异常,以前是直接将异常信息显示在页面上,而现在我们编写一个具有异常的testException方法
package com.controller;
import com.exception.PrintException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class userController {
@RequestMapping("/testException")
public String testException() throws PrintException{
System.out.println("testException方法执行了");
try {
//模拟异常
int a = 10/0;
} catch (Exception e) {
//打印异常信息
e.printStackTrace();
//抛出自定义异常信息
throw new PrintException("出现错误");
}
return "success";
}
}
可以看到里面有分母不能为0的异常。那么我们运行看看页面会是什么样?
控制台报错,如果没有异常处理器则是将这些异常显示在用户的返回页面中,而我们则是调用了error.jsp这个错误页面。error.jsp错误页面则是将我们想要表达的错误信息显示给用户看: