How to get absolute url as href/src value using Thymeleaf and Urlrewritefilter - url-rewriting

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.

Related

Why can't I get Thymeleaf to work on Script tags

I have a simple Spring Boot application and I am trying to add Thymeleaf. The basic structure of my app is available here.
Basically when I try to render the following in the template...
<h1>[[${key}]]</h1>
It works perfect! However, when I try...
<script src="https://my.api.com/api/js?key=${key}" />
The ${key} is not getting replaced. What am I missing?
If you want Thymeleaf to resolve attributes, you have to prefix them with th:.
<script th:src="|https://my.api.com/api/js?key=${key}|" />
or
<script th:src="#{https://my.api.com/api/js(key=${key})}" />

Spring Boot 2.0 Static content not using context path

I have a Spring Boot 2.0 application that I'm trying to deploy as a WAR file. This means that it will have a custom context path. To test as a Java application I added
server.servlet.context-path=/MyApplication
to the application.properties. In my index.html (located in src/main/resources/static) I try to include Javascript using something like this:
<script src="dist/main.js"</script>
Regardless of whether I am using the context path, this always tries to load the file from http://localhost:8080/dist/main.js completely ignoring the context path I have specified. The same is true if I try to deploy my application as a WAR. The file is really at http://localhost:8080/MyApplication/dist/main.js.
What do I need to change in my configuration to make Spring Boot use the context path when serving static content?
I just figured it out. In my index.html I had set a base href:
<base href="/">
I converted index.html to a JSP and set the base href using a JSP tag:
<base href='<c:url value="/" />'>
Modify the <base href="/"> in index.html to the following,
<base href="./">
This will try to load all the scripts from the context path that is specified and it fixed the issue for me.

URL rewrite not showing effect

I have written below rule in my urlrewrite.xml file of my struts application which is using tuckey filter 4.0. We have a requirement in our application that a url of type say http:localhost:8080/tgld/viewTopic?guidelinename=A&topicFile=B needs to be replaced by URL of type http:localhost:8080/tgld/etg/topicFile . some kind of masking for the original URL as it looks shaby with so many parameters. URLrewrite jar 4.9 is already in WEB-INF and my http://localhost:8080/rewrite-status page is loading fine.
here tgld is the application context and viewTopic is my action name
it will be great if someone will help us here
<rule>
<from>/viewTopic/*</from>
<to>/etg/$2</to>
</rule>
Please Change the rule as below
<rule>
<from>^/viewTopic?guidelinename=(.*)&topicFile=(.*)$</from>
<to type="redirect">/tgld/etg/$2</to>
</rule>

Spring #RequestMapping("/favicon.ico") does not work when requested from index.html

I used to get mapping not found for favicon.ico so decided to deal with it.
Easiest thing for me was to add an action to a controller method,#RequestMapping("/favicon.ico").
I no longer get these complaints ( although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically ).
When I visit http://localhost:8080/favicon.ico the action gets hit!
I add the following to my html file:
<link href="/favicon.ico" rel="icon" type="image/x-icon" />
But the action never gets hit.
I've also tried
<link href="http://localhost:8080/favicon.ico" rel="icon" type="image/x-icon" />
but action does not get hit.
I suspect this has to do with get/post request something, and when I request it from the browser manually, a get request is made. When from an html file something else, and Spring won't recognize this.
Please do not recommend me doing:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
as I like to do it from my controller as I have some logic there.
Could there be some cache involved?
EDIT:
I should also mention that i keep getting a tomcat favicon instead. Can't see any info on a favicon.ico ever being requested.
Is tomcat supplying it by default and ignoring to hit/forward it to my action?
I ran into the same issue.
When spring boot starts you can see this in the console:
[...] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
You can disable the spring mvc favicon handler from the application.properties:
spring.mvc.favicon.enabled=false
Source: Spring Boot: Overriding favicon

Spring3 - WebFlow - JSF -- Can't get mapping of '/' to work properly

Sorry if this is a Newbie question, but I am trying to teach myself Spring MVC/WebFlow with JSF/Primefaces, and I've run into a snag setting it up...
If in web.xml, I set the MVC dispatcher to a catch all '/', then register #RequestMapping(value = "/{catchall}", method = RequestMethod.GET), in my controller. The page is served, but the resources files all have the {catchall} name prepended to the start of the name e.g.
If I use //127.0.0.1:8080/testpage
<link type="text/css" rel="stylesheet" href="/testpage/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
This results in every resource being NOT FOUND, and returning a 404 error?
If instead of a 'catch-all', I set the MVC dispatcher to '/a/*', the perform the same test, e.g.
//127.0.0.1:8080/a/testpage, it works fine with the resources being shown as:
<link type="text/css" rel="stylesheet" href="/a/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
I am trying to setup a system where the page is served dynamically from the datastore, and want the page to be - www.whatever.com/{pagename} - without any prefixed structure, or postfixed identifier (e.g. .jsp, .jsf, .xhtml, etc.)
I can post configs if required, but am sure I'm just missing something stupid!!!!
Please help.
Last time I tried I found that the Sun Mojarra library assumes your servlet mapping is either a prefix mapping or a servlet mapping by extension (but not the default servlet mapping "/"). Your best bet to use URLs without a servlet prefix might be to use URL rewriting techniques such as Tuckey UrlRewriteFilter or in JSF PrettyFaces is quite popular.

Resources