benstpierre
benstpierre

Reputation: 33581

Cannot serve static files in webapp with spring

I have a webapp with a very simple web.xml.

<?xml version="1.0"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
</web-app>

I set this contextConfigLocation property so that the class org.glassfish.jersey.server.spring.SpringWebApplicationInitializer does not automatically add ContextLoadListener and RequestContextListener as I wish to do this manually.

Then I have my own WebApplicationInitializer class to add servlets programmatically instead of via web.xml.

public class MyWebAppInitializer implements WebApplicationInitializer {

    private static final Logger LOGGER = Logger.getLogger(SpringWebApplicationInitializer.class.getName());

    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        //Setup spring listeners
        {
            LOGGER.config(LocalizationMessages.REGISTERING_CTX_LOADER_LISTENER());
            sc.addListener(ContextLoaderListener.class);
            sc.addListener(RequestContextListener.class);
        }
        //Jersey Rest Servlet
        {
            final ServletRegistration.Dynamic registration = sc.addServlet(ServletContainer.class.getName(), ServletContainer.class);
            registration.setInitParameter("javax.ws.rs.Application", com.tervita.portal.RestApplication.class.getName());
            registration.addMapping("/rest/*");
        }
        //Normal Servlets
        {
            final ServletRegistration.Dynamic registration = sc.addServlet(LoginAction.class.getName(), LoginAction.class);
            registration.addMapping("/LoginAction");
        }
        //Apache Default Servlet
        {
            ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", DispatcherServlet.class);
            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/test");
        }

    }
}

When I run my webapp I am unable to access static files I have added such as /css/app/ui-grid.css.

war file structure

When I run Tomcat I get this though...

enter image description here

So I am confused I do not have any entries in my servlet config that override the default servlet which in Tomcat should be /*. Why would the default servlet not be being hit and serve my content?

Upvotes: 1

Views: 226

Answers (1)

benstpierre
benstpierre

Reputation: 33581

Jersey has a class that implements ServletContainerInitializer called JerseyServletContainerInitializer which was changing my default mappings for /* and such. I had to disable scanning for ServletContainerInitializer classes in the container via web.xml as per How to disable Servlet 3.0 scanning and auto loading of components and my static resources work again.

Upvotes: 0

Related Questions