如果我们在使用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. python数据库连接池设计

    一.背景: 传统访问资源,一般分为一下几个步骤: 1.实例数据驱动对象与链接资源.2.实例操作资源游标.3.获取资源.4.关闭链接资源. 根据以上步骤,我们可以很简单使用这个原始方法来访问资源为我们业 ...

  2. LAMP基础

    前言:上一篇博文,说到了URL.http的协议.事务以及私有https的实现.此次 一. 概念: LAMP: a:apache m:mariadb,mysql p:php,perl,python 二. ...

  3. node.js-v6新版安装过程

    1.Node.js简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用 ...

  4. Unity3D 心跳检测

    在B/S结构的项目开发的过程当中 在服务端与客户端正常的通信之外 服务端通常还需要知道客户端是否还处于连接状态 或者客户端也需要知道服务端是否还处在开启状态 大白话说完了,听一下比较正统的解释吧(摘自 ...

  5. python 全栈开发,Day3(正式)

    一.基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余  + - * / ** %...2.bool 布尔值 判断真假以及作为条件变量3. ...

  6. selenium webdriver 使用Chrome 浏览器

    首先需要有ChromeDriver驱动来协助.ChromeDriver是实现WebDriver有线协议的一个单独的服务.ChromeDriver通过chrome的自动代理框架控制浏览器,ChromeD ...

  7. null和undefined的异同

    相同点: 都表示值得空缺,二者往往可以互换,用“==”相等运算符判断两个是相等的,要用“===”判断. 在希望值是布尔类型的地方,他们的值都是假值,和“false”类似. 都不包含属性和方法. 使用& ...

  8. Runtime源码解析(JDK1.8)

    package java.lang; import sun.reflect.CallerSensitive; import sun.reflect.Reflection; import java.io ...

  9. Gauge----自动化测试工具--使用

    开始吧 1 下载安装gauge(根据官网教程 http://getgauge.io/documentation/user/current/)测试:gauge -v step01 磁盘上新建一个空目录- ...

  10. 收集nodejs经典组件:

    mysql功能简介:mysql- node.js平台mysql驱动,支持事务.连接池.集群.sql注入检测.多做参数传递写法等特性.主页地址:https://github.com/felixge/no ...