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="<%=basePath %>"> <title>DefaultExceptionPage</title> </head> <body> ERROR! DefaultExceptionPage<br> message: <c:out value="${ex.message }"></c:out> </body> </html>
springMVC配置SimpleMappingExceptionResolver
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- 指定所有没有指定的异常,都跳转到该页面 --> <property name="defaultErrorView" value="/defaultException.jsp" /> <!-- 跳转时携带异常对象 --> <property name="exceptionAttribute" value="ex"></property> </bean>
时间: 2024-10-21 21:15:13