Spring MVC resource versioning with adding ResourceUrlEncodingFilter
I have been trying to get new versioning of resources since 4.1. Of
http://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources
and
I cannot register the ResourceUrlEncodingFilter correctly. How do you do this so that it picks up urls in jsp? I am using javaconfig and in my extended method WebMvcConfigurerAdapter -> addResourceHandlers I have
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/META-INF/resources/static/")
.resourceChain(true)
.addResolver(
new VersionResourceResolver()
.addFixedVersionStrategy("1.1.0", "/**/*.js")
.addContentVersionStrategy("/**"));
This seems to work as I can get the changes in the controller
@Autowired
private ResourceUrlProvider resourceUrlProvider;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String homePub() {
logger.debug("js = '{}'", this.resourceUrlProvider.getForLookupPath("/static/test.js"));
logger.debug("css = '{}'", this.resourceUrlProvider.getForLookupPath("/static/test.css"));
return "test";
}
Will output
DEBUG TestController - js = '/static/1.1.0/test.js'
DEBUG TestController - css = '/static/styles/test-4c517674c05348b2aa87420e7adc420b.css'
Originally urls in jsp are ignored, so I added
container.addFilter("resourceUrlEncodingFilter", ResourceUrlEncodingFilter.class).addMappingForUrlPatterns(
null, true, "/*");
To my implementation of WebApplicationInitializer
This gives below exception
java.lang.IllegalStateException: Failed to determine lookup path: /test/static/test.js
So at least I know what the filter is called, it just didn't take my handlers I tried to add a resource handler for / test / static / ** but it didn't help. Changing isMatchAfter to false stops the exception, but the filter doesn't seem to be called.
The jsp question is very simple
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<c:url value='/static/test.js'/>
So my guess is not how to configure the ResourceUrlEncodingFilter, how should it be added
source to share
There seems to be a mistake
https://jira.spring.io/browse/SPR-12279
You need to add
@Override
public HandlerMapping resourceHandlerMapping() {
SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) super.resourceHandlerMapping();
handlerMapping.setInterceptors(this.getInterceptors());
return handlerMapping;
}
And extend WebMvcConfigurationSupport instead of WebMvcConfigurerAdapter
There are other problems too
https://jira.spring.io/browse/SPR-12281
And securityFilter causes problems, but the above should contain the basics
Update: Release 4.1.2 addresses these issues
source to share