Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC form:select selected value?

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

like image 436
zaxx Avatar asked Aug 26 '11 09:08

zaxx


1 Answers

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>
like image 193
Timeless Avatar answered Sep 17 '22 12:09

Timeless