How can I replace "#panel-element-964921" to "#panel-element-${stateOne}" while having the value of "${stateOne}" to show up using Thymeleaf?
<div class="panel-heading">
<a class="panel-title" data-toggle="collapse"
data-parent="#panel-790692" href="#panel-element-964921" th:text="${stateOne}">State
Name</a>
</div>
Here it is th:href="|#panel-element-${stateOne}|"
To concatenate plain strings with server-side model attributes one of the easier approaches is to use the || symbol wherein we write the complete string with server-side variables embedded within ${varibale}. This is a common string template feature supported in quite a few programming languages (sadly not in Java)
Then any attributes to be processed by Thymeleaf need to be prepended by th:.
Related
Anyone know that "th:for" is in Thymeleaf? I know it's a simple question, but I can't find the answer online, even in the Thymeleaf documentation.
It is the Thymeleaf attribute equivalent of the HTML for attribute used by <label> elements. For example:
<div class="preference">
<label for="cheese">Do you like cheese?</label>
<input type="checkbox" name="cheese" id="cheese">
</div>
It is listed in the Thymeleaf documentation section 5.2:
There are quite a lot of attributes like these, each of them targeting a specific HTML5 attribute...
It's no different from most other HTML attributes which have a Thymeleaf version - you can use it with a Thymeleaf expression. If you don't need a Thymeleaf expression, just use the plain for attribute instead.
I am getting an error because the razor and html is getting confused by the compiler I imagine.
<div id="#Model.MyLabelCars" ...>
My model variable is:
Model.MyLabel
The "Cars" is just raw text that should be in the HTML.
So say Model.MyLabel's value is "123" the ID should be:
id="123Car"
How can I seperate the model's variable name and HTML?
You could use regular string add operator
<div id="#(Model.MyLabel + "Car")"></div>
Or C# 6's string interpolation.
<div id="#($"{Model.MyLabel}Car")"></div>
What you want is to use the <text></text> pseudo tags
<div id="#Model.MyLabel<text>Cars</text>" ...>
I use spring boot and thymeleaf. I have many entity and I need to create crud ui. All ui will be similar Is it possible to use reflection to reduce ui to generate?
I would like to loop on the fields and get value in the thymeleaf template
In these days I have been working on an issue very similar to yours.
I have discovered that Thymeleaf has this thing called preprocessing which allows you to have nested expression, so something like this works:
<p th:text="${__${'myObj.myField'}__}"></p>
In this situation, the expression __${'myObj.myField'}__ is evaluated first: after this preprocessing, the expression that will be evaluated by the <p> tag will become:
<p th:text="${myObj.myField}"></p>
Which is the desidered result. Note that, in the preprocessed expression, we are passing the field as a string: therefore, it should be totally possible to create a fragments with a th:each in it which accepts a list of strings in which each string is a field. This list could either be injected directly in the model through Java or could be statically declared in the Thymeleaf's markup. An example of such fragment could be:
<div th:fragment = "show-details">
<tr th:each = "field : ${fieldsToPrint}">
<td th:text = "${__${myObj + '.' + field}__}"></td>
</tr>
</div>
Which could, for example, be used like this:
<div th:replace = "myfragments :: show-details(fieldsToPrint = ${ {'name', 'surname'} }, myObj = ${objFromModel})"></div>
The list of fields could either be generate manually or through Java reflection.
This procedure still involves some work, but way less than creating by hand all the views, especially if you can show all the entities with the same show-details fragments. I have not got around to it yet, but it should be possible to use a similar pattern also for editing such entities.
my problem is following:
in this code
<header>
<h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1>
</header>
<blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" />
<ul th:if="${not #lists.isEmpty(menuItems)}">
<li th:each="menuItem : ${menuItems}">
<a th:href="#{${menuItem.url}}" th:class="${menuItemStat.first}? 'home'">
<span th:utext="${menuItem.label}"></span>
</a>
</li>
</ul>
the line <h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1> gets the name of actual menu. I need the menuName in this line <blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" /> to do the same thing, but "${BLC_PAGE.pageFields[menu_name]}" is not appropriate, as it is from thymeleaf tags library. Any idea how to get this this value in blc:menu tag?
In order to resolve the Thymeleaf expression ${BLC_PAGE.pageFields[menu_name]}, you will need to wrap it like so: th:attr="menuName=${BLC_PAGE.pageFields[menu_name]}". Because menuName is not a standard HTML attribute, you won't be able to simply add the Thymeleaf namespace to menuName (i.e., th:menuName), and, without the th: namespace, Thymeleaf doesn't know that it needs to resolve any expressions. The Thymeleaf attr attribute allows you to use non-standard HTML attributes with Thymeleaf. For more information on setting attribute values using Thymeleaf you can review their documentation.
Here are links to the Broadleaf and Thymeleaf documentation on Processors and Dialects:
Thymeleaf: http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html#dialects-and-processors
Is there an alternate way I can get this to process with Thymeleaf? Thymeleaf doesn't like the open tag, but I want to only render it for ROLE_A.
<span sec:authorize="hasRole('ROLE_A')">
<div class="col-xs-10">
</span>
bunch of text not specific to ROLE_A
<span sec:authorize="hasRole('ROLE_A')">
custom text specific to ROLE_A
</div>
</span>
I tried using
<sec:authorize="hasRole('ROLE_A')">
and
<div sec:authorize="hasRole('ROLE_A')">.
The former also doesn't run due to the same open tag issue and the latter is mixing up the closed div tags.
I have numerous blocks like this, so duplicating sections for different roles is not a great solution.
Not in the style you want. You need to close those elements.
My advice is then consider using fragments and load them as required with the needed data/
That way you reuse existing code thus making it cleaner and smaller.
For more examples and way you can do the above check out the Thymeleaf page http://www.thymeleaf.org/doc/springsecurity.html.