自404使用Spring的DispatcherServlet [英] Custom 404 using Spring DispatcherServlet
问题描述
我如下设置web.xml中。我也有一个基于注解控制器,它接受任何URL模式,然后进入到相应的jsp(我有设定在-servlet.xml后缀)。但是,如果我去,在结束的.html页面(而其JSP不存在),我没有看到自定义404错误页面(以及在日志中看到下面的错误)。不以html结束任何页面上,我可以看到自定义404错误页面。
我如何可以配置有一个自定义404页,通过DispatcherServlet的推移任何页面?
另外要补充一点,如果我把我的错误页面静态页面(即error.htm)它的工作原理,但如果我把它变成一个jsp(即error.jsp文件),我得到的IllegalStateException异常。任何帮助将是AP preciated。
记录错误
产生的原因:java.lang.IllegalStateException:的getOutputStream()已经呼吁该响应
在org.apache.catalina.connector.Response.getWriter(Response.java:606)
在org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
在org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
控制器
@RequestMapping(价值= {/ **})公共ModelAndView的测试(){ ModelAndView的ModelAndView的=新的ModelAndView(); 返回的ModelAndView;
}
的web.xml
<&servlet的GT;
< servlet的名称>&my_servlet LT; / servlet的名称>
<的servlet类> org.springframework.web.servlet.DispatcherServlet< / servlet的类>
< / servlet的>
...
< Servlet映射>
< servlet的名称>&my_servlet LT; / servlet的名称>
< URL模式> * HTML和LT; / URL模式>
< / Servlet映射>
...
<错误页>
<无差错code取代; 404 LT; /无差错code>
&LT;地点&gt; /error.html< /地点&gt;
&LT; /错误页&GT;
一个选项是通过你的调度servlet来你所有的错误页面映射。
创建新的HTTP错误控制器:
@Controller
公共类HTTPErrorController { @RequestMapping(值=/错误/ 404.html)
公共字符串handle404(){
返回errorPageTemplate
} @RequestMapping(值=/错误/ 403.html)
...}
地图在web.xml中的错误页面
&LT;错误页&GT;
&LT;无差错code取代; 404 LT; /无差错code&GT;
&LT;地点&gt; /errors/404.html< /地点&gt;
&LT; /错误页&GT;
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.
How can I configure to have a custom 404 page for any page that goes through the DispatcherServlet?
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.
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
<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>
One option is to map all your error pages through your dispatcher servlet.
Create a new HTTP error controller:
@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
<error-page>
<error-code>404</error-code>
<location>/errors/404.html</location>
</error-page>
这篇关于自404使用Spring的DispatcherServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!