课程学习--SSM--第9讲--异常映射机制

思路

在这里插入图片描述

实现

1、XML

  1. 配置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>
  1. 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好方便啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

charliejohn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值