我似乎遇到了 Spring 的限制 - 我有一个简单的情况需要处理 - 我正在模拟服务方法的异常:
@RequestMapping( method = RequestMethod.POST )
public String register( @RequestParam( "mail" ) String mail ){
throw new IllegalStateException();
}
并尝试通过以下方式处理新请求:
@RequestMapping( value = "/exception_location" )
@ExceptionHandler( IllegalStateException.class )
public String handleException( IllegalStateException ex ){
return "exception_view";
}
我的 web.xml:
<error-page>
<exception-type>java.lang.IllegalStateException</exception-type>
<location>/exception_location</location>
</error-page>
发生的情况是,handleException 确实被触发,但异常 View 不会在客户端上呈现。 Spring 配置是否正确,或者 web.xml 中的条目不需要?我是否遗漏了一些可能导致处理程序没有被调用的原因? 如有任何反馈,我们将不胜感激。 谢谢。
最佳答案
我发现了问题 - 抛出异常的方法需要与处理程序方法位于同一 Controller 中。我将这两种方法移到同一个 Controller 中,一切正常。
关于java - Spring MVC - 使用 @ExceptionHandler 处理异常不会呈现新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5465436/