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的方法,一是经过错误码来配置,而是经过异常的类型来配置,分别举例以下:css

一.   经过错误码来配置error-pagehtml

Eg.java

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

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

 

二.   经过异常的类型配置error-pagespring

Eg.spring-mvc

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

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

相关文章
相关标签/搜索