我正在使用spring MVC 3.2.4。我有从业务层抛出的业务异常,并希望在同一视图中显示错误消息,而不是在捕获后转发到错误页面。我知道我可以在每个处理程序方法中放置try-catch,然后返回到同一个视图。但我真的想在Spring MVC中实现这个目标。我试过@ExceptionHandler和@ControllerAdvice,似乎没有用。 这是我想要做的,
@SomeExceptionHandlerAnnation(BusinessException.class)??
public String handleBusinessException(Model model,
BusinessException ex)
{
//convertExceptionToMessage();
//add message to model
return userView;//stay in the same view
}
问题是如何获得相同视图的模型。看来Spring MVC异常处理程序并不支持它。任何的想法?提前谢谢。
答案 0 :(得分:0)
试试这个:
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handleAllExceptions(Exception e) {
--do something here
return new ModelAndView();
}
}