You can check whether I wrote the code in the web.xml. I want to display an error 404 page not found.

Web XML

<?xml version="1.0" encoding="windows-1251"?> <error-page> <location>/views</location> </error-page> 

Error Controller

 package fallen.java.schoolmaven.controller; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @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"); } } 

ErrorPage JSP

 <!DOCTYPE html> <%@ 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> 

File location

  • If it works, it means it is written correctly. If it does not work, it means that it is not written correctly. You'd better check your code for a start, if something doesn't work, then create a question from it, indicating what is wrong, and how it should be. - Farkhod Daniyarov
  • otherwise, your question will be closed: Questions asking for help with debugging (“why does this code not work?”) should include the desired behavior, a specific problem or error, and the minimum code for playing it right in the question. Questions without an explicit description of the problem are useless for other visitors. See How to create a minimal, self-contained and reproducible example. - Farkhod Daniyarov
  • look here: maybe it will help you, baeldung.com/custom-error-page-spring-mvc - Farkhod Daniyarov
  • If you did it directly, do you have different lines: <location>/errors</location> or does it not affect? - Farkhod Daniyarov
  • At first I did that tutorial, then I tried to change the location for my project - Steve Gates

1 answer 1

If I'm not mistaken, your web.xml should look like this.

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <error-page> <location>/views</location> </error-page> </web-app> 
  • @SteveGates glad that helped. The code was taken from github use the search for similar projects: github.com/gaussic/SpringMVCDemo/tree/master/src/main/webapp/… - Farkhod Daniyarov
  • @ SteveGates probably yes, there is a redirect, but I'm not sure, since this code itself has not been tested. - Farkhod Daniyarov
  • ok) thanks)))) - Steve Gates