全局异常处理

在spring applicationContext.xml文件中添加如下配置:
<div class="iteye-blog-content-contain" style="font-size: 14px">
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">    
         <property name="exceptionMappings">    
             <props>    
                 <prop key="java.lang.Exception">error</prop>    
                 <prop key="java.lang.Throwable">error</prop>    
             </props>    
         </property>    
         <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->    
         <property name="warnLogCategory" value="ERROR"></property>    
         <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->    
         <property name="defaultErrorView" value="error"></property>    
         <!-- 默认HTTP状态码 -->    
         <property name="defaultStatusCode" value="500"></property>    
    </bean>    
</div>
其它代码不许任何更改。
注:error就是对应异常的响应页面,路径要配置一致,此处配置的error是因为使用了velocity,缺省了路径和后缀名。以下贴上velocity配置

<div class="iteye-blog-content-contain" style="font-size: 14px">
  <!-- velocity config -->
<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
  <value>/WEB-INF/view</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="runtime.log">velocity_loger</prop>
<prop key="runtime.log.logsystem.class">org.apache.velocity.runtime.log.SimpleLog4JLogSystem
</prop>
<prop key="runtime.log.error.stacktrace">false</prop>
<prop key="runtime.log.warn.stacktrace">false</prop>
<prop key="runtime.log.info.stacktrace">false</prop>
<prop key="runtime.log.invalid.reference">false</prop>
</props>
</property>
</bean>

<!-- velocity view -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="dateToolAttribute">
<value>dateTool</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
<property name="contentType">
<value>text/html;charset=utf-8</value>
</property>
<property name="allowSessionOverride" value="true" />
<property name="allowRequestOverride" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="requestContextAttribute" value="rc" />
<property name="exposeRequestAttributes" value="true" />
<property name="layoutKey" value="layout"/>
        <property name="screenContentKey" value="screen_content"/>
<property name="layoutUrl" value="layout/empty.vm" />
<property name="toolboxConfigLocation" value="/WEB-INF/view/toolbox.xml" />
</bean>
</div>

猜你喜欢

转载自sky10198866.iteye.com/blog/2086070
今日推荐