简体   繁体   中英

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

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. 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. Well there should be some differences between redirection and typical request via typing something on url or ajax.


What I want to know

  1. How can I make <error-page> configuration work?
  2. What's the differences between the automatic redirection by Spring and the typical request from a user action in Spring MVC ?

Error page size must > 1024 bytes.
Add somethings useless to you error page for a test.
Add this to make sure the error page has this attribute: [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

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

Can you check your servlet-context.xml configuration. 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. 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.

Verify this in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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