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服務器