【问题标题】:How to fill List<string> field in Thymeleaf + Spring如何在 Thymeleaf + Spring 中填充 List<string> 字段
【发布时间】:2018-11-02 02:29:15
【问题描述】:

这是我的表格

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

}

我可以做到。但是我在视图中对列表中输入字段的数量进行了硬编码。我需要动态生成它们并使列表中的元素数量是空的。

【问题讨论】:

  • 你看答案了吗?

标签: spring spring-mvc spring-boot thymeleaf


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

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

    像这样:

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

    Spring框架中的Controller。

    @RequestMapping(value = "/")
        public ModelAndView showView(ModelAndView mav) {
    
        List<String> destinations = new ArrayList<String>();
        destinations.add("elementA");
        destinations.add("elementB");
        destinations.add("elementC");
    
        mav.addObject("destinations", destinations);
        mav.setViewName("/viewName");
    
        return mav;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多