【问题标题】:@ExceptionHandler in SpringSpring中的@ExceptionHandler
【发布时间】:2012-10-27 11:10:31
【问题描述】:

在我的应用程序中,我在 web.xml 中配置了一个错误页面。我需要根据条件为特定控制器覆盖它。在这里,当条件变为真时,我需要重定向到特定的错误页面,否则应该呈现正常的错误页面。 这是代码。请帮帮我。

@Controller
public class Test{

 @ExceptionHandler(Exception.class)
 public ModelAndView generateException(HttpServletRequest httpServletRequest){
  if(condition) {
   return new ModelAndView("myError.jsp");
  } else {
   //should execute default error page.
  }
 }
}

【问题讨论】:

    标签: spring model-view-controller


    【解决方案1】:

    再次抛出异常,该异常将由 DefaultHandlerExceptionResolver 处理,以响应 web.xml 中定义的错误页面;它不会调用控制器的相同异常处理程序。

    @ExceptionHandler(Exception.class)
    public ModelAndView generateException(Exception ex) throws Exception{
        if(condition) {
            return new ModelAndView("myError.jsp");
        } else {
            //should execute default error page.
            throw ex;
        }   
    }
    

    【讨论】:

    • 谢谢vinay。这是完美的。
    • 如果您满意,您介意接受它作为答案吗? ;)
    猜你喜欢
    • 2015-11-14
    • 2017-12-05
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多