spring mvc 异常处理
来源:互联网 发布:2016网络射击游戏大全 编辑:程序博客网 时间:2024/06/04 17:44
<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
0 0
- Spring MVC异常处理
- spring mvc处理异常
- spring mvc 异常处理
- Spring MVC异常处理
- spring mvc 异常处理
- spring mvc异常处理
- spring mvc 异常 处理
- Spring MVC异常处理
- Spring MVC 异常处理
- spring mvc 异常处理
- Spring MVC 异常处理
- Spring MVC处理异常
- spring mvc异常处理
- Spring MVC 异常处理
- spring mvc 处理异常
- spring mvc 的异常处理
- spring mvc的异常处理
- Spring mvc 全局异常处理
- 简单自定义View的实现(电子钟表)
- iOS开发 之 不要告诉我你真的懂isEqual与hash!
- docker安装
- 第13章 更多的结构化命令
- 烘焙
- spring mvc 异常处理
- ContextUtils.java
- express + node.js 入门(2)
- C程序设计 例题5.9 判断一个大于3的数是否为素数
- 解决CentOS(6和7版本),/etc/sysconfig/下没有iptables的问题
- 轻松使用Nginx搭建web服务器
- 如何在Elasticsearch中安装中文分词器(IK+pinyin)
- java windows服务器 实现文档在线预览
- ECharts属性设置