How Can I show data on Thymeleaf - spring

Hello all brother this is my controller
model.addAttribute("getDataHead",cartHeadStockinService.getDatahead());
System.out.println(model);
and after that I syso to review i get like this
getDataHead=[Cart_HeadStockin [id=101, invoice=fsdfsdf, po=, remark=, supplyId=1, date=null]]
and I want to show this on my Thymeleaf
th:object="${getDataHead}"
th:text="${invoice}"

Hello friend thats great you are able put the context variables in your Model
The way to print the text of your in your Thymeleaf is like this
<span th:text="${getDataHead.invoice}"></span>
Here is a reference link to Thymeleaf documentation

Related

Thymeleaf code in Spring Boot attribute model

is it possible to sent thymeleaf code via attributeModel to View, to be treated like standard code?
I would like to load a piece of code(thymeleaf fragments) in different places only when it is needed.
But when I try this:
Spring Boot Controller:
model.addAttribute("fragment", "<th:block th:include=\"fragments/header :: body\"></th:block>");
View:
<div th:text="${fragment}"></div>
In WebBrowser as a TEXT I have:
<th:block th:include="fragments/header :: body"></th:block>
Can I force re-render? And how can I do that? But if it is not possible, what can I do in replace?
Every thing that th:text parse, set to innerHTML and not rerendered.
You can set parameter in your controller and then check in Thymeleaf if that set, then include or replace your fragment. like this:
Controller
model.addAttribute("isFragmentBodyShow", true);
View
<th:block th:if="${isFragmentBodyShow}" th:include="fragments/header :: body"></th:block>
You can also send fragment name from contoller to Thymeleaf and use it in th:include for dynamic template include.

Passing a list generated with Javascript to a Spring controller

My app is being built with Spring Boot using a MVC pattern, and as template viewer I use Thymeleaf.
I'm generating a dynamic list with Javascript in a form, which I need to collect as a List with the controller.
I have tried to solve it with a #RequestParam, but generating the list with Javascript, as far as I'm concerned, I can't set the Thymeleaf tags.
This is the list:
<ul id="addItemList">
<li class="list-group-item" id="group" name="group" value="Outdoors">Outdoors</li>
<li class="list-group-item" id="group" name="group" value="Entertainment">Entertainment</li>
</ul>
Any indication on which approach I should take, would be much appreciated.
Thanks in advance.
Create a Model having a List as property, and pass it as #ModelAttribute in your controller.
In the end I solved this issue with ajax. I had a button to add an element to the list, which was being made with Javascript. I added a jQuery $.post function to save the item, each time that a new one was added to the list by selecting that button. I didn't find the way to move the whole list from javascript, to the Spring Controller.
Follow the process:
Get the list from controller using ajax
Parse the data then generate li with the retrieved values and update the html of id addItemList

How to define a link in part of an i18n Thymeleaf / Spring message?

Using Spring / Thymeleaf i18n, I'd like to create a HTML paragraph message like "Click here", in which there is a link only for the word "here". What is the best way to do this?
The way I tried doesn't look nice and also results in a like break:
In messages.properties file:
error.generic.click=Click
error.generic.here=here
And in the HTML file:
<p th:text="#{error.generic.click}"></p><p><a th:text="#{error.generic.here}" th:href="#{/contact}"></a></p>
Answer
Your way seems okay to me. If you just want to fix the newline issue go ahead with the following one:
<p>
<span th:text="#{error.generic.click}"></span>
<a th:text="#{error.generic.here}" th:href="#{/contact}"></a>
</p>
The span will make "Click" stay on the same line as "here". However i'd just go for a link that say "Click here" instead of just "here".
For example in german you could say "Hier klicken". "Hier" would mean "here" and "klicken" would mean "click". The Problem is that the meaning for the words changed but the position didn't. You would end up with a link saying "klicken" instead of "Hier".
Not recommented
There is another approach, but it has some drawbacks. You could use:
<p th:utext="#{error.generic}"></p>
with the following messages.properties:
error.generic=Click here
The drawback on this one is that you can't use th:href anymore. I would not recomment this way. However this can be helpfull when using no th:* and just plain html tags. So i wanted to mention it.

Thymeleaf URI template JSP equivalent

In JSP I used to have a table and a link to a record using this syntax:
<td>${person.id}</td>
So I could click the link on a table to move to a person's details page. I would like to achieve the same result using Thymeleaf, but not quite sure how. So my question is: what is Thymeleaf equivalent?
<td th:text="${trip.hrPerson}"></td>
<td><a th:href="#{/remove/trip.id}"></a> ${trip.id} </td>
First example displays static value and it's ok, second is supposed to be a link but it fails.
In Thymleaf you make link as follows
<a th:href="#{'/person/' + $person.id}" th:text="${person.id}">My User id</a>

Blogger template: Style blog post based on label

I'm trying to change the style of a blog post (for instance change the title color), based on the labels associated to the post.
I'm a bit new to the templating, so I though I would be going to add a class with the label in the title <h3> element, and then add my CSS rules.
So I found this which would generate a proper list of labels separated by a space:
<b:loop values='data:post.labels' var='label'><data:label.name/> </b:loop>
However, it seems the validator does not let me add this inside the class attribute as follow:
<h3 class='post-title entry-title <b:loop values="data:post.labels" var="label"><data:label.name/> </b:loop>'>
From there, I found half the solution. Apparently, I should use expr:class instead of class as follow:
<h3 expr:class='"post-title entry-title " + data:list_of_labels'>
So now:
- How can I build this variable data:list_of_labels? (basically how to set a variable)
- Is there a full description of the template syntax somewhere?
- Is there another way to go around this?
Thanks,
JB
This should do it. Using XML entities allows you bypass the XML validation and move the Blogger functions to where you need them. Longer explanation here: http://www.karlhorky.com/2012/06/add-blogger-labels-to-post-as-css.html
<div class="post<b:if cond="data:post.labels"><b:loop values="data:post.labels" var="label"> <data:label.name></data:label.name></b:loop></b:if>">
<data:post.body>
</div>
There is no way to set variables in the blogger data xml, however you can set variables using javascript.
There are many pages on the blogger data xml. Google is your friend. For example this one.
You are on the right track: do a loop, use javascript to check for the combinations you want, change the style properties or load a css file dynamically.

Resources