Spring MVC: How to return custom 404 errorpages?

后端 未结 8 2134
遇见更好的自我
遇见更好的自我 2020-11-27 04:40

I\'m looking for a clean way to return customized 404 errorpages in Spring 4 when a requested resource was not found. Queries to different domain types should result in diff

8条回答
  •  死守一世寂寞
    2020-11-27 04:57

    You can map the error codes in web.xml like the following

        
            400
            /400
        
    
        
            404
            /404
        
    
        
            500
            /500
        
    

    Now you can create a controller to map the url's that are hit when any of these error is found.

    @Controller
    public class HTTPErrorHandler{
    
        String path = "/error";
    
        @RequestMapping(value="/404")
        public String error404(){
           // DO stuff here 
            return path+"/404";
        }
        }
    

    For full example see my tutorial about this

提交回复
热议问题