Я хочу в своем проекте реализовать то, что если допустим будет неправильно введен URl то должна выскочить JSP страница со словами- "ошибка 404 страница не найдена"
Я написал код но он неправильный. Надо чтобы просто выскакивала страница со словами об ошибке
WEB XML
<error-page>
<location>/errors</location>
</error-page>
ErrorController
@Controller
public class ErrorController {
@RequestMapping(value = "errors", method = RequestMethod.GET)
public ModelAndView renderErrorPage(HttpServletRequest httpRequest) {
ModelAndView errorPage = new ModelAndView("errorPage");
String errorMsg = "";
int httpErrorCode = getErrorCode(httpRequest);
switch (httpErrorCode) {
case 400: {
errorMsg = "Http Error Code: 400. Bad Request";
break;
}
case 401: {
errorMsg = "Http Error Code: 401. Unauthorized";
break;
}
case 404: {
errorMsg = "Http Error Code: 404. Resource not found";
break;
}
case 500: {
errorMsg = "Http Error Code: 500. Internal Server Error";
break;
}
}
errorPage.addObject("errorMsg", errorMsg);
return errorPage;
}
private int getErrorCode(HttpServletRequest httpRequest) {
return (Integer) httpRequest
.getAttribute("javax.servlet.error.status_code");
}
@RequestMapping(value = "500Error", method = RequestMethod.GET)
public void throwRuntimeException() {
throw new NullPointerException("Throwing a null pointer exception");
}
}
JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${errorMsg}</h1>
</body>
</html>
Как можно исправить код чтобы просто выскакивала вот такая страница
<%@ page contentType='text/html; charset=UTF-8' %>
<html>
<head>
<title>Страница не найдена</title>
</head>
<body>
<p>
Запрашиваемая страница не найдена
</p>
</table>
</body>
</html>