Spring MVC自定義錯誤頁面
阿新 • • 發佈:2019-02-18
在web.xml中新增:
<error-page>
<location>/error</location>
</error-page>
新增控制器:
@Controller public class ErrorController { @RequestMapping(path = "/error", produces = MediaType.APPLICATION_JSON_VALUE) public ModelAndView handle(HttpServletRequest request) { System.out.println("ERROR"); ModelAndView modelAndView = new ModelAndView("error"); Map<String, Object> map = new HashMap<String, Object>(); map.put("status", request.getAttribute("javax.servlet.error.status_code")); map.put("reason", request.getAttribute("javax.servlet.error.message")); modelAndView.addObject("map", map); return modelAndView; } }
檢視:
<html>
<head>
<title>Title</title>
</head>
<body>
${map}
</body>
</html>