Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<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?
like image 882
hina10531 Avatar asked Mar 25 '15 01:03

hina10531


Video Answer


1 Answers

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

like image 97
Jonathan Z Avatar answered Sep 24 '22 02:09

Jonathan Z