2019/4/22
问题描述
handler通过@Exception编写如下异常处理代码
@RequestMapping("testMyException2")
public String testMyException2(@RequestParam("i") Integer i) throws MyArrayOutofBoundsException {
if(i == 3) {
return "testResponseStatus";//跳转到某一个异常处理方法
}
return "success";
}
结果报错 404找不到testResponseStatus.jsp
解决思路
有代码可知,我这里是在发生该异常时跳转到某一个方法进行异常处理,但是浏览器报错却报错找不到啊jsp页面,说明这里testResponseStatus默认被转换成了testResponseStatus.jsp格式,通过查看springDispatcherServlet-servlet.xml,发现以下代码
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
这里通过配置自动给testResponseStatus加上了前缀和后缀,所以return “testResponseStatus”
变成了 return “views/testResponseStatus.jsp”,这时候testResponseStatus是一个方法,而不是一个jsp页面,故报错。
解决方法
这个时候需要取消前缀以及后缀(当然不是直接修改xml文件),而是return "testResponseStatus"修改为重定向的方式,代码如下:
@RequestMapping("testMyException2")
public String testMyException2(@RequestParam("i") Integer i) throws MyArrayOutofBoundsException {
if(i == 3) {
return "redirect:testResponseStatus";//跳转到某一个异常处理方法
}
return "success";
}