Announcement Announcement Module
Collapse
No announcement yet.
WebApplicationInitializer and web.xml error-page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • WebApplicationInitializer and web.xml error-page

    I am trying to use Spring's JavaConfig approach but got stuck at the web.xml error-page directive.
    ServletContext does not offer anything here but I do not want to mix & match Java + xml configuration.

    For Tomcat 7 here is what you can do in your WebApplicationInitializer:

    Code:
    try {
    	ApplicationContextFacade appContextFacade = (ApplicationContextFacade)container.getContext(container.getContextPath());
    	Field appContextInFacade = ApplicationContextFacade.class.getDeclaredField("context");
    	appContextInFacade.setAccessible(true);
    	ApplicationContext appContext = (ApplicationContext)appContextInFacade.get(appContextFacade);
    	Field appContextInContext = ApplicationContext.class.getDeclaredField("context");
    	appContextInContext.setAccessible(true);
    	StandardContext context = (StandardContext)appContextInContext.get(appContext);
    
    	ErrorPage ep500 = new ErrorPage();
    	ep500.setExceptionType("java.lang.Throwable");
    	ep500.setLocation("/errors/500");
    	ErrorPage ep404 = new ErrorPage();
    	ep404.setErrorCode("404");
    	ep404.setLocation("/errors/404");
    	Method addErrorPage = StandardContext.class.getDeclaredMethod("addErrorPage", ErrorPage.class);
    	addErrorPage.invoke(context, ep500);
    	addErrorPage.invoke(context, ep404);
    } catch (Exception e) {
    	e.printStackTrace();
    }
    Last edited by mscheibe; Jan 17th, 2012, 03:50 PM. Reason: added code

  • #2
    Basically no there isn't a way to do that. You could use an web-fragment to only define your error pages, this would leave you with little xml but a portable solution. Your current solution isn't very portable nor clean.

    Comment


    • #3
      Yes, my current solution is not portable or very clean but for web-fragment to work I would have to create a jar with just META-INF/web-fragment.xml in it which will split up configuration even more...

      Comment


      • #4
        If I'm not mistaken it should also work if you put in in the META-INF directory inside the WEB-INF/classes directory. It should scan the META-INF directories on the class path and that one is on the class path also.

        Comment


        • #5
          Does not work in WEB-INF/classes/META-INF... Hopefully it will be added to ServletContext at some point.

          Comment

          Working...
          X