1

Here is relevant code from my controller:

@ModelAttribute("store_location_types")
public StoreLocationType[] getStoreLocationTypes() {
    return StoreLocationType.values();
}

Here is the definition of StoreLocationType, defined in the same controller:

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;
    }
}

Here is the the relevant JSP code:

        <li>
            <label>Location Type:</label>
            <form:radiobuttons path="StoreLocationType" items="${store_location_types}" itemLabel="displayName" itemValue="value"/>
        </li>

Here is what gets generated when the page is rendered:

    <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>

The value attributes are not being populated with the "value" field of my enum. What am I doing wrong here? What I would expect to see is this:

        <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>

The value attribute of the input tag should be the value of StorLocationType.ONLINE.getValue()

4

2 回答 2

1

我在您的代码中找不到任何问题。当我测试时,它运行良好。

但是在这种情况下,您可以以更简单的方式进行操作。您不需要value在枚举中添加该字段。如果省略 Spring 的itemValue属性,<form:radiobuttons>则将枚举实例的名称作为itemValue属性的值。

所以你可以这样做。

枚举

private enum StoreLocationType {
    PHYSICAL("Physical"),
    ONLINE("Online");

    private String displayName;

    private StoreLocationType(String displayName) {
        this.displayName = displayName;
    }

    public String getDisplayName() {
        return this.displayName;
    }
}

JSP

<label>Location Type:</label>
<form:radiobuttons path="StoreLocationType" 
    items="${store_location_types}" itemLabel="displayName" />
于 2015-10-22T07:44:11.190 回答
0

我使用多个单选按钮标签而不是单个单选按钮标签解决了它。

这是JSP的代码:

<c:forEach var="item" items="${store_location_types}">
    <form:radiobutton path="StoreLocationType" value="${item.value}"/>${item.displayName}
</c:forEach>

我使用数据库中的普通对象,所以在我的情况下,值是 Id,显示的字符串是描述。但这也应该适用于枚举。

于 2014-02-19T16:50:06.567 回答