Spring MVC表单:选择所选值?

nkhmeac6  于 2022-11-14  发布在  Spring
关注(0)|答案(7)|浏览(388)

有没有办法通过Spring MVC <form:options>在下拉列表中选择当前值?

wbgh16ku

wbgh16ku1#

不需要使用if else

示例:

Student student = new Student();
student.gender = "F";
model.addObject("student", student);

Map<String, String> genders = new LinkedHashMap<String, String>();
genders.put("M", "Male");
genders.put("F", "Female");
model.addObject("genders", genders);

JSP代码
modelAttributecommandName可互换

<c:url value="/Student/Edit" var="editstudenturl"/>
<form:form method="post" action="${editstudenturl}" modelAttribute="student" class="form-horizontal">
  <form:select path="gender" class="form-control" id="gender" >
    <form:options items="${genders}" />
  </form:select>
</form:form>
oknwwptz

oknwwptz2#

根据绑定值将'selected'设置为适当值
http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html#spring-form.tld.options

6gpjuf90

6gpjuf903#

这是我解决这个问题的方法。它很庞大,但很有效

性别:Java模型中的限定符

// model.addAttribute("genders", genders);

× ×账号× ×:Spring表单的绑定模型属性

格式:消息:将m翻译为“Mees”(爱沙尼亚语)

<form:select path="cfGender">
<c:forEach var="item" items="${genders}">
    <c:choose>
        <c:when test="${account.getCfGender().getCfGender()==item.getCfGender()}">
            <form:option selected="true" value="${item.getCfGender()}">
                <fmt:message key="cf_gender.${item.getCfGender()}" />
            </form:option>
        </c:when>

        <c:otherwise>
            <form:option value="${item.getCfGender()}">
                <fmt:message key="cf_gender.${item.getCfGender()}" />
            </form:option>
        </c:otherwise>
    </c:choose>
</c:forEach>
</form:select>
xkftehaa

xkftehaa4#

试试这个,对我有用

<form:select path="size">
    <c:forEach items="${sizeList}" var="s" varStatus="status">
        <c:choose>
            <c:when test="${s eq 25}">
                <option value="${s}" selected="true">${s}</option>
            </c:when>
            <c:otherwise>
                <option value="${s}">${s}</option>
            </c:otherwise>
        </c:choose> 
    </c:forEach>
</form:select>
5uzkadbs

5uzkadbs5#

我也遇到过类似的问题,经过几天的努力,我在模型类中实现了hash和equal方法,解决了这个问题。问题是spring无法确定下拉列表中的某个项在哪里等于模型中的某个值。但是在模型对象中实现了hash和equals之后,一切都正常了。

@Entity
@Table(name = "BANKS")
public class Bank implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = -8928809572705999915L;

private Long id;

private String bankCode;

private String bankName;

...........

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((bankCode == null) ? 0 : bankCode.hashCode());
    result = prime * result
            + ((bankName == null) ? 0 : bankName.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Bank other = (Bank) obj;
    if (bankCode == null) {
        if (other.bankCode != null)
            return false;
    } else if (!bankCode.equals(other.bankCode))
        return false;
    if (bankName == null) {
        if (other.bankName != null)
            return false;
    } else if (!bankName.equals(other.bankName))
        return false;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

在我的视野中,我看到了这样的东西。

<form:select path="bank" cssClass="form-control"    required="true">
        <form:option value="">--Select--</form:option>
        <form:options items="${banks}" itemLabel="bankName"
                                        itemValue="bankCode" />
    </form:select>
t8e9dugd

t8e9dugd6#

在将表单绑定到模型之前,您应该在jsp的控制器中将您希望选择的任何变量设置为所需的值。

Form form = new Form();
    form.setFoo("bar");
    model.addAttribute("form", form);

在JSP中构建表单时,该表单变量将是选项列表中的默认选定值。

bxjv4tth

bxjv4tth7#

需要同时设置itemLabel和itemValue属性以预先选择值,例如:<form:options items="${subCategoryList}" itemLabel="name" itemValue="id"/> name和id是来自SubCategory对象的字段,subCategoryList是List<Subcategory>

相关问题