2013-07-17 16 views
4

我有来自数据库的数据表(动态插入)。在一列中插入复选框。现在我想选择其中一个并发送到下一个表单(我选择一个产品并将属性发送到另一个表单,在此表单中只应显示属性)。但我不知道在th中插入什么样的值:field =“* {}”。我尝试了很多解决方案,但不起作用。我的HTML表单与所有的产品表:复选框中th:字段属性的值

<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 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> 


感谢您的帮助。

回答

2

看看百里香春季整合文档。

所有th:字段都映射到命令对象。这就是为什么你需要* {}表达式。

模板引擎无法做到的一件事(还)是直接在循环内映射字段。所以你不能使用* {}方法来引用循环中的produkt变量。

你所要做的就是使用th的索引:每个表达式并为该索引构建一个具有预先计算表达式的属性存取器。

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

你不需要th:值,th:字段正在照顾它。 (除非你想取代它)

7

我不确定这是你寻求的答案,但你可以在http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields找到一个例子。

下面是一个简单的例子,说明如何在Spring MVC中使用Thymeleaf中的复选框。

控制器:

@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; 
    } 
} 

希望这有助于。