【发布时间】:2018-03-23 17:39:43
【问题描述】:
我使用 ExceptionHandler 在控制器中捕获异常。
@ExceptionHandler(value = {Exception.class, RuntimeException.class})
public final ModelAndView globalExceptionHandler(final Exception exception) {
ModelAndView modelAndView = new ModelAndView("error/500");
modelAndView.addObject("tl_exception", errorSystem.processingError(exception));
return modelAndView;
}
但是,例如,如果在 jsp 文件中我想从空对象获取数据,那是异常而不是 cathcing。
我需要建议,如何在 jsp 文件中捕获异常?还是我只需要在控制器中捕获所有错误?
更新:
最好的解决方案是放在 web.xml uri 中以防出错。
<error-page>
<location>/error</location>
</error-page>
创建处理请求错误所需的控制器后:
@Controller
public final class ErrorController {
@RequestMapping(value = "/error")
public final ModelAndView globalErrorHandle(final HttpServletRequest request) {
String page = "error/500";
final String code = request.getAttribute("javax.servlet.error.status_code").toString();
if (null != code && !code.isEmpty()) {
final Integer statusCode = Integer.parseInt(code);
switch (statusCode) {
case 404 : page = "error/404";
case 403 : page = "error/403";
}
}
return new modelAndView(page);
}
}
【问题讨论】:
-
你在使用 JSP 标准标签库吗?
标签: java spring jsp exception exception-handling