在Thymeleaf的下拉列表中使用HashMap

时间:2014-06-02 09:50:34

标签: spring-mvc thymeleaf

在我的控制器中,我正在设置一个hashmap

@ModelAttribute("clientImpMap")
public Map<String,String> populateClientImpMap() throws MalformedURLException, IOException 
{

    Map<String,String> clientImpMap = new HashMap<String,String> ();
    clientImpMap.put("1","High");
    clientImpMap.put("2","Low");
    return clientImpMap;
}

现在我想用Thymeleaf标签填充这个hashmap。怎么做? 使用核心Spring-mvc标签我可以做到这一点

<td>Client Importance :</td>
    <td><form:select path="personBean.clientImpRef">
            <form:option value="NONE" label="--- Select ---" />
            <form:options items="${clientImpMap}"  />
        </form:select></td>
    <td><form:errors path="personBean.clientImpRef" cssClass="error" /></td>
</tr>

在百里香叶中这相当于什么?

2 个答案:

答案 0 :(得分:8)

以下代码对应于您在问题中发布的带有spring标记的jsp。

<form action="your-controller-mapping" th:object="${personBean}">
   <select th:field="*{clientImpRef}">
      <option value="NONE">----Select----</option>
      <option th:each="entry : ${clientImpMap.entrySet()}" th:value="${entry.key}" th:text="${entry.value}">
        This will be replaced - is only used for natural templating
      </option>
   </select>
</form>

答案 1 :(得分:1)

以下是示例代码&amp;链路

th:each="item : ${items}" th:text="${item.description}"

Thymeleaf for each for select option & similar scenarios

<ul>
  <li th:each="item : ${items}" th:text="${item.description}">Item description here...</li>
</ul>