Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC VersionResourceResolver / ContentVersionStrategy not working correctly in JSP

I have a Spring MVC (4.3.0) application and have registered a VersionResourceResolver with added ContentVersionStrategy with the ResourceHandlerRegistry. I have the ResourceUrlEncodingFilter enabled.

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean devMode = this.env.acceptsProfiles("local");
        //boolean useResourceCache = !devMode;
        Integer cachePeriod = devMode ? 0 : (60 * 60 * 24 * 365); //If dev clear cache else 1 year

        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/")
                .setCachePeriod(cachePeriod)
                .resourceChain(false)
                .addResolver(new VersionResourceResolver()
                    .addContentVersionStrategy("/**"))
                .addTransformer(new AppCacheManifestTransformer());
}

When I access anything in /resources (JS, Images, CSS, etc) on a JSP page using the c:url or spring:url tags, the "versioned" URL does not show up (meaning: no hash code in the URL). Example:

<link href="<c:url value="/resources/css/views/login.css" />" rel="stylesheet">

Produces: /myapp/resources/css/views/login.css as the URL string when inspecting the page.

But, if I use a ResourceURLProvider in my Controller, I do see the hash code in the URL:

@Autowired
private ResourceUrlProvider mvcResourceUrlProvider;

@RequestMapping(value = { "/" }, method = RequestMethod.GET)
public String projectBaseRedirect() {

    logger.debug("js =  '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/js/views/overview.js") + "'");
    logger.debug("css =  '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/css/views/overview-page.css") + "'");

    return "redirect:/admin/overview";
}

The log messages produce this:

2016-07-09 11:47:19   DEBUG AdminLoginController:35 - js =  '/resources/js/views/overview-36d1ff98d627d92a72d579eca49dbd8a.js'
2016-07-09 11:47:19   DEBUG AdminLoginController:36 - css =  '/resources/css/views/overview-page-d47f10e5bcf0fdd67bd8057479b523f0.css'

Why is this working in the controller but not on my JSP pages?

I am also using Spring Security (4.1.0)...

like image 675
Brian Waldhart Avatar asked Jul 15 '16 01:07

Brian Waldhart


2 Answers

As it is missing in your example it is maybe missing in your project as well. You need a resource provider for the template engine you're using. In case of JSP, register a filter:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
    return new ResourceUrlEncodingFilter();
  }
}

Or use a filter registration bean with that filter:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new ResourceUrlEncodingFilter());
    filterRegistrationBean.addUrlPatterns("/*");
    return filterRegistrationBean;
  }
}
like image 175
Michael Simons Avatar answered Nov 15 '22 06:11

Michael Simons


Rossen's comment on Michael's answer was exactly the missing glue I needed. After I put this code in the class that extends AbstractAnnotationConfigDispatcherServletInitializer, it worked perfectly with the other code from my original post!

@Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

        FilterRegistration.Dynamic fr = servletContext.addFilter("resourceUrlEncodingFilter",
                new ResourceUrlEncodingFilter());
        fr.setInitParameter("encoding", "UTF-8");
        fr.setInitParameter("forceEncoding", "true");
        fr.addMappingForUrlPatterns(null, true, "/*");
}
like image 39
Brian Waldhart Avatar answered Nov 15 '22 07:11

Brian Waldhart