【发布时间】:2014-08-25 23:33:54
【问题描述】:
我最近开始使用 @ControllerAdvice 类来管理我的 Spring 项目中的异常。我目前的实现是这样的:
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e;
return new ModelAndView("error/5xx", "exception", e);
}
}
我的下一步应该是处理更多异常,但为此我正在考虑使用多个带有@ControllerAdvice 的类,其中一个用于http 状态代码。我的目标是让我的控制器处理表单提交的方法将用户重定向到我的一些自定义状态页面(每个组都有一个 - 1xx、2xx、3xx、4xx、5xx)。
这些方法的结构类似于:
@RequestMapping(value="cadastra")
@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")
public String cadastra(Model model) throws InstantiationException, IllegalAccessException {
model.addAttribute("command", this.entity.newInstance());
return "private/cadastrar";
}
任何人都可以告诉我这是否是一个好方法,并给出一些提示如何实现我的控制器方法来完成我想要的?
【问题讨论】:
-
看看here
标签: spring spring-mvc exception