如何填写Thymeleaf + Spring中的List <string>字段

时间:2018-05-23 07:44:09

标签: spring spring-mvc spring-boot thymeleaf

这是我的表格

            <!--destinationList List-->
               <div class="form-group">
                <div class="col-sm-3">
                    <label for="Destination">Destination</label>
                </div>

                <div class="col-sm-9">
                    <input type="text" class="form-control" th:field="*{destinationList[0]}" />
                    <input type="text" class="form-control" th:field="*{destinationList[1]}" />
                    <input type="text" class="form-control" th:field="*{destinationList[2]}" />
                    <input type="text" class="form-control" th:field="*{destinationList[3]}" />
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-9">
                    <button type="submit" class="btn btn-primary btn-block">Calculate</button>
                </div>
            </div>

        </form>

我将填写以下模型

public class PriceSearchDTO {

    private List<String> destinationList;

    public List<String> getDestinationList() {
        return destinationList;
    }
    public void setDestinationList(List<String> destinationList) {
        this.destinationList = destinationList;
    }

}

我能做到这一点。但是我在视图中硬编码了列表中的输入字段数。我需要动态地对它们进行动态化,并使列表中的元素数量是空中的。

2 个答案:

答案 0 :(得分:0)

试试这个:

<div class="col-sm-9">
    <input type="text" class="form-control" th:each="destination : ${destinationList}" th:field="*{destination}" />
</div>

答案 1 :(得分:0)

您在Thymeleaf中使用迭代。事实上,有一套非常完整的对象可以被 th:each 属性迭代。

像这样:

import hazelcast, logging
from time import sleep

config = hazelcast.ClientConfig()
config.network_config.addresses.append('localhost:5701')                                    


logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)

client = hazelcast.HazelcastClient(config)
my_map = client.get_map("map-name").blocking()

my_map.put("key_1", "value_1")
value = my_map.get("key_1")
print("Get val :"+value)
print("map.contains_key", my_map.contains_key("key"))
print("map.get", my_map.get("key"))
print("map.size", my_map.size())
print("map.putcount", my_map.get("putCount"))

client.shutdown()

Spring框架中的Controller。

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

  <head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="../../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
  </head>

  <body>

    <h1>Product list</h1>

    <table>
      <tr>
        <th>NAME</th>
        <th>PRICE</th>
        <th>IN STOCK</th>
      </tr>
      <tr th:each="destination: ${destinations}">
        <td th:text="${destination}">Onions</td>
      </tr>
    </table>

    <p>
      <a href="../home.html" th:href="@{/}">Return to home</a>
    </p>

  </body>

</html>