为Modelandview传递多个值

时间:2014-10-15 11:08:04

标签: java spring jsp modelandview

我有一个Java对象,我使用ModelandView的一部分,如下所示,

Spring Controller: -

@RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }

Java对象: -

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文件

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

问题: -

我需要传递性别列表,即; MALE和FEMALE到JSP文件...我可以在Java对象中将其添加为数组但是如何在我的JSP文件中引用..

1 个答案:

答案 0 :(得分:0)

Spring表单负责处理它。从控制器和jsp传递列表,

<form:radiobuttons path="sex" items="${genderList}" />

它将填充两个单选按钮,即来自genderList值。在这里阅读documentation