Spring MVC does not load static resources from webapp

The question Failed to get Spring to add a resource handler for static resources.

Background info I have a Spring MVC web application running on a standalone Jetty instance. My webapp structure

webapp root/
    resources/
        css/
        images/
        js/
    WEB-INF/
        layouts/
            tiles.xml
            page.jsp
        lib/
        views/
            home/
                home.jsp
                tiles.xml
        web.xml

      

I have expanded WebMvcConfigurerAdaptor

to add resource mapping so that urls such as company.com/myservlet/resources/css/main.css

will resolve to the folder webapp root/resources/css

.

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

      

My little controller:

@Controller
public class SpringAppController {       

    public SpringAppController() {
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView handleRequestHome(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        return new ModelAndView("home");
    }
}

      

My web.xml is just this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- Java-based Spring container definition -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <!-- Location of Java @Configuration classes that configure the components that makeup this application -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.company</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>admin</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>admin</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Ensure UTF-8 encoded pages so that certain characters are displayed and submitted correctly -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Enables support for DELETE and PUT request methods with web browser clients -->
    <filter>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>hiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

      

My JavaConfig for webapp for instantiating some mvc resolvers is like this:

@Configuration
public class MainConfig {

    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions(new String[] {
            "/WEB-INF/layouts/tiles.xml",
            "/WEB-INF/views/**/tiles.xml"
        });
        configurer.setCheckRefresh(true);
        return configurer;
    }

    @Bean
    public org.springframework.context.MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/WEB-INF/messages/messages");
        return messageSource;
    }

}

      

When I click url company.com/myservlet/home, the tile stuff works and the home.jsp page is displayed, but the css and images are not loading. The servlet logs this warning:

[DEBUG] [DispatcherServlet] [DispatcherServlet with name 'admin' processing GET request for [/api/v1/tlb/resources/page.css]] 
[DEBUG] [RequestMappingHandlerMapping] [Looking up handler method for path /resources/form.css] 
[DEBUG] [RequestMappingHandlerMapping] [Did not find handler method for [/resources/form.css]]
[WARN ] [PageNotFound] [No mapping found for HTTP request with URI [/api/v1/tlb/resources/form.css] in DispatcherServlet with name 'admin']

      

I've tried debugging the servlet and the method is WebMvcConfig.addResourceHandlers()

never called and hence why static resources cannot be found. What am I missing to get this to work?

As a side note, if I add the following to my web.xml the css resource is loaded, but I would like to know why the method is WebMvcConfig.addResourceHandlers()

not being called, so I don't need this servlet mapping.

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

      

+3


source to share


1 answer


Well, I fixed the issue, but not really sure why or how else. The project imported the jar containing the class, which it extended WebMvcConfigurationSupport

as follows:

@Configuration
public class EnableUriMatrixVariableSupport extends WebMvcConfigurationSupport {

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();
        hm.setRemoveSemicolonContent(false);
        return hm;
    }
}

      



Additionally, I also have an annotation @EnableMvc

that is importing DelegatingWebMvcConfiguration. I think this results in two instances of WebMvcConfigurationSupport being created and this is causing havoc in the spring container. Unfortunately I upgraded to spring 4.x in the process of fixing this issue, so I'm not sure if this helped in any way.

0


source







All Articles