<error-page> does not work in Spring MVC

PROBLEM

I want to show users a custom error page. Simeply put, <error-page> on web.xml does not seem to work. I can guess what the problem is, but I need to find out why it does not work.


MY SETTING

I set <error-page> to web.xml in my spring mvc application. Below are 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 right in ...

 WEB-INF β”” views β”” Errorpages β”” ErrorPage.jsp 

For your information, I also tried these things below, but none of these 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 was trying to find resources on redirecion, so I changed my plan and tried to achieve this goal with the usual return to Spring MVC .

I did this action

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

What I expected was partially correct because it successfully triggered 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 to url and redirecting / error, as I pointed to <location> in web.xml. But he does not return the view.

To make sure the action returns a view, I tried changing the type of action, for example ...

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

But this does not work ...


Strange thing

is that if I invoke an action through localhost:8080/error , it will invoke the action and return the expected error page. Well, there should be some differences between a redirect and a typical request by typing something in url or ajax.


What i want to know

  • How can I configure <error-page> ?
  • What is the difference between an automatic redirect to Spring and a typical request from a user action in Spring MVC ?
+7
java spring spring-mvc
source share
4 answers

The error page size must be> 1024 bytes.
Add something useless to you error page for the test.
Add this so that the error page has this attribute: [isErrorPage="true"]

+1
source share
 <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error.jsp</location> </error-page> 

the error page may not fit in web-inf, go out and try a lot

0
source share
  <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 need to put your errorpages.jsp file outside WEB_INF folde r and just put the following code in your web.xml.

0
source share

You can check the configuration of servlet-context.xml. For me, this works 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 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 jsp location in web.xml, you can specify there or else you can add a URL. If you add the url, DispatcherServlet will take care to give the answer to the browser, if you specify jsp, it will be like a regular servlet response.

Check this in the servlet-context.xml file

 <!-- 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> 
0
source share

All Articles