我似乎遇到了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,但异常视图不会在客户端上呈现。 Spring配置是OK还是不需要web.xml中的条目?我错过了可能是处理程序未被调用的原因吗? 任何反馈都表示赞赏。 谢谢。
答案 0 :(得分:1)
我发现了问题 - 抛出异常的方法需要与处理程序方法在同一个控制器中。我在同一个控制器中移动了两个方法,一切正常。
答案 1 :(得分:0)
@ExceptionHandler( IllegalStateException.class )
public String handleException( IllegalStateException ex ){
return "exception_view";
}