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)...
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;
}
}
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, "/*");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With