使用 Spring DispatcherServlet 自定义 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1196569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:54:56  来源:igfitidea点击:

Custom 404 using Spring DispatcherServlet

springannotationshttp-status-code-404web.xml

提问by njdeveloper

I've set up web.xml as below. I also have an annotation-based controller, which takes in any URL pattern and then goes to the corresponding jsp (I've set that up in the -servlet.xml). However, If I go to a page that ends in .html (and whose jsp doesn't exist), I don't see the custom 404 page (and see the below error in the log). Any page that doesn't end in .html, I can see the custom 404 page.

我已经设置了 web.xml 如下。我还有一个基于注解的控制器,它接受任何 URL 模式,然后转到相应的 jsp(我已经在 -servlet.xml 中设置了它)。但是,如果我转到以 .html 结尾的页面(并且其 jsp 不存在),我将看不到自定义 404 页面(并在日志中看到以下错误)。任何不以 .html 结尾的页面,我都可以看到自定义 404 页面。

How can I configure to have a custom 404 page for any page that goes through the DispatcherServlet?

如何为通过 DispatcherServlet 的任何页面配置自定义 404 页面?

Also want to add that if I set my error page to a static page (ie. error.htm) it works, but if I change it to a jsp (ie. error.jsp), I get the IllegalStateException. Any help would be appreciated.

还想补充一点,如果我将错误页面设置为静态页面(即 error.htm),它可以工作,但如果我将其更改为 jsp(即 error.jsp),则会出现 IllegalStateException。任何帮助,将不胜感激。

log error

记录错误

Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:606)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)

controller

控制器

@RequestMapping(value = {"/**"})

public ModelAndView test() {

    ModelAndView modelAndView = new ModelAndView();

    return modelAndView;
}

web.xml

网页.xml

<servlet>
 <servlet-name>my_servlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

...

...

<servlet-mapping>
    <servlet-name>my_servlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

...

...

<error-page>
    <error-code>404</error-code>
    <location>/error.html</location>
</error-page>

回答by Rob Beardow

One option is to map all your error pages through your dispatcher servlet.

一种选择是通过调度程序 servlet 映射所有错误页面。

Create a new HTTP error controller:

创建一个新的 HTTP 错误控制器:


@Controller
public class HTTPErrorController {

    @RequestMapping(value="/errors/404.html")
    public String handle404() {
        return "errorPageTemplate";
    }

    @RequestMapping(value="/errors/403.html")
    ...

}

Map the error pages in web.xml

映射 web.xml 中的错误页面

<error-page>
    <error-code>404</error-code>
    <location>/errors/404.html</location>
</error-page>