用thymeleaf用标签给页面select下拉框赋值怎么实现

如题所述

thymeleaf用标签给页面select下拉框赋值的实现方法:

1、定义type.java

public class Type {
   private Integer id;
   private String type;
  ...getters and setters
}

2、定义SeedStarterMngController.java,将来给select的option填充值:

@ModelAttribute("allTypes")
   public List<Type> populateTypes() {
       Type type1 = new Type();
       type1.setId(1);
       type1.setType("OUTDOOR");

       Type type2 = new Type();
       type2.setId(2);
       type2.setType("INDOOR");

       List<Type> tipos = new ArrayList<Type>();
       tipos.add(type1);
       tipos.add(type2);
       return tipos;
   }

3、填充方法实现页面seedstartermng.html:

<select th:field="*{type}">
       <option th:each="type : ${allTypes}" th:value="${type.id}" th:text="${type.type}">typeSelect</option>
</select>

4、实现效果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-06
如果你 th:field="*{paymentMethod}"的paymentMethod对象也是个list对象的话,multiple select也是直接使用就OK了。

但如果paymentMethod是个以,号分隔的字符串的话,则只能自己处理了下,thymeleaf还没找到怎么整呢,我的处理方式如下:

<select multiple="multiple" class="width-40 chosen-select" name="knowledgePointIdSet" data-placeholder="请选择课件分类...">

<option th:each="category:${knowPointList}" th:value="${category.id}"

th:selected="${#strings.contains(instance.knowledgePointIdSet,category.id)}"

th:text="${category.name}" >模块名称</option>

</select>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网