SpringMVC---异常处理SimpleMappingExceptionResolver

一:SimpleMappingExceptionResolver

  • 如果希望对所有映射统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常。

二:实现

1.创建测试方法

@RequestMapping("testSimpleMappingExceptionResolver")
	public String testSimpleMappingExceptionResolver(@RequestParam("i") int i) {
		String [] arr=new String[10];
		System.out.println(arr[i]);
		return "success";
	}

2.配置springMVC的xml文件

<!-- 配置SimpleMappingExceptionResolver -->
	<bean id="simpleMappingExceptionResolver" 
	class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<!-- 定义异常对象的属性名,可在页面上根据属性名取异常对象,默认是exception -->
		<property name="exceptionAttribute" value="ex"></property>
		<property name="exceptionMappings">
			<props>	
				<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
			</props>
		</property>
	</bean>

3.测试页面

<a href="testSimpleMappingExceptionResolver?i=10">test SimpleMappingExceptionResolver</a>
		  	<br>
发布了64 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39093474/article/details/103902474