Как заполнить поле List <string>в Thymeleaf + Spring - PullRequest
0 голосов
/ 23 мая 2018

вот моя форма

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

}

Я могу это сделать.Но я жестко запрограммировал количество полей ввода в списке, как указано выше в представлении.Мне нужно динамически генерировать их и сделать количество элементов в списке «airbitary».

Ответы [ 2 ]

0 голосов
/ 23 мая 2018

Вы используете итерацию в Thymeleaf.На самом деле, существует довольно полный набор объектов, которые считаются итерируемыми атрибутом th: каждый .

Примерно так:

<!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рамки.

@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;
}
0 голосов
/ 23 мая 2018

Попробуйте это:

<div class="col-sm-9">
    <input type="text" class="form-control" th:each="destination : ${destinationList}" th:field="*{destination}" />
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...