0

I am developing this Spring app where I "suddenly" decided to match error pages such as 404 (the famous HTTP 404 Not Found) to a more user friendly page.

After quick research, I find that adding this code to web.xml does the trick

    <error-page>
        <error-code>404</error-code>
        <location>/error/404.jsp</location>
    </error-page>

Here we assume that I added the required 404.jsp file to /webapp/error/ directory.

Related articles:

However after running the app and typing a random url (such as http://localhost:8080/SpringWebApp/myRandomUrladasdad) I find myself redirected to another standard 404 page - not mine.

FYI this is my entire web.xml

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>sphbmveclp.account.controllers.ImageFromDirServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/image/*</url-pattern>
  </servlet-mapping>

  <error-page>
    <error-code>404</error-code>
    <location>/error/404.jsp</location>
  </error-page>

After an unreasonnable amount of googling I ran into this:

http://www.mkyong.com/spring-mvc/404-error-code-is-not-working-in-spring-mvc/

So I assumed I had something good here. I assumed my vast <url-pattern>/</url-pattern> may have been causing all this trouble. So I changed it to something more specific <url-pattern>/admin/*</url-pattern> in this case.

But no luck, It is still not working as expected.

Any idea?

Community
  • 1
  • 1
Adriano
  • 19,463
  • 19
  • 103
  • 140

3 Answers3

1

Pretty sure that 404 handling is outside the Spring contanier.. How are you handling url patterns on Spring-MVC? Can you post your spring context file?

smk
  • 5,340
  • 5
  • 27
  • 41
  • thx for answering, voted up but actually found the real issue... stupid IDEs... :) – Adriano Oct 19 '12 at 17:29
  • 1
    Also, if you are using Spring, I recommend SpringSource IDE. Makes life much easier for any Spring related projects. – smk Oct 19 '12 at 18:29
-1

This is just one more warning about running Tomcat within Eclipse.

Somehow my config works perfectly fine if I deploy my app manually or automatically "outside" Eclipse (deploying the .war file in Tomcat manually)

There have been other issues related to running Tomcat within an IDE, for instance:

Conclusion: be careful when running Tomcat within Eclipse...

Adriano
  • 19,463
  • 19
  • 103
  • 140
  • Wrong conclusion. Run Tomcat inside Eclipse, but know how to do it right. Or just simple get rid of any IDE and write your code in notepad and deploy war manually. ;) – Aleksandr M Oct 19 '12 at 22:07
  • what do u mean by do it right? any link to useful resource is welcome – Adriano Oct 22 '12 at 10:18
  • http://www.mulesoft.com/tomcat-eclipse and http://www.coreservlets.com/Apache-Tomcat-Tutorial/eclipse.html – Aleksandr M Oct 22 '12 at 10:22
  • so what you are saying is that the only reason why I have this issue is because I haven't configured my Tomcat properly? – Adriano Oct 22 '12 at 10:32
  • What I am saying is: not using Tomcat from Eclipse is just silly. – Aleksandr M Oct 22 '12 at 10:39
  • I know it sounds silly but I had a couple of issues which happened very specifically when running Tomcat within Eclipse. And I do believe my eclipse & tomcat configs are fine. I probably went too far by concluding "don't run Tomcat within Eclipse" and should prob change it to "be careful when running Tomcat within Eclipse" though. – Adriano Oct 22 '12 at 10:47
-1

After a reasonnable amount of googling I found this: http://developingdeveloper.wordpress.com/2008/02/20/handling-exceptions-in-spring-mvc-part-1/.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143