這裏是我的控制器相關代碼:Spring MVC的形式:單選按鈕標籤未設定值屬性
@ModelAttribute("store_location_types")
public StoreLocationType[] getStoreLocationTypes() {
return StoreLocationType.values();
}
這裏是StoreLocationType的定義,在同一個控制器定義:
private enum StoreLocationType {
PHYSICAL("Physical"),
ONLINE("Online");
private String displayName;
private String value;
private StoreLocationType(String displayName) {
this.displayName = displayName;
this.value = this.name();
}
public String getDisplayName() {
return this.displayName;
}
public String getValue() {
return this.value;
}
}
這裏相關的JSP代碼:
<li>
<label>Location Type:</label>
<form:radiobuttons path="StoreLocationType" items="${store_location_types}" itemLabel="displayName" itemValue="value"/>
</li>
以下是頁面呈現時生成的內容:
<li>
<label>Location Type:</label>
<span>
<input id="StoreLocationType1" name="StoreLocationType" type="radio" value="">
<label for="StoreLocationType1">Physical</label>
</span>
<span>
<input id="StoreLocationType2" name="StoreLocationType" type="radio" value="">
<label for="StoreLocationType2">Online</label>
</span>
</li>
值屬性未使用我的枚舉的「值」字段填充。我在這裏做錯了什麼?我希望看到的是:
<span>
<input id="StoreLocationType1" name="StoreLocationType" type="radio" value="PHYSICAL">
<label for="StoreLocationType1">Physical</label>
</span>
<span>
<input id="StoreLocationType2" name="StoreLocationType" type="radio" value="ONLINE">
<label for="StoreLocationType2">Online</label>
</span>
輸入變量的值屬性應該是StorLocationType.ONLINE.getValue()的值