在Thymeleaf的下拉列表中使用HashMap

Aya*_*was 2 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;
}
Run Code Online (Sandbox Code Playgroud)

现在我想用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>
Run Code Online (Sandbox Code Playgroud)

百里香的相当于什么呢?

geo*_*and 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>
Run Code Online (Sandbox Code Playgroud)