can't reference iteration variable in Thymeleaf - java

I try to iterate a list of items using th:each,
<div class="row" th:each="item : *{items}">
<input type="text" th:field="item.name"/>
</div>
it works if I access the iteration variable using th:text, but throws
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'item' available as request attribute
when I use th:field to access it, where did I do wrong?

Something like this could work:
<div class="row" th:each="item, stat : *{items}">
<input type="text" th:field="*{items[__${stat.index}__].name}"/>
</div>
Take a peek here for more info: http://forum.thymeleaf.org/I-have-problem-in-binding-the-list-of-objects-contained-inside-a-object-on-the-form-using-thymeleaf-td3525038.html

th:field is broken in several ways, this is one of them.

Related

Thymeleaf Iteration: Use index as ArrayIndex inside th:each loop

I want to use the index of an thymeleaf-loop as arrayindex inside.
Example:
<div th:each="service : ${requestedServices}">
<div class="col-sm-1">
<input type="checkbox" th:checked=*{service[index of loop?].requested}">
</div>
</div>
I'll already tried to use serviceStat.index inside the array
${requestedServices} is an ArrayList of Requested Service
class RequestedService {
Service service
boolean requested
}
class Service {
String name;
int value;
}
I hope to find an answer for my problem
serviceStat.index is the correct way to to index into your array (see the last paragraph of keeping iteration status -- If you don’t explicitly set a status variable, Thymeleaf will always create one for you by suffixing Stat to the name of the iteration variable:).
That being said, because you are using it in a th:field attribute, you must use preprocessing for your index.
<div th:each="service : ${requestedServices}">
<div class="col-sm-1">
<input type="checkbox" th:checked="*{service[__${serviceStat.index}_].requested}" />
</div>
</div>
(You might also be interested in the Thymeleaf tutorial on dynamic fields.)

Spring and thymeleaf set and get value

I send object myObject to view, one of values is put in input inside form with post method. How is it possible to set value to view and then get it by submit this form (but with another object type)?
I tried something like:
<form action="#" th:action="#{/myMethod}" th:object="${anotherObject}" method="post" xmlns="http://www.w3.org/1999/html">
<input style="display:none" type="text" th:name="*{fieldFromAnotherObject}" th:value="${sendedObject.fieldFromSendedObject}"/>
but the result is that name field in this html element is empty:
(value is correct). And then I have null as fieldFromAnotherObject field of anotherObject in my backend.
I am not sure why you are using two objects one for binding values to view and one for submitting,if both are of same class one object would be sufficient.Using th:field you can achieve this with single object. Try below code it should work.
<form action="#" th:action="#{/myMethod}" th:object="${anotherObject}" method="post" >
<input style="display:none" type="text" th:field="*{fieldFromSendedObject}"/>
Just in case if both are of different class objects, try below code
<input style="display:none" type="text" th:name="anotherObject.fieldFromSendedObject" th:value="${sendedObject.fieldFromSendedObject}"/>

Thymeleaf insert text in html code?

I want to insert an Attribute into the html Code.
I tried this, but it's not working:
<div id="${var}"> ... </div>
I think you know what I mean. The attribute 'var' should be the id. I didn't find a solution...
You just need to use the th:attr attribute. It is explained in the reference documentation 5.1:
5.1 Setting the value of any attribute
Enter then the th:attr attribute, and its ability to change the value
of attributes of the tags it is set in:
<form action="subscribe.html" th:attr="action=#{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
The
concept is quite straightforward: th:attr simply takes an expression
that assigns a value to an attribute. Having created the corresponding
controller and messages files, the result of processing this file will
be:
<form action="/gtvg/subscribe">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="¡Suscríbe!"/>
</fieldset>
</form>
Use this
<div th:attr="id=${var}"> ... </div>
Thymeleaf only evaluates attributes that are prefixed with th:. Here is a list of the attributes that are evaluated:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes
In your case, th:id is already built in so you can simply do <div th:id="${var}"> ... </div> and it will work. th:attr, is used to define attributes that thymeleaf doesn't normally support.

How to generate edit modals for each element in the model?

I'm trying to use thymeleaf to generate edit and delete modals for each element in the model in my ModelAndView using th:each.
The modals are indeed created and have unique ids based on the id field of the elements. The problem I have is none of the values from elements are parsed into the inputs to enable the user to see the current values.
They are obviously there because the view also has a table which displays each element's values along with the anchors which toggle the modals.
Here's some example code of how I'm doing it:
<div th:each="f : ${foos}" th:id="um- + ${f.id}" class="modal fade"
tabindex="-1" role="dialog">
...
<form role="form" th:action="#{/foo/update}" th:object="${foo}" th:method="post">
<input type="hidden" th:field="*{id}" th:value="${f.id}"/>
<fieldset class="form-group">
<label for="bar">Bar</label>
<input th:field="*{bar}" th:value="${f.bar}" class="form-control"
id="bar" type="text" placeholder="Bar"/>
</fieldset>
...
</form>
...
</div>
How to generate edit modals for each element in the model? I'm not sure why thymeleaf is unable to get the values of the fields from the model elements.
That's not a great approach actually. In addition to it not working, doing using a loop obviously creates n modals for the collection.
The solution that worked best was to provide a single modal that would be populated and submitted with Ajax calls.
This no-frills Spring Boot app has all the relavant code.

value input field in foreach to input field outside foreach

The problem I have is best explained with a code example:
I have the following VIEW.jsp:
<c:forEach var="widget" items="${widgets}">
<div class="drag">
<p>Id: ${widget.id}</p>
<input class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget">
<input class="idWidget" type="hidden" value="${widget.id}">
</div>
</c:forEach>
<div id="editDialog" title="Edit widget">
<fieldset>
<input class="editWidgetId" type="hidden" value="??" id="editWidgetId">
</fieldset>
</div>
editWidgetId should have its 'value' attribute filled with the value of idWidget inside the foreach loop. This value should be different for each element in the loop (element is selected by edit button).
The questions:
How can I get the value of one input field to another input field?
How can I do this when a foreach loop is present?
Thanks in advance
Given you want dynamic behavior, this work has to be executed on the client-side by JavaScript as at the server-side, you have many widgets to one editDialog and are lacking the client-side user event to make your decision.
What you want to do is assign a function handler (or statement in my example below) to each editWidget to change the value of the editWidgetId input box with the appropriate value:
<input onclick="document.getElementById('editWidgetId').value = '${widget.id}'" class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget" >
Haven't tested this but I hope you get the idea

Categories