Is there a way to handle all possible error codes while still passing the code to my .jsp? Here I have a single error-page passing 404 where it is added to the model. Rather than adding an error-page for every possible error code, is there a better way I can catch the error and pass the code to the controller/jsp file?
Controller
@RequestMapping(value="/error/{code}", method=RequestMethod.GET)
public String error(@PathVariable("code") String code, Model model)
{
model.addAttribute("code", code);
return "error";
}
web.xml
<error-page>
<error-code>404</error-code>
<location>/error/404</location>
</error-page>
You could register a generic exception resolver in Spring to catch all exceptions and transform into a rendering of your
error.jsp.Using a specialised RuntimeException thrown by your business logic, that has a
codemember:Or rely on existing RuntimeException instances with your
codein the exception message.Extract the
codeand/or message and set the HttpServletResponse status and ModelAndView accordingly.For example: