spring MVC中定义异常页面
如果我们在使用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结合的更紧密,可扩展的更多。
spring MVC中定义异常页面的更多相关文章
- spring MVC中的异常统一处理
1.spring MVC中定义了一个标准的异常处理类SimpleMappingExceptionResolver 该类实现了接口HandlerExceptionResolver 2.看下SimpleM ...
- spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。
此处用的是jsp,控制层用的是ModelAndView, 具体解决方法如下: @RequestMapping(value = "notfound", method = Reques ...
- Spring Mvc中Jsp也页面怎么会获取不到Controller中的数据
----------Controller ------- package com.test.mvc; import org.springframework.stereotype.Controller; ...
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring MVC中的HandlerMapping与HandlerAdapter
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Spring MVC 中的基于注解的 Controller(转载)
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...
- spring mvc中使用freemark的一点心得
参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...
- Spring MVC 快捷定义 ViewController
WHY : 为什么我们需要快捷定义 ViewController ? 在项目开发过程中,经常会涉及页面跳转问题,而且这个页面跳转没有任何业务逻辑过程,只是单纯的路由过程 ...
随机推荐
- Win10_x86_x64PE维护光盘——我用过最好用的PE
先感谢hongxj和fish2006两位大大提供的PE. 先放出所有工具的下载地址: hongxj的PE:https://yunpan.cn/crAw6HS6ar9ck 访问密码 4a4e fish ...
- 【Unity与23种设计模式】迭代器模式(Iterator)
GoF中定义: "在不知道集合内部细节的情况下,提供一个按序方法存取一个对象集合体的每一个单元." 迭代器模式由于经常使用到 已经被现代程序设计语言纳为标准语句或收录到标准函数库中 ...
- Online Judge(OJ)搭建——5、配置
Spring 配置一些本地类,还有 HTML form 提交文件的解析器. package per.piers.onlineJudge.config; import org.springframewo ...
- 并查集(Java实现)
(最好在电脑下浏览本篇博客...手机上看代码不方便) 当时学的时候看的一本印度的数据结构书(好像是..有点忘了..反正跟同学们看的都不一样...)...里面把本文提到的所有情况都提到了,我这里只是重复 ...
- JQ 判断 浏览器打开的设备类型
<script> $(document).ready(function(){ var ua = navigator.userAgent; var ipad = ua.match(/(iPa ...
- MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: "XX"
<select id="sltTreatment" resultType="com.vitaminmd.sunny.core.bo.Treatment"& ...
- Ubuntu 14.04下Hadoop2.4.1集群安装配置教程
一.环境 系统: Ubuntu 14.04 64bit Hadoop版本: hadoop 2.4.1 (stable) JDK版本: OpenJDK 7 台作为Master,另3台作为Slave. 所 ...
- svn 发布脚本整合
svn提交时出现(413 Request Entity Too Large)错误解决方法 在nginx的server配置中增加 client_max_body_size 100M; linux多实例a ...
- Nginx 开启gzip 压缩,实现基于域名的虚拟主机。
一:gzip(GNU-ZIP)是一种压缩技术. 经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多. gzip 的压缩页面需要浏览器和服务器双方都支持,实际上 ...
- c++ --> #define中的三个特殊符号:#,##,#@
#define中的三个特殊符号:#,##,#@ 看下面三个define宏定义: #define Conn(x,y) x##y #define ToChar(x) #@x #define ToStrin ...