Spring自定义错误页面不为/ error返回明确的映射

时间:2019-06-24 10:09:53

标签: java spring

我想像这样从spring中定制错误页面:

@Controller
public class Error implements ErrorController{

    @RequestMapping(value = "/error")
    public ModelAndView error(){

        return new ModelAndView("error");

    }

    @Override
    public String getErrorPath(){
        return "/error";
    }

}

ModelAndView("error")-错误是jsp中的WEB-INF/jsp/

当我尝试通过访问无效链接触发错误时:mydom.com/invalidlink未打印error.jsp

已插入此消息:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

null
There was an unexpected error (type=null, status=null).

如何自定义错误页面?

1 个答案:

答案 0 :(得分:1)

我将我的代码发布给您

@Controller
public class CustomErrorController implements ErrorController  
    @RequestMapping("/error") 
    public String handleError(HttpServletRequest request, Model model) { 
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

我的代码有效,您的代码与我的代码只有一个区别

public String handleError()

它是返回字符串和您的情况

public ModelAndView error()

返回ModelAndVIew .....