gpt4 book ai didi

spring - Spring中的@ExceptionHandler

转载 作者:行者123 更新时间:2023-12-02 09:51:42 24 4
gpt4 key购买 nike

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

@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.
}
}
}

最佳答案

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

@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;
}
}

关于spring - Spring中的@ExceptionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13268985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com