简体   繁体   English

<error-page>安装程序在Spring MVC中不起作用

[英]<error-page> setup doesn't work in Spring MVC

PROBLEM 问题

I want to show users a custom error page. 我想向用户显示自定义错误页面。 Simeply put, <error-page> on web.xml doesn't seem to work. Simeply put,web.xml上的<error-page>似乎不起作用。 I can guess what's the problem but I need to learn why exactly it doesn't work. 我可以猜到问题是什么,但我需要了解为什么它不起作用。


MY SETUP 我的设置

I set up <error-page> on web.xml in my spring mvc application. 我在spring mvc应用程序中的web.xml上设置了<error-page> Below is the settings. 以下是设置。

<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>

And my error page is located right in the... 我的错误页面就在...

WEB-INF
└   views
    └   Errorpages
        └   ErrorPage.jsp

For your infomation, I've tried these things below as well, but none of those things worked. 对于你的信息,我也尝试过以下这些东西,但这些都没有用。

<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 -->

SECOND TRY 第二个尝试

I looked at the server log and noticed that spring tries to look up the resources by redirecion, so I changed my plan and tried to acheive this goal through typical view return in Spring MVC . 我查看了服务器日志,发现spring试图通过redirecion查找资源,所以我改变了我的计划,并试图通过Spring MVC典型视图返回来实现这个目标。

I made this action 我做了这个动作

@RequestMapping("/error")
public String throwsErrorPage(Locale locale, Model model) {
    logger.info("Errorpage action..." + locale.getLanguage());              
    return "/Errorpages/ErrorPage";
}

What I expected was partly right, because It successfully calls the action. 我所期待的部分是正确的,因为它成功地称之为行动。 My Server log makes it clear. 我的服务器日志清楚地说明了。

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

As you can see, I generated 404 error by typing notfound on url, and it redirects to /error as I specified on <location> in web.xml. 正如您所看到的,我通过在url上键入notfound来生成404错误,并且它重定向到/我在web.xml中的<location>上指定的错误 But it does not return the view. 但它不会返回视图。

To make sure the action returns a view, I tried changing the action's type like... 为了确保操作返回视图,我尝试更改操作的类型,如...

@RequestMapping("/error")
public ModelAndView throwsErrorPage(Locale locale, ModelAndView mv) {
    logger.info("Errorpage action..." + locale.getLanguage());              
    return mv.setViewName("/Errorpages/ErrorPage");
}

But it doesn't work as well... 但它不起作用......


Strange thing 奇怪的事情

is that if I call the action through localhost:8080/error it surely calls the action and returns the expected error page. 如果我通过localhost:8080/error调用该操作,它肯定会调用该操作并返回预期的错误页面。 Well there should be some differences between redirection and typical request via typing something on url or ajax. 那么通过在url或ajax上键入内容,重定向和典型请求之间应该存在一些差异。


What I want to know 我想知道什么

  1. How can I make <error-page> configuration work? 如何使<error-page>配置工作?
  2. What's the differences between the automatic redirection by Spring and the typical request from a user action in Spring MVC ? Spring自动重定向与Spring MVC用户操作的典型请求之间有什么区别?

Error page size must > 1024 bytes. 错误页面大小必须> 1024字节。
Add somethings useless to you error page for a test. 添加一些无用的错误页面进行测试。
Add this to make sure the error page has this attribute: [isErrorPage="true"] 添加此项以确保错误页面具有以下属性: [isErrorPage="true"]

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>

the error page may not put in web-inf,move out and try a lot 错误页面可能不会放入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

You have to put your errorpages.jsp outside the WEB_INF folde r and just put the following code in your web.xml . 您必须将您的errorpages.jsp 放在WEB_INF文件之外,并将以下代码放在您的web.xml中。

Can you check your servlet-context.xml configuration. 你能检查你的servlet-context.xml配置吗? For me its working with the following configuration. 对我来说,它使用以下配置。

<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>

and the spring controller method 和弹簧控制器方法

@RequestMapping(value = "/error404", method = RequestMethod.GET)
public String error() {
    logger.info("in error method");
    return "/errorPages/error404";
}

and if you want to specify the location of jsp in web.xml you can specify there or else you can add url. 如果你想在web.xml中指定jsp的位置,你可以在那里指定,否则你可以添加url。 If you add url DispatcherServlet will take care of giving response to browser, if you specify the jsp it will be like normal servlet response. 如果添加url,DispatcherServlet将负责给予浏览器响应,如果指定jsp,它将像普通的servlet响应一样。

Verify this in servlet-context.xml 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM