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 ? 在项目开发过程中,经常会涉及页面跳转问题,而且这个页面跳转没有任何业务逻辑过程,只是单纯的路由过程 ...
随机推荐
- 自增ID算法snowflake(雪花)
在数据库主键设计上,比较常见的方法是采用自增ID(1开始,每次加1)和生成GUID.生成GUID的方式虽然简单,但是由于采用的是无意义的字符串,推测会在数据量增大时造成访问过慢,在基础互联网的系统设计 ...
- react实战第一步--搭建项目
使用 create-react-app 快速构建 React 开发环境 1.cnpm install -g create-react-app 2.create-react-app react2(rea ...
- 详解apply
先谈this 关于this的指向就不细说了,网上有无数博客以及you don't kown js这类书讲的已经很详细了这里就不炒冷饭了. 举几个简单的例子 function foo() { conso ...
- MongoDB3.6之Replica Set初步体验
Replica Set在国内叫做副本集,简单来说就是一份数据在多个地方存储. 1.为什么要用副本集,什么时候使用副本集? 有人说一份数据在多个地方存储占用了大量的额外空间,是一种浪 ...
- ACM搜索问题盘点
深度搜索:棋盘问题,详见http://poj.org/problem?id=1321 //#include<bits/stdc++.h> #include<cstdio> #i ...
- Cxf 自动生成客户端服务端代码
第一步: 下载apache-cxf安装包.并安装. 第二步: 配置cxf的环境变量. CXF_HOME = "CXF安装路径". 例如:F:\apache-cxf-2.1.2 在P ...
- Tomcat 开启Gzip压缩
原理简介 HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求服务器对应资源后,从服务器端将资源文件压缩,再输出到客户端,由客户端的浏览器负责解压缩并浏览.相对于普通的浏 ...
- logback读取src/test/resource下的配置文件
import java.io.File; import java.net.URISyntaxException; import java.util.Map; import java.util.Prop ...
- Repository个人实践
1.背景 最近,有空了,想着把之前一些乱七八糟的小项目给整理一下,尤其是涉及到Repository.UoW几处.为此,专门查阅了博客园中几个大神 关于Repository的实践,到最后都感觉依然莫衷一 ...
- python(练习实例)
Python 练习实例1 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 我的代码:python 3+ #2017-7-20 list_h = [1,2,3,4 ...