1. Append in Web. xml
<Error-page> <Error-code>404</Error-code> < Location>/404</ Location> </Error-page> <Error-page> <Error-code>500</Error-code> < Location>/500</ Location> </Error-page>
* Note Slash/
2. Append a Controller
PackageIo.deolin.controller;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classErrorpagecontroller {Logger log= Logmanager.getlogger (Errorpagecontroller.class); @RequestMapping ("404") PublicString page404 () {Log.info ("HTTP-400 Error"); return"HTTP-404"; } @RequestMapping ("500") PublicString page500 () {Log.error (500); return"HTTP-500"; }}
* Note that the request URI remains consistent
You can test a URL that does not map to a request method and a static resource, such as
Http://localhost:8080/impossibleeeeeeeeeeeeeeeeeeeee
Then the browser's page content is the content of http-404.html.
Springmvc Specify 404, 500 error pages