![](/img/trans.png)
[英]Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure session-config and error-page
[英]<error-page> setup doesn't work in Spring MVC
问题
我想向用户显示自定义错误页面。 Simeply put,web.xml上的<error-page>
似乎不起作用。 我可以猜到问题是什么,但我需要了解为什么它不起作用。
我的设置
我在spring mvc应用程序中的web.xml上设置了<error-page>
。 以下是设置。
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.*</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>
我的错误页面就在...
WEB-INF
└ views
└ Errorpages
└ ErrorPage.jsp
对于你的信息,我也尝试过以下这些东西,但这些都没有用。
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/views/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/Errorpages/ErrorPage.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
<!-- Or tried those above without the .jsp that comes after ErrorPage -->
<!-- And also, tried those after moving the resources out of WEB-INF -->
第二个尝试
我查看了服务器日志,发现spring试图通过redirecion查找资源,所以我改变了我的计划,并试图通过Spring MVC
典型视图返回来实现这个目标。
我做了这个动作
@RequestMapping("/error")
public String throwsErrorPage(Locale locale, Model model) {
logger.info("Errorpage action..." + locale.getLanguage());
return "/Errorpages/ErrorPage";
}
我所期待的部分是正确的,因为它成功地称之为行动。 我的服务器日志清楚地说明了。
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/notfound] in DispatcherServlet with name 'appServlet'
INFO : com.company.web.controller.HomeController - Errorpage action...ko
正如您所看到的,我通过在url上键入notfound来生成404错误,并且它重定向到/我在web.xml中的<location>
上指定的错误 。 但它不会返回视图。
为了确保操作返回视图,我尝试更改操作的类型,如...
@RequestMapping("/error")
public ModelAndView throwsErrorPage(Locale locale, ModelAndView mv) {
logger.info("Errorpage action..." + locale.getLanguage());
return mv.setViewName("/Errorpages/ErrorPage");
}
但它不起作用......
奇怪的事情
如果我通过localhost:8080/error
调用该操作,它肯定会调用该操作并返回预期的错误页面。 那么通过在url或ajax上键入内容,重定向和典型请求之间应该存在一些差异。
我想知道什么
<error-page>
配置工作? Spring
自动重定向与Spring MVC
用户操作的典型请求之间有什么区别? 错误页面大小必须> 1024字节。
添加一些无用的错误页面进行测试。
添加此项以确保错误页面具有以下属性: [isErrorPage="true"]
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
错误页面可能不会放入web-inf,移出并尝试很多
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/index.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page
您必须将您的errorpages.jsp 放在WEB_INF文件夹之外,并将以下代码放在您的web.xml中。
你能检查你的servlet-context.xml配置吗? 对我来说,它使用以下配置。
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.*</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error404</location>
</error-page>
和弹簧控制器方法
@RequestMapping(value = "/error404", method = RequestMethod.GET)
public String error() {
logger.info("in error method");
return "/errorPages/error404";
}
如果你想在web.xml中指定jsp的位置,你可以在那里指定,否则你可以添加url。 如果添加url,DispatcherServlet将负责给予浏览器响应,如果指定jsp,它将像普通的servlet响应一样。
在servlet-context.xml中验证这一点
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.