【问题标题】:Spring MVC form:radiobuttons tag not setting value attributeSpring MVC 表单:radiobuttons 标签未设置值属性
【发布时间】:2013-06-19 18:45:55
【问题描述】:

这是来自我的控制器的相关代码:

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

输入标签的value属性应该是StorLocationType.ONLINE.getValue()的值

【问题讨论】:

    标签: java jsp spring-mvc enums radio-button


    【解决方案1】:

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

    但在这种情况下,您可以以更简单的方式进行操作。您不需要在枚举中添加 value 字段。如果省略&lt;form:radiobuttons&gt;itemValue 属性,Spring 会将枚举实例的名称作为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" />
    

    【讨论】:

      【解决方案2】:

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

      JSP 代码如下:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-31
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-09
        • 2015-10-16
        • 2022-01-22
        相关资源
        最近更新 更多