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
<error-page>
configuration work?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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With