Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VersionResourceResolver not working

Tags:

I have static resources' structure like this in my spring application :

\src
    \main
        \webapp
           \resouces
              \css\..
              \js\..

and have configured VersionResourceResolver like this :

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/")
                .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)).resourceChain(true)
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
    }

I included the resources using jstl in my jsp:

<script type="text/javascript" src="<c:url value="/resources/js/my.js"/>"></script>

But when i run the application I cannot see any kind of versioning. What am I missing ?

Also I have tried :

https://stackoverflow.com/a/38407644/3603806

But no luck

UPDATE

I am including resources like this :

<link rel="stylesheet" href="<c:url value="/resources/css/mycss.css?v=2" />">
<script type="text/javascript" src="<c:url value="/resources/js/myjs.js?v=1"/>"></script>

and this is what is getting generated :

<link rel="stylesheet" href="/app/resources/css/mycss.css?v=2">
<script type="text/javascript" src="/app/resources/js/myjs.js?v=1"></script>

index page using :

<welcome-file-list>
        <welcome-file>/WEB-INF/views/index.jsp</welcome-file>
    </welcome-file-list>

Controller : for home etc...

@RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView home(HttpServletRequest request, ModelMap model) {
        ...
    }
like image 908
Himanshu Tyagi Avatar asked Jan 26 '17 10:01

Himanshu Tyagi


1 Answers

Might be ResourceUrlProvider is null for you in encodeURL of ResourceUrlEncodingFilter.ResourceUrlEncodingResponseWrapper

@Override
        public String encodeURL(String url) {
            ResourceUrlProvider resourceUrlProvider = getResourceUrlProvider();
            if (resourceUrlProvider == null) {
                logger.debug("Request attribute exposing ResourceUrlProvider not found");
                return super.encodeURL(url);
            }
            initIndexLookupPath(resourceUrlProvider);
            if (url.length() >= this.indexLookupPath) {
                String prefix = url.substring(0, this.indexLookupPath);
                int suffixIndex = getQueryParamsIndex(url);
                String suffix = url.substring(suffixIndex);
                String lookupPath = url.substring(this.indexLookupPath, suffixIndex);
                lookupPath = resourceUrlProvider.getForLookupPath(lookupPath);
                if (lookupPath != null) {
                    return super.encodeURL(prefix + lookupPath + suffix);
                }
            }
            return super.encodeURL(url);
        }

To fix it, register ResourceUrlEncodingFilter in web.xml or in WebApplicationInitializer, as follows:

    <filter>
        <filter-name>resourceUrlEncodingFilter</filter-name>
        <filter-class>
            org.springframework.web.servlet.resource.ResourceUrlEncodingFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>resourceUrlEncodingFilter</filter-name>
        <servlet-name>spring-dispatcher</servlet-name>
    </filter-mapping> 

where spring-dispatcher is DispatcherServlet, as:

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

And make sure your my jsp is rendered through spring-dispatcher, for example:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @RequestMapping(value = "/my-jsp", method = RequestMethod.GET)
    public String home() {
        return "my";
    }
}

I created a sample app available on GitHub - spring-resource-versioning, which can answer your further questions.

Hope it help!

like image 95
Arpit Aggarwal Avatar answered Sep 24 '22 11:09

Arpit Aggarwal