Spring MVC form:select selected value?
Asked Answered
B

7

14

Is there any way to select current value in dropdown list by Spring MVC by <form:options>?

Bezel answered 26/8, 2011 at 9:4 Comment(1)
question is unclear. what is 'current value'. Where do you want to select it?Less
C
7

No need to use if else

Example:

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 Code

modelAttribute & commandName are interchangeable

<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>
Cynic answered 18/4, 2015 at 14:45 Comment(1)
I think this will work when we have the options on Map of two type . But this will not work for list of object , example List<Gender> .Stacystadholder
H
6

Sets 'selected' as appropriate based on bound value.

http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html#spring-form.tld.options

Hairdo answered 2/11, 2011 at 16:35 Comment(0)
B
6

Here's my fix to this problem. Its massive, but it works

genders: qualifier from java model

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

account: binded modelattribute for spring forms

fmt:message: translates m to "Mees" (Estonian)

<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>
Brandling answered 27/1, 2012 at 11:50 Comment(0)
S
3

Try this, it works for me

<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>
Sophistic answered 10/4, 2014 at 2:17 Comment(2)
Thanks - I've been trying to figure this out all day!Stinkwood
Btw, an advantage to this approach is you can show multiple selections.Stinkwood
H
2

I had similar problem and after several days of battling with it, I was able to fix it by implementing hash and equal methods in my model class. The problem is that spring was not able to determine where an item in the drop down is equals to a value in the model. But after implementing the hash and equals in the model object, everything worked fine.

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

And in the view i have something like this

    <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>
Hett answered 25/8, 2015 at 19:46 Comment(0)
G
0

prior to binding the form to the model, you should set whatever variable you want to be selected to the desired value in the controller of your jsp.

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

When the form is built in the jsp, that form variable will be the default selected value in your options list.

Gloomy answered 6/9, 2013 at 8:42 Comment(0)
R
0

Need to set both these attributes itemLabel and itemValue to preselect the value eg: <form:options items="${subCategoryList}" itemLabel="name" itemValue="id"/> The name and ids are the fields from the SubCategory object and subCategoryList is List<Subcategory>

Rumpf answered 7/11, 2022 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.