I have a Java Object that I use part of the ModelandView as below,
Spring Controller:-
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
Java Object:-
import java.util.List;
public class Student {
private Integer age;
private String fname;
private String mname;
private String lname;
private String dob;
private List gender;
private String birthplace;
private String nationality;
private String mothertongue;
private String religion;
private Integer id;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public List getGender() {
return gender;
}
public void setGender(List gender) {
this.gender = gender;
}
public String getBirthplace() {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public String getMothertongue() {
return mothertongue;
}
public void setMothertongue(String mothertongue) {
this.mothertongue = mothertongue;
}
public String getReligion() {
return religion;
}
public void setReligion(String religion) {
this.religion = religion;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
JSP file
<h2>Student Information</h2>
<form:form method="POST" action="/SpringMVC/addStudent">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
Question:-
I need to pass a list for sex ie; MALE and FEMALE to the JSP File...I can add it as a array in my Java object but how do I reference in my JSP file..
Spring form takes care of it . pass the list from the controller and in jsp ,
<form:radiobuttons path="sex" items="${genderList}" />
it will populate two radio buttons namely from the genderList
values. Read the documentation here.