SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理

关山初度尘未洗,策马扬鞭再奋蹄!这篇文章主要讲述SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理相关的知识,希望能为你提供帮助。
SpringMVC的异常处理,SimpleMappingExceptionResolver只能简单的处理异常
当发生异常的时候,根据发生的异常类型跳转到指定的页面来显示异常信息
ExceptionController.java 处理器

package com.orange.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.orange.exception.NameException; import com.orange.exception.PasswordException; @Controller @RequestMapping("/exception") public class ExceptionController {@RequestMapping("/simple") public String doException(){int i = 3 / 0; return "/showException.jsp"; }}

 
defaultException.jsp 发生异常跳转的页面
< %@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> < %@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> < %@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> < % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html> < head> < meta http-equiv="Content-Type" content="text/html; charset=GBK"> < base href="https://www.songbingjia.com/android/< %=basePath %>"> < title> DefaultExceptionPage< /title> < /head> < body> ERROR! DefaultExceptionPage< br> message: < c:out value="https://www.songbingjia.com/android/${ex.message }"> < /c:out> < /body> < /html>

springMVC配置SimpleMappingExceptionResolver
< bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> < !-- 指定所有没有指定的异常,都跳转到该页面 --> < property name="defaultErrorView" value="https://www.songbingjia.com/defaultException.jsp" /> < !-- 跳转时携带异常对象 --> < property name="exceptionAttribute" value="https://www.songbingjia.com/android/ex"> < /property> < /bean>

【SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理】 

    推荐阅读