在springmvc.xml中定义全局的异常处理

前端之家收集整理的这篇文章主要介绍了在springmvc.xml中定义全局的异常处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 在Controller类的内部方法上使用@ExceptionHandler,则此类的方法抛出未处理的异常时,回到此方法上处理。
  2. @ExceptionHandler可以指定异常的类型,会自动进行匹配
  3. 如果需要全局配置,可以在类上使用@ControllerAdvice
 <bean class="org.springframework.web.serlect.handler.SimpleMappingExceeptionResolver">
     
     <!-- 为所有的异常定义默认的异常处理页面,exceptionMappings未定义的异常使用本默认配置 -->
	<property name="defaultErrerView" value="errer"></property>
     <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
	<property name="exceptionAttribute" value="ex"></property>
     <!--
    定义需要特殊处理的异常,用类名或完全路径名作为key,异常页文件名作为值,
    将不同的异常映射到不同的页面上。
 -->
	<property name="exceptionMappings">
		<props>
			<prop key="IOException">errer/ioexp</prop>
			<prop key="org.springframework.jdbc.BadsqlGrammarException">errer/sqlexp</prop>
			<prop key="org.apache.shiro.authz.UnauthorizedException">errer/403</prop>
		</props>
	</property>
</bean>

猜你在找的SpringMVC相关文章