spring MVC中定义异常页面
来源:互联网 发布:php开发教程视频 编辑:程序博客网 时间:2024/06/04 19:25
http://www.cnblogs.com/rollenholt/archive/2012/12/25/2832731.html
如果我们在使用Spring MVC的过程中,想自定义异常页面的话,我们可以使用DispatcherServlet来指定异常页面,具体的做法很简单:
下面看我曾经的一个项目的spring配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?
xml
version="1.0" encoding="UTF-8" ?>
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 扫描web包,应用Spring的注解 -->
<
context:component-scan
base-package="com.xxx.training.spring.mvc"/>
<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
<
bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/views/"
p:suffix=".jsp"/>
<!--定义异常处理页面-->
<
bean
id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<
property
name="exceptionMappings">
<
props
>
<
prop
key="java.sql.SQLException">outException</
prop
>
<
prop
key="java.io.IOException">outException</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
上面的定义异常处理部分的解释为:只要发生了SQLException或者IOException异常,就会自动跳转到WEB-INF/views/outException.jsp页面。
一般情况下我们的outException.jsp页面的代码为:
1
2
3
4
5
6
7
8
9
10
11
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title>异常处理页面</title>
</head>
<body>
<% Exception ex = (Exception) request.getAttribute(
"Exception"
);%>
<H2>Exception:<%=ex.getMessage()%>
</H2>
</body>
</html>
当然你也可以修改样式,这个就看个人喜好了、
另外记得要在web.xml也使用类似下面的方式处理异常哦。:
1
2
3
4
5
6
7
8
9
<
error-page
>
<
error-code
>404</
error-code
>
<
location
>/WEB-INF/pages/404.jsp</
location
>
</
error-page
>
<
error-page
>
<
exception-type
>java.lang.Exception</
exception-type
>
<
location
>/WEB-INF/pages/exception.jsp</
location
>
</
error-page
>
因为这两个异常处理的维度是不一样的,简单说,spring的resolver是spring内部使用的,而web。xml里的是整个webapp共同使用的。
建议两个都配置上,
因为spring的resolver可以和spring结合的更紧密,可扩展的更多。
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 异常处理
- chrome插件--jsonview安装
- 多态(1)静态多态与动态多态
- springMvc 数据绑定,类型转换,数据校验 解析
- 浅谈Html5的优势和劣势
- HDOJ 2824 The Euler function (欧拉函数)
- spring MVC中定义异常页面
- c++无限输入求和,非数组
- 委托,lambda表达式和事件
- spring boot-jpa整合QueryDSL来简化复杂操作
- bzoj 1036: [ZJOI2008]树的统计Count
- LeetCode 38. Count and Say 字符串“阅读式”扩张
- 分组统计
- JAVA程序员必看的15本书
- Leetcode 68. Text Justification