思路
实现
1、XML
- 配置mvc
<!-- 配置基于XML的异常映射 -->
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 配置异常类型和具体视图页面的对应关系 -->
<property name="exceptionMappings">
<props>
<!-- key属性指定异常全类名 -->
<!-- 标签体中写对应的视图(这个值要拼前后缀得到具体路径) -->
<prop key="java.lang.Exception">system-error</prop>
</props>
</property>
</bean>
- system-error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>我养你@</h1>
${requestScope.exception.message}
</body>
</html>
2、注解
2.1 编写工具类,判断请求类型。
- 加入依赖
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.crowd</groupId>
<artifactId>atcrowdfunding05-common-util</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 引入Servlet容器中相关依赖 -->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
- 通过请求头判断是否为json
编写工具类
package com.atguigu.crowd.util;
import javax.servlet.http.HttpServletRequest;
public class CrowdUtil {
public static boolean judgeRequestType(HttpServletRequest request) {
String acceptHeader = request.getHeader("Accept");
String xRequestHeader = request.getHeader("X-Requested-With");
return (acceptHeader!=null&&acceptHeader.contains("application/json"))
||
(xRequestHeader != null&&xRequestHeader.equals("XMLHttpRequest"));
}
}
基于注解的异常处理机制
package com.atguigu.crowd.mvc.config;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.ResultEntity;
import com.google.gson.Gson;
// @ControllerAdvice表示当前类是一个基于注解的异常处理器类
@ControllerAdvice
public class CrowdExceptionResolver {
@ExceptionHandler(value = ArithmeticException.class)
private ModelAndView resolveMathResolve(
ArithmeticException exception,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
String viewName = "helloNull";
return commonResolve(viewName, exception, request, response);
}
// @ExceptionHandler将一个具体的异常类型和一个方法关联起来,例如:406、空指针
@ExceptionHandler(value = NullPointerException.class)
private ModelAndView commonResolve(
NullPointerException exception,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
String viewName = "system-error";
return commonResolve(viewName, exception, request, response);
}
private ModelAndView commonResolve(
String viewName,
Exception exception,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 1.判断当前请求类型
boolean judgeResult = CrowdUtil.judgeRequestType(request);
// 2.如果是Ajax请求
if(judgeResult) {
// 3.创建ResultEntity对象
ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());
// 4.创建Gson对象
Gson gson = new Gson();
// 5.将ResultEntity对象转换为JSON字符串
String json = gson.toJson(resultEntity);
// 6.将JSON字符串作为响应体返回给浏览器
response.getWriter().write(json);
// 7.由于上面已经通过原生的response对象返回了响应,所以不提供ModelAndView对象
return null;
}
// 8.如果不是Ajax请求则创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
// 9.将Exception对象存入模型
modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION,exception);
// 10.设置对应的视图名称
modelAndView.setViewName(viewName);
// 11.返回modelAndView对象
return modelAndView;
}
}
小结
xml好方便啊