importing external URL content with th:include

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

importing external URL content with th:include

constv
Hi,

I have been trying to import content rendered by a request to a URL in my application that is not associated with any template file. Instead, the controller's handler method returns a response, e.g. JSON or string. Specifically, I am using Spring Boot Actuator, and I have been trying to output the content rendered by actuator built-in endpoints (e.g. /configprops) in a designated section on my admin page that implements a certain layout. I have added an instance of UrlTemplateResolver to the SpringTemplateEngine in my application context, and the following works fine:

   <div th:include="http://localhost:9000/myapp/configprops">...</div>

This dumps the list of all configuration properties in to a certain section of the page while maintaining the common page layout used by most pages of the application. Note that I am explicitly specifying the URL here, and that's not what I would want. I would rather have the application dynamically resolve the URL relative to the servlet context. However, if  dIo this:

<div th:include="@{/configprops}" >...</div>

I get an error telling that no template with name myapp/configprops is found. The following doesn't work either although the variable resolves to exactly the same string I use in the first example - the one that works:

<blockquote th:with="importUrl='http://' + ${#httpServletRequest.getLocalAddr()} + ':' + ${#httpServletRequest.getLocalPort()} + '/myapp/configprops'">
    <div th:include="${importUrl}" >...</div>
</blockquote>

I get the same message about template "http://localhost:9000/myapp/configprops" not being found.

It seems the engine is parsing the th:include tag differently if it immediately sees the "http" prefix explicitly in the value and only then the UrlTemplateResolver is used. Otherwise, it is interpreted as a template name, and Spring uses the wrong resolver.

What am I doing wrong? Is there a way to import content from a URL that is mapped to a controller handler that returns response body (not a view name.)

Thank you for any help!
Reply | Threaded
Open this post in threaded view
|

Re: importing external URL content with th:include

Zemi
Administrator
Hello,

assuming that your diagnosis is right (I've not tried it), maybe you could use

   <blockquote th:with="importUrl='${#httpServletRequest.getLocalAddr()} + ':' + ${#httpServletRequest.getLocalPort()} + '/myapp/configprops'">
      <div th:include="|http://${importUrl}|" >...</div>
   </blockquote>

Of course, it should be better to encapsulate this logic in an utility method, so you could end up with something like

    <div th:include="|http://${#url.path('/myapp/configprops')}|" >...</div>

or even better

    <div url:rootinclude="/myapp/configprops" >...</div>

Regards,
   Zemi


Reply | Threaded
Open this post in threaded view
|

Re: importing external URL content with th:include

jameshowe
It appears that this no longer (3.0.11.RELEASE) works at all.
I get "Error resolving template" with the absolute URL no matter what I do.
Reply | Threaded
Open this post in threaded view
|

Re: importing external URL content with th:include

jameshowe
jameshowe wrote
I get "Error resolving template" with the absolute URL no matter what I do.
Which can then be resolved by configuring a UrlTemplateResolver.