<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionAttribute" value="ex"></property> <!-- 不指定的话value默认是exception -->
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error/authExceptionView</prop>
<!-- 指定YourCustomException异常发生时跳转 -->
<prop key="com.test.demo.exception.YourCustomException">error/customExceptionView</prop>
</props>
</property>
<property name="defaultErrorView" value="error/genericView"/><!-- 其他异常发生时跳转页面 -->
</bean>
package com.test.demo.exception;
import java.util.Date;
public class YourCustomException extends RuntimeException
{
private static final long serialVersionUID = 1L;
private Date date;
private String message;
public YourCustomException(Date date, String message) {
super();
this.date = date;
this.message = message;
}
public Date getDate() {
return date;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "YourCustomException [date=" + date + ", message=" + message + "]";
}
}
@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
@RequestMapping(value="/businessOne", method = RequestMethod.GET)
public String welcome(Model model)
{
throw new YourCustomException(new Date(), "我发生了一个业务错误");
}
}
采用的是servlet3.0,所以要手动开启使用el表达式(2.4以前的无需手动开始,2.5开始往后的要在页面手动开启isELIgnored=false)
error/customExceptionView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
${ex.message}
</body>
</html>
@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
@RequestMapping(value="/businessTwo", method = RequestMethod.GET)
public String welcome2(Model model)
{
throw new NullPointerException();
}
}
error/genericView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
发生了一些异常!!
</body>
</html>
遇到的问题:
1.前期以为使用el表达式要引用taglib,在页面使用<%@ taglib prefix="c" uri=http://java.sun.com/jsp/jstl/core %>,结果pom.xml中引入jstl配置
页面老是报The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application异常
以为是没有配置引用jstl jar,经检查后有配置。使用jd-gui打开jstl.jar中的tld文件发现标签引用中uri指定的路径不对,修正uri地址页面不报异常。
2.el表达式不能正常显示,显示为${ex.message}
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionAttribute" value="ex"></property> <!-- 不指定的话value默认是exception -->
<property name="exceptionMappings">
<props>
<prop key="java.lang.Throwable">error/authExceptionView</prop>
<!-- 指定YourCustomException异常发生时跳转 -->
<prop key="com.test.demo.exception.YourCustomException">error/customExceptionView</prop>
</props>
</property>
<property name="defaultErrorView" value="error/genericView"/><!-- 其他异常发生时跳转页面 -->
</bean>
package com.test.demo.exception;
import java.util.Date;
public class YourCustomException extends RuntimeException
{
private static final long serialVersionUID = 1L;
private Date date;
private String message;
public YourCustomException(Date date, String message) {
super();
this.date = date;
this.message = message;
}
public Date getDate() {
return date;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "YourCustomException [date=" + date + ", message=" + message + "]";
}
}
@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
@RequestMapping(value="/businessOne", method = RequestMethod.GET)
public String welcome(Model model)
{
throw new YourCustomException(new Date(), "我发生了一个业务错误");
}
}
采用的是servlet3.0,所以要手动开启使用el表达式(2.4以前的无需手动开始,2.5开始往后的要在页面手动开启isELIgnored=false)
error/customExceptionView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
${ex.message}
</body>
</html>
@Controller
@RequestMapping("/test-demo")
public class TestDemoController
{
@RequestMapping(value="/businessTwo", method = RequestMethod.GET)
public String welcome2(Model model)
{
throw new NullPointerException();
}
}
error/genericView.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<% page isELIgnored="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>error</title>
</head>
<body>
发生了一些异常!!
</body>
</html>
遇到的问题:
1.前期以为使用el表达式要引用taglib,在页面使用<%@ taglib prefix="c" uri=http://java.sun.com/jsp/jstl/core %>,结果pom.xml中引入jstl配置
页面老是报The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application异常
以为是没有配置引用jstl jar,经检查后有配置。使用jd-gui打开jstl.jar中的tld文件发现标签引用中uri指定的路径不对,修正uri地址页面不报异常。
2.el表达式不能正常显示,显示为${ex.message}
在页面手动开isELIgnored="false"设置,显示效果达到预期后,干掉了对taglib c的引用
参考:http://www.tuicool.com/articles/FbaMb2
http://www.open-open.com/lib/view/open1413639779887.html
http://blog.csdn.net/for_china2012/article/details/9307471