预选从下拉列表中选择价值

时间:2015-11-11 03:05:29

标签: spring-mvc thymeleaf

我能够填充我的下拉列表,但是我无法根据来自DB的值预先选择下拉列表的值。

我的Thymeleaf

   <select id="inputstatus" name="status" th:field="*{status}" >            
      <option th:each="enumStatus : ${listStatus}"    
           th:value="${enumStatus.code}" 
           th:text="#{${enumStatus.value}}" />
    </select>

我的控制器:

@RequestMapping(value = "userdetails/{username}")
public String getAccounts(@PathVariable String username, Model model) {
    Account accountDetail = rsAccountDetailsService.loadAccountByUserName(username);
    model.addAttribute(ACCNTSEARCH_MODEL_ACCNTSTATUS_KEY, AccountDetailsStatus.values());
    model.addAttribute("userdetailform",accountDetail);


    return "account/userdetails";

}

我的ENUM

public enum AccountDetailsStatus {
ACTIVE(0, "status.active"),
EXPIRED(2, "status.expired"),
LOCKED(3, "status.locked");

private int code;
private String value;

private final static class BootstrapSingleton {
    public static final Map<String, AccountDetailsStatus> lookupByValue = new HashMap<String, AccountDetailsStatus>();
    public static final Map<Integer, AccountDetailsStatus> lookupByCode = new HashMap<Integer, AccountDetailsStatus>();

}

AccountDetailsStatus(int code, String value) {
    this.code = code;
    this.value = value;
    BootstrapSingleton.lookupByValue.put(value, this);
    BootstrapSingleton.lookupByCode.put(new Integer(code), this);

}

public int getCode() {
    return code;
}


public String getValue() {
    return value;
}

}

如果用户详细信息加载了例如“ACTIVE”状态,则不会选择下拉列表中的活动状态。

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找类似的东西:选择标签

<option th:each="i : ${#numbers.sequence(0, 23)}" th:value="${i}" th:text="${i}" th:selected="${ i==9 } ">Options</option>

我在这里链接了一些类似的帖子 http://forum.thymeleaf.org/th-selected-not-working-on-multiple-select-td4025883.html

th:selected a number in a select/option with Thymeleaf doesn't work