Java Spring MVC表单:单选按钮标记未设置值属性
以下是我的控制器的相关代码:Java Spring MVC表单:单选按钮标记未设置值属性,java,jsp,spring-mvc,enums,radio-button,Java,Jsp,Spring Mvc,Enums,Radio Button,以下是我的控制器的相关代码: @ModelAttribute("store_location_types") public StoreLocationType[] getStoreLocationTypes() { return StoreLocationType.values(); } private enum StoreLocationType { PHYSICAL("Physical"), ONLINE("Online"); private String
@ModelAttribute("store_location_types")
public StoreLocationType[] getStoreLocationTypes() {
return StoreLocationType.values();
}
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;
}
}
以下是在同一控制器中定义的StoreLocationType的定义:
@ModelAttribute("store_location_types")
public StoreLocationType[] getStoreLocationTypes() {
return StoreLocationType.values();
}
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>
物理的
在线 的
输入标记的value属性应该是StorLocationType.ONLINE.getValue()的值。我使用多个radiobutton标记而不是单个radiobuttons标记解决了这个问题 以下是JSP的代码:
<c:forEach var="item" items="${store_location_types}">
<form:radiobutton path="StoreLocationType" value="${item.value}"/>${item.displayName}
</c:forEach>
${item.displayName}
我使用数据库中的普通对象,所以在我的例子中,值是Id,显示的字符串是描述。但这也适用于enum。我使用多个radiobutton标记而不是单个radiobuttons标记解决了这个问题 以下是JSP的代码:
<c:forEach var="item" items="${store_location_types}">
<form:radiobutton path="StoreLocationType" value="${item.value}"/>${item.displayName}
</c:forEach>
${item.displayName}
我使用数据库中的普通对象,所以在我的例子中,值是Id,显示的字符串是描述。但这也适用于enum。我在您的代码中找不到任何问题。当我测试时,它运行良好 但是在这种情况下,你可以用一种更简单的方法来做。您不需要在枚举中添加
值
字段。如果省略
的itemValue
属性,Spring将把枚举实例的名称放在itemValue
属性的值中
所以你可以这样做
枚举
JSP
位置类型:
我在您的代码中找不到任何问题。当我测试时,它运行良好
但是在这种情况下,你可以用一种更简单的方法来做。您不需要在枚举中添加值
字段。如果省略
的itemValue
属性,Spring将把枚举实例的名称放在itemValue
属性的值中
所以你可以这样做
枚举
JSP
位置类型: