Thymeleaf don't expand data-<name> attribute on body tag - spring-boot

I have to downgraded a project with Spring Boot + Thymeleaf to make it work on Tomcat 7 for reasons of the production environment.
I have a Thymelead template where I collect some variables on HTML body tag:
<body th:title="${key}" th:data-key="${key}" th:data-rol="${rol}" th:data-iden="${iden}">
The title attribute was introduced to see that the variable 'key' is collected.
In the original project with Thymeleaf 3.0 (running on Tomcat 8.5) all the variables was collected but with the downgrade to Thymeleaf 2.1.6 only the attribute title is initialized.
Is there any possibility to make the template works with this approach?

I doubt there is a way to turn on that functionality in Thymeleaf 2, that being said the alternative syntax would be this (should work in all versions):
<body th:title="${key}" th:attr="data-key=${key},data-rol=${rol},data-iden=${iden}">

Related

What is the right approach to process a Freemarker Template on the fly in Spring service bean?

I a few existing freemarker templates (ftl) in Spring MVC project. They are using standard spring tags (#spring.url, #spring.message and #spring.bind). While using standard approach of using FreemarkerConfig inside the servlet context (with ViewResolver) all of the templates work fine.
I also have some email templates which is programatically being created, but they are not using any spring tags or additional tlds.
The twist is, I have a task to use the regular template and process them using Freemarker Template class from a Service Bean. The service class have the find the template in the specified location ("/WEB-INF/views"), but it does not process the spring tags, and finds "spring" variable to null. I have set "auto_import" to "/spring.ftl as spring", but my understanding is it will only work within Web/servlet context? Am I using the right approach here? What is the best way to process a "Template" outside and how to expose the spring.tld or anyother.ltd for it? Appreciate your help / pointers!

Thymeleaf th:include doesn't work after update to Spring Boot 1.3

I have an application which works fine with Spring Boot 1.2.6. Now I tried to 1.3.5 and have the problem, that the following statement doesn't work anymore:
<head >
<title th:text="#{app.title} + ' - ' + #{login.title}"></title>
<th:block th:include="main::head"/>
</head>
I can see in the Thymeleaf log that the main.html is found. Furthermore the Thymeleaf-Expressions from the head are evaluated. How ever the html in the browser has no content in head, neither title nor the content from main.html.
As far as I can see the Thymeleaf version hasn't changed.
So what can be the reason?
The version of the Layout dialect has changed. So I added the property <thymeleaf-layout-dialect.version>1.2.9</thymeleaf-layout-dialect.version> to my pom.xml and everything was fine.
I created an issue for that.

Spring Boot + Freemarker master template

I`m trying to build an app with spring boot and freemarker as template engine. The problem I have is, I want to make a "master template" for all my pages to use. I found out that this is achievable in Freemarker with macros. This is how my indexmaster.ftl looks like:
[#macro indexmaster title="defaultTitle"]
<html>
<head> css stuff </head>
<body>
<div id="content">[#nested /]</div>
</body>
</html>
[/#macro]
and in the other pages, I use the macro like this:
[#import "/WEB-INF/ftl/master/indexmaster.ftl" as layout /]
[#layout.indexmaster title="My title"]
...rest of the page
[/#layout.indexmaster]
The problem I`m facing is, the freemarker "code" is interpreted as text when I access the page
click me
What am I doing wrong? Is there any extra spring boot configuration needed?
Use "<>" instead of "[]" for Freemarker tags.
You need to set the tag_syntax configuration setting of FreeMarker to auto_detect or square_bracket. The default is angle_bracket for backward compatibility. Another option is to start the template with [#ftl], which turns on square bracket syntax even if tag_syntax is angle_bracket.

Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation?

I am developing a myCustomTag.tag file to implement my custom tag in spring MVC supported project. I know that the developer using the myCustomTag would use the myCustomTag within the spring provided tag. For E.g. :
<form:form action="..." commandName="...>
<myprefix:mycustomTag ............ />
</form:form>
Is it a good design to use a spring's MVC tag ( let's say form:input tag) within a custom myCustomTag.tag file implementation ?
Absolutely, I don't see a reason why you would not want to use spring tags within a custom tag.
For example, for a large form, you might want to separate sections into their own tags and spring tags are perfectly ok to be used there (ie, if it is syntactically allowed).

Display Spring MVC model attributes in thymeleaf template

I am developing a full Spring application with Spring MVC and Thymeleaf in view layer. In the past I've worked with JSPs and Spring MVC in view layer, but those are now dinosaurs I guess.
So my problem is that with JSPs I could very easily display model attributes in view by adding value in model.addAttribute in controller and displaying the same in JSP anywhere with placeholder evaluating to springex ${value}. So if I want to place a title in page I can write <title>${appName}<title>. This is one of the places where I can put any springex.
I am having hard time to figure out how to do this with Thymeleaf as it uses attribute based parsers. So anywhere on page if thymeleaf prefix is not included it won't process spring expression. It's very hard to work with limited set of tag libraries. I've heard of custom attributes for thymeleaf but I guess there should be a better way to do this.
You can use the th:text attribute, e.g.
<html ... xmlns:th="http://www.thymeleaf.org">
...
<title th:text="${appName}">mocking text</title>
...
</html>
The content of the tag ("mocking text" in this case) gets replaced by the result of the expression in the th:text attribute.
Of course you need to have the appropriate JAR files on CLASSPATH and have the Thymeleaf view resolver properly configured, as described in the Thymeleaf+Spring guide.
For additional information about how template processing works with Thymeleaf in general you can refer to the Thymeleaf guide.

Resources