如果我们在使用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中定义异常页面的更多相关文章

  1. spring MVC中的异常统一处理

    1.spring MVC中定义了一个标准的异常处理类SimpleMappingExceptionResolver 该类实现了接口HandlerExceptionResolver 2.看下SimpleM ...

  2. spring mvc 中自定义404页面在IE中无法显示favicon.ico问题的解决方法。

    此处用的是jsp,控制层用的是ModelAndView, 具体解决方法如下: @RequestMapping(value = "notfound", method = Reques ...

  3. Spring Mvc中Jsp也页面怎么会获取不到Controller中的数据

    ----------Controller ------- package com.test.mvc; import org.springframework.stereotype.Controller; ...

  4. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  5. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  6. Spring MVC中的HandlerMapping与HandlerAdapter

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  7. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  8. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  9. Spring MVC 快捷定义 ViewController

    WHY  :               为什么我们需要快捷定义 ViewController ? 在项目开发过程中,经常会涉及页面跳转问题,而且这个页面跳转没有任何业务逻辑过程,只是单纯的路由过程 ...

随机推荐

  1. BZOJ2329 HNOI2011 括号修复 splay+贪心

    找平衡树练习题的时候发现了这道神题,可以说这道题是近几年单考splay的巅峰之作了. 题目大意:给出括号序列,实现区间翻转,区间反转和区间更改.查询区间最少要用几次才能改成合法序列. 分析: 首先我们 ...

  2. maven compile启动报错

    ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-co ...

  3. 1833 深坑 TLE 求解

    题目描述: 大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 ...

  4. 【Python】 获取MP3信息replica

    replica 初衷是想要整理iphone中的音乐.IOS(我自己的手机还是IOS8.3,新版本的系统可能有变化了)自带的音乐软件中所有音乐文件都存放在/var/mobile/Media/iTunes ...

  5. 【Python】正则表达式re

    re 正则表达式(regular expression)这玩意儿多nb就不用说了,python用re模块来支持正则 首先是一些正则表达式的概念 1. 通配符 . 2. 多字符选择 [...] [abc ...

  6. c++ --> cin和cout输入输出格式

    cin和cout输入输出格式 Cout 输出 1>. bool型输出 cout << true <<" or " << false < ...

  7. 如何使用maven搭建web项目

    博客园注册了有二十多天了,还没有写过博客,今天就发一篇,也便于后面查找笔记. 我个人已经做了几年的java web开发了,由于所在的公司是业务型公司,用的都是一些老旧的稳定技术,很少接触到稍微新点的内 ...

  8. Hibernate学习笔记三 多表

    一对多|多对一 表中的表达 实体中的表达 实体代码: package com.yyb.domain; import java.util.HashSet; import java.util.Set; p ...

  9. RTMP规范协议

    本文参照rtmp协议英文版,进行简单的协议分析 1.什么是RTMP 关于 Adobe 的实时消息协议(Real Time Messaging Protocol,RTMP),是一种多媒体的复用和分组的应 ...

  10. JAVA多线程中start方法与run方法区别

    start()方法告诉jvm该线程准备运行,jvm通过调用任务的run()方法执行任务. 一个任务类必须实现Runnable接口,而任务必须从线程运行. 实现Runnable接口后必须重写run()方 ...