spring MVC中定义异常页面-定义异常问题处理页面

第一种方法、在spring-servlet.xml定义

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- DispatcherServlet Context: defines this servlet‘s request-processing infrastructure -->

	<!--注解驱动 Enables the Spring MVC @Controller programming model -->

	<mvc:annotation-driven>
	  <!--<mvc:argument-resolvers>  
          <bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
      </mvc:argument-resolvers>-->
	</mvc:annotation-driven>
		<!-- 扫描器 -->
    <context:component-scan base-package="me" />
	 <mvc:resources mapping="/shakearoud.html" location="/shakearoud.html" />  
     <!---配置拦截器不拦截的静态资源
	 <mvc:resources mapping="/img/**" location="/img/" />  
	 <mvc:resources mapping="/fonts/**" location="/fonts/" /> 
     <mvc:resources mapping="/js/**" location="/js/" />  
     <mvc:resources mapping="/css/**" location="/css/" />--> 
	<!--添加spring mvc intercepters
    <mvc:interceptors>
	    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	 <mvc:interceptor>
	    <mvc:mapping path="/"/>
        <bean class="me.chanjar.weixin.cp.Interceptor.MyHandlerInterceptor">
	       <property name="openingTime" value="15"/>
           <property name="closingTime" value="26"/>
        </bean>
	 </mvc:interceptor>
    </mvc:interceptors>-->
     <mvc:interceptors>
	    <bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
        <bean class="me.chanjar.weixin.cp.Interceptor.MyHandlerInterceptor">
		   <property name="openingTime" value="1"/>
           <property name="closingTime" value="26"/>
		</bean>
     </mvc:interceptors>
	 <!---配置拦截器-->
	<!-- 配置视图解析器。Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- Spring分段文件上传所必须的 ,用于检查请求中是否包含multipart  
    see: http://www.html.org.cn/books/springReference/ch13s08.html  
    -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    </bean> 
	<!--定义异常处理页面-->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.web.HttpSessionRequiredException">Error</prop>
                <prop key="java.io.IOException">outException</prop>
            </props>
        </property>
    </bean>
	<import resource="classpath:applicationContext.xml"/>
</beans>

第二种方法、web.xml里面定义处理页面

我知道在web.xml中有两种配置error-page的方法,一是通过错误码来配置,而是通过异常的类型来配置,分别举例如下:

一.   通过错误码来配置error-page

Eg.

<error-page> 
        <error-code>500</error-code> 
        <location>/error.jsp</location> 
 </error-page>

上面配置了当系统发生500错误(即服务器内部错误)时,跳转到错误处理页面error.jsp。

 

二.   通过异常的类型配置error-page

Eg.

<error-page> 
        <exception-type>java.lang.NullException</exception-type> 
        <location>/error.jsp</location> 
   </error-page>

上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp

时间: 2024-07-31 18:09:22

spring MVC中定义异常页面-定义异常问题处理页面的相关文章

Spring MVC中基于注解的 Controller

终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响应请求.实际上,ControllerClassNameHandlerMapping, MultiActionController 和选择恰当的 methodNameResolver(如 InternalPathMethodNameResolver) 就已经可以在很大程度上帮助我们省去不少的 XML 配置,谁让

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

原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响应请求.实际上,ControllerClassNameHandlerMapping, MultiActionController 和选择恰当的 methodNameResolver(如 InternalPathMetho

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

  终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响应请求.实际上,ControllerClassNameHandlerMapping, MultiActionController 和选择恰当的 methodNameResolver(如 InternalPathMethodNameResolver) 就已经可以在很大程度上帮助我们省去不少的 XML 配置,

Http请求中Content-Type讲解以及在Spring MVC中的应用

引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在Spring MVC中如何使用它们来映射请求信息. 1.  Content-Type MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息. [html] vie

Spring MVC中数据绑定之日期类型

数据绑定应该算是Spring MVC的特点之一吧~简单易用且功能强大,极大地简化了我们编程人员对于用户输入数据的接收及转换. 早先版本的Spring中的数据绑定完全都是基于PropertyEditor的.而Spring3中引入了Converter,用来替代PropertyEditor完成类型转换. 那么我们也依照这个顺序,先来讲讲基于传统的PropertyEditor来实现日期类型的数据绑定. 在Spring MVC中,我们可以通过WebDataBinder来注册自定义的PropertyEdit

Spring MVC中,事务是否可以加在Controller层

一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找到有力的论据来支持我这个想法,搞得我一度认为事务只能加在Service层,直到我读过spring官方文档并实践之后,我知道我的想法是对的. 在spring-framework-reference.pdf文档中有这样一段话: <tx:annotation-driven/> only looks fo

Spring Session在Spring MVC中的使用.md

Web项目会通过Session进行会话保持,Session是保存在服务器内存中: 现在为了提高站点的性能和稳定性,将Web项目发布到多个服务器,通过代理如Nginx或F5做负载均衡: 由于负载均衡正常配置,会对客户端的请求随机转发到某一个服务器,这会导致Session丢失: 解决方案:一种是可将代理如Nginx或F5配置为高可用,即用户访问时,在同一会话期间内,只往一台服务器转发:另一种是引入Spring Session,对Session进行持久化.统一管理: Spring Session对Se

在Spring MVC中使用Apache Shiro安全框架

我们在这里将对一个集成了Spring MVC+Hibernate+Apache Shiro的项目进行了一个简单说明.这个项目将展示如何在Spring MVC 中使用Apache Shiro来构建我们的安全框架. [TOC] 阅读文章前,您需要做以下准备: Maven 3环境Mysql-5.6+JDK1.7+git环境git.oschina.net帐号Apache Tomcat 7+您熟练掌握的编辑工具,推荐使用InterlliJ IDEA 14+开始项目地址git.oschina.net 项目地

详解Http请求中Content-Type讲解以及在Spring MVC中的应用

详解Http请求中Content-Type讲解以及在Spring MVC中的应用 引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在spring MVC中如何使用它们来映射请求信息. 1.  Content-Type MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型,在Http协议消息头中,

Spring MVC中处理静态资源的多种方法

处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中的DispatcherServlet吗?它是Spring MVC中的前置控制器,若配置的拦截路径为“/”,那么所有的请求都将被它拦截.对静态资源的访问也属于一个请求,那么也会被它拦截,然后进入它的匹配流 程,我们知道它是根据HandlerMapping的配置来匹配的.而对于静态资源来说,默认的Spr