复选框中 th:field 属性的值 [英] Values for th:field attributes in checkbox

查看:34
本文介绍了复选框中 th:field 属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自数据库的数据表(动态插入).在其中一栏中插入复选框.现在我想选择其中一个并发送到下一个表单(我选择一个产品并将属性发送到另一个表单.在这个表单中应该只显示选择产品的属性).但我不知道在 th:field="*{}" 中插入什么样的值.我尝试了很多解决方案,但没有奏效.我的 html 表单包含所有产品表:

<form action="/oferta/zamow" th:action="@{/oferta/zamow}"th:object="${oferta}" method="post"><table border="1" id="display-data"><tr><td>#</td><td>标题</td><td>作者</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></表单>

显示所选产品的表格:

<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></表单>

感谢您的帮助.

解决方案

我不确定这是否是您想要的答案,但您可以在 http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields.

这里有一个简单的例子来说明如何在具有 Spring MVC 的 Thymeleaf 中使用复选框.

控制器:

@RequestMapping(value = "/showForm", method=RequestMethod.GET)公共字符串showForm(模型模型){列表<字符串>allItems = new ArrayList();allItems.add("value1");allItems.add("value2");allItems.add("value3");model.addAttribute("allItems", allItems);foo foo = new foo();列表<字符串>checkItems = new ArrayList();//默认情况下会检查 value1.checkItems.add("value1");foo.setCheckedItems(checkedItems);model.addAttribute("foo", foo);...}@RequestMapping(value = "/processForm", method=RequestMethod.POST)public String processForm(@ModelAttribute(value="foo") Foo foo) {//获取选中项的值.列表<字符串>checkItems = foo.getCheckedItems();...}

html:

<div th:each="item : ${allItems}"><input type="checkbox" th:field="*{checkedItems}" th:value="${item}"/><label th:text="${item}">example</label>

<输入类型=提交"/></表单>

Foo.java:

公共类 Foo {私人列表<字符串>检查项;公共列表<字符串>getCheckedItems() {返回checkedItems;}public void setCheckedItems(List checkedItems) {this.checkedItems=checkedItems;}}

希望这会有所帮助.

I have table with datas from database (insert dynamically). In one column I insert checkbox. Now I want to select one of them and send to next form (I select one product and send properties to another form. In this form should be displayed properties only the select product). But I don't know what kind of value insert in th:field="*{}". I tried many solutions but doesn't work. My html form with all products 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 to display select 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 help.

解决方案

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

Here is a simple example to illustrate how to use a checkbox in Thymeleaf with 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.

这篇关于复选框中 th:field 属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆