2015-11-25 25 views
13

db.htmlThymeleaf Mapa Formularz Oprawa

<div th:each="pr, stat: *{mergeMap}"> 
    <tr> 
     <td><input type="text" name="key" th:value="${pr.key}" /></td> 
     <td><input type="text" name="value" th:value="${pr.value}" /></td> 
    </tr> 
</div> 

dniu złożenia tego wejścia, zawsze dotrzesz mergeMap być pusty w kontrolerze wiosennym. Co należy zrobić, aby uzyskać wartość mergeMap?

Controller.java

@RequestMapping(value = "/shot") 
    public String saveMergeProducts(@ModelAttribute(value="prod") MergedProductInfoDTO prod, BindingResult bindingResult, 
      Model model, HttpServletRequest request) { 
     System.out.println(prod.toString()); 
     return "forward:/backoffice/db"; 
    } 

HTML

<form action="#" th:action="@{shot}" method="POST" th:object="${prod}"> 
      <tr> 
      <td><span th:text="${index.index}"></span></td> 
       <td><input type="text" name="id" th:value="*{id}" th:readonly="readonly" /></td> 
       <td><input type="text" name="categoryName" th:value="*{categoryName}" th:readonly="readonly" /></td> 
       <td><input type="text" name="subCategoryName" th:value="*{subCategoryName}" th:readonly="readonly" /></td> 
       <td><input type="text" name="productBrand" th:value="*{productBrand}" /></td> 
       <td><input type="text" name="productSubBrand" th:value="*{productSubBrand}" /></td> 
       <td><input type="text" name="series" th:value="*{series}" /></td> 
       <td><input type="text" name="model" th:value="*{model}" /></td> 
      </tr> 
     <tr> 
      <td colspan="7"> 
          <tr> 
           <th>KEY</th> 
           <th>VALUE</th> 
          </tr> 
          <div th:each="pr, stat: *{mergeMap}"> 
           <tr> 
            <td><input type="text" name="mergeMapKey" th:value="${pr.key}" /></td> 
            <td><input type="text" name="mergeMapValue" th:value="${pr.value}" /></td> 
           </tr> 
          </div> 
         </table> 
        </td> 
        <td><input type="text" name="tags" th:value="*{tags}" /></td> 
        <td><input type="submit" value="Submit" /></td> 
       </tr> 

Odpowiedz

4

Aby uzyskać dostęp do właściwości fasoli kształtowym kopii Map użyj __${...}__ preprocesor

<div th:each="pr, stat: *{mergeMap}"> 
    <tr> 
     <td><input type="text" name="value" th:value="${pr.key}" readonly="true"/></td> 
     <td><input type="text" name="value" th:field="*{mergeMap[__${pr.key}__]}"/></td> 
    </tr> 
</div> 

co robi ocenia wyrażenie wewnętrzne najpierw przed oceną całego wyrażenia sion. Zauważ, że w tym przypadku nie należy modyfikować ${pr.key}, aby aktualizacja została odzwierciedlona we właściwości mapy komponentu bean powiązanego z formularzem.

referencyjny: http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

+0

wartość mergeMap nie jest uzyskiwanie zaludnionych w interfejsie thymeleaf. –

+0

Mapa powinna być wstępnie wypełniona klawiszami. Klawisze te zostaną użyte do aktualizacji wartości na mapie. – Bnrdo

+0

mapa zawiera zarówno klucz, jak i wartość, w jaki sposób mogę również wypełnić jego wartości? –