Th: field attribute values ​​in checkbox

I have a table with data from a database (dynamically insert). In one column, I check the box. Now I want to select one of them and submit it to the next form (I select one product and submit the properties to another form. This form should only display properties for the selected product). But I don’t know what value the th: field = "* {}" value inserts. I tried many solutions but did not work. My html form with the whole product table:

<form action="/oferta/zamow" th:action="@{/oferta/zamow}" th:object="${oferta}" method="post"> <table border="1" id="display-data"> <tr> <td>#</td> <td>title</td> <td>author</td> <td>rok</td> <td>cena</td> <td></td> </tr> <tr th:each="produkt, pozycja : ${oferta}"> <td th:text="${pozycja.count}"></td> <td> <span th:text="${produkt.tytul}" ></span> </td> <td> <span th:text="${produkt.autor}"></span> </td> <td> <span th:text="${produkt.rok}"></span> </td> <td> <span th:text="${produkt.cena}"></span> </td> <td> <input type="submit" value="zamow" /> <!-- <a th:href="@{/zamowienie}" >zamow</a>--> </td> <td> <label >zamow</label> <input type="checkbox" th:field="*{produkt}" th:value="${produkt}"/> </td> </tr> </table> </form> 


Form for displaying the selected product:

  <form action="/zamowienie/zam" th:action="@{/zamowienie/zam}" th:object="${zamowienie}" method="post"> <table border="1" id="display-data"> <tr align="center"> <td colspan="2"> twoje zamowienie </td> </tr> <tr > <td > tytul </td> <td> <span th:text="${produkt.tytul}"></span> </td> </tr> <tr > <td> autor </td> <td> <span th:text="${produkt.autor}" ></span> </td> </tr> <tr > <td> rok </td> <td> <span th:text="${produkt.rok}"></span> </td> </tr> <tr> <td> cena </td> <td> <span th:text="${produkt.cena}"></span> </td> </tr> <tr> <td> data zlozenia zamowienia </td> <td> <span th:text="${datazam}"></span> </td> </tr> </table> </form> 


Thanks for the help.

+4
source share
2 answers

I am not sure if this is your answer, but you can find an example at http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields .

Here is a simple example illustrating the use of a checkbox in Thymeleaf using Spring MVC.

Controller:

 @RequestMapping(value = "/showForm", method=RequestMethod.GET) public String showForm(Model model) { List<String> allItems = new ArrayList<String>(); allItems.add("value1"); allItems.add("value2"); allItems.add("value3"); model.addAttribute("allItems", allItems); Foo foo = new Foo(); List<String> checkedItems = new ArrayList<String>(); // value1 will be checked by default. checkedItems.add("value1"); foo.setCheckedItems(checkedItems); model.addAttribute("foo", foo); ... } @RequestMapping(value = "/processForm", method=RequestMethod.POST) public String processForm(@ModelAttribute(value="foo") Foo foo) { // Get value of checked item. List<String> checkedItems = foo.getCheckedItems(); ... } 

HTML:

 <form action="#" th:action="@{/processForm}" th:object="${foo}" method="post"> <div th:each="item : ${allItems}"> <input type="checkbox" th:field="*{checkedItems}" th:value="${item}" /> <label th:text="${item}">example</label> </div> <input type="submit" /> </form> 

Foo.java:

 public class Foo { private List<String> checkedItems; public List<String> getCheckedItems() { return checkedItems; } public void setCheckedItems(List<String> checkedItems) { this.checkedItems = checkedItems; } } 

Hope this helps.

+7
source

Take a look at the spring thimeleaf integration docs.

All th: the field maps to the command object. That is why you need the expression * {}.

One thing that the template engine cannot do (yet) is to directly display the fields inside the loop. Therefore, you cannot use the * {} method to refer to the produkt variable from a loop.

What you need to do is use the th index: each expression and create an accessory of properties with a pre-evaluated expression for the index.

 <input type="checkbox" th:field="*{produkts[__${index}__].checked" /> 

You do not need the field th: value, th: it takes care of this. (Except you want to replace it)

+2
source

Source: https://habr.com/ru/post/1491792/