0

I'm using Thymeleaf templating engine with Spring boot for developing my web application. For rewriting url, I'm using UrlRewriteFilter library.

My problem is that I couldn't able to rewrite url to a absolute url. For example, if script src value is configured in html like below

<script th:src="@{/js/jquery.js}"></script>

and rules defined in urlrewrite.xml as

<urlrewrite>
        <outbound-rule>
            <from>/js/jquery.js</from>
            <to>https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js</to>
        </outbound-rule>
</urlrewrite>

The expected output is

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js"></script>

But it is generating as

<script src="/myapphttps://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.js"></script>

where /myapp is context root. How to get rid of context root and get only absolute url which I configured.

sureshd
  • 555
  • 1
  • 5
  • 16
  • 1
    The problem is first THymeleaf replaces the src tag and makes it `/myapp/js/query.js` then you tell the `UrlRewriteFilter` to replace the `/js/jquery.js` part with a full URL and this is exactly what it does. Either don't use `th:src` or include `/webapp` in your rewrite rule, or just specify the full CDN url instead of using a rewrite filter. – M. Deinum Jun 09 '16 at 13:10
  • @M.Deinum you are right, thanks. I changed my rule by including context root like /myapp/js/jquery.js. Now it generated with required absolute url. – sureshd Jun 09 '16 at 13:32
  • @M.Deinum instead of hard-coding context root in the rewrite rule, is there a way to specify it dynamically. – sureshd Jun 09 '16 at 15:33
  • I'm able to get context root dynamically by specifying `%{context-path}`. Its working when I use it inside **To** tag but not working inside **From** tag. How to get working inside **From** tag too? – sureshd Jun 09 '16 at 16:59

0 Answers0