2016-11-14 44 views
0

我有一個HashMap,提供數據的某些文件:未指定Thymeleaf動態數據類型形式

Key | Value 
---------------- 
Name | test1 
Type | type1 
... 

行數和兩者鍵和值,都在第一串。

現在我想編輯這個動態數據。 Therfor我創造了這個模板:

<form action="#" th:action="@{/documents/{id}(id=${doc_id})}" th:object="${properties}" method="post"> 
    <h1 th:text="${document_h1_text}">Documents</h1> 
    <table class="table table-striped"> 
     <tr> 
      <th>Property</th> 
      <th>Value</th> 
     </tr> 
     <tr th:each="property : ${document_properties}"> 
      <td th:text="${property.key}">Property</td> 
      <td> 
       <input name="${property.key}" th:value="${property.value}" /> 
      </td> 
     </tr> 
    </table> 
    <button type="submit" class="btn btn-default">Submit</button> 
</form> 

document_properties關係到一個HashMap<String,String>

當我與

@RequestMapping(path = "documents/{doc_id}", method = RequestMethod.GET) 
public String document(@PathVariable long doc_id, Model model) { ... } 

得到這個Web服務能正常工作,現在我想編輯在此輸出前端並提交更改:

@PostMapping("documents/{doc_id}") 
public String editDocument(@PathVariable long doc_id, @ModelAttribute HashMap<String, String> properties,Model model) { ... } 

Unfort單擊HashMap properties是空的,當我打電話通過點擊「提交」按鈕。你們有沒有人知道如何解決這個問題?僅供參考:我故意選擇不在這裏使用傳統的類綁定。原因是我想得到一個動態的解決方案,使用任意類。

回答

1
  1. 你至少需要一個包裹地圖的包裝(我不認爲你可以直接綁定到一個地圖上,你希望它的工作方式)。
  2. 如果可能,應始終使用th:field代替th:name和th:value。

下面是一個應該按照您希望的方式工作的快速示例。

包裝

public class MapWrapper { 
    private Map<String, String> map = new HashMap<>(); 

    public Map<String, String> getMap() { 
     return map; 
    } 

    public void setMap(Map<String, String> map) { 
     this.map = map; 
    } 
} 

控制器

@Controller 
public class MapController { 
    @GetMapping("map.html") 
    public String get(Map<String, Object> model) throws Exception { 
     MapWrapper wrapper = new MapWrapper(); 
     wrapper.getMap().put("1", "One"); 
     wrapper.getMap().put("2", "Two"); 
     wrapper.getMap().put("3", "Three"); 

     model.put("wrapper", wrapper); 
     return "map"; 
    } 

    @PostMapping("map.html") 
    public String post(Map<String, Object> model, @ModelAttribute("wrapper") MapWrapper wrapper) throws Exception { 
     return "map"; 
    } 
} 

形式

<form action="map.html" th:object="${wrapper}" method="post"> 
    <table class="table table-striped"> 
     <tr> 
      <th>Property</th> 
      <th>Value</th> 
     </tr> 

     <tr th:each="key : ${wrapper.map.keySet()}"> 
      <td th:text="${key}" /> 
      <td> 
       <input th:field="*{map[__${key}__]}" /> 
      </td> 
     </tr> 
    </table> 

    <button type="submit" class="btn btn-default">Submit</button> 
</form> 
+0

非常感謝! – MS1

0

它出價過高的支持對象,並沒有似乎是一個不錯的方式,我想我們需要一些通用的模式,例如:

public class GenericEntity { 
protected Map<String, Object> properties = new LinkedHashMap<String, Object>(); 
//setter getter 
} 

其中entity extends GenericEntity 和控制器

<tr th:each=" key : ${wrapper.map.keySet()}"> 
     <td th:text="${key}" /> 
     <td> 
     <input th:field="*{properties[__${key}__]}" /> 
     </td> 
    </tr> 

,但你應該找到一種方法來設置屬性在超類映射,它是如此的硬編碼的,其他方式可以寫下thymleafe V3定製方言。