为模型和视图传递多个值

问题描述 投票:0回答:2

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

弹簧控制器:-

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

问题:-

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

java spring jsp modelandview
2个回答
0
投票

Spring 表单可以解决这个问题。从控制器和 jsp 中传递列表,

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

它将填充两个单选按钮,即来自

genderList
值。请阅读此处的文档


0
投票

如何通过model和view发送多个数据

@控制器 公共类 HomeController {

@RequestMapping("/")
public String home()
{
    return "first";
}

@RequestMapping("/second")
public ModelAndView second(@RequestParam("firstname") String thename
        ,@RequestParam("lastname") String thelast)
{
    ModelAndView model = new ModelAndView();
    model.addObject("firstname", thename.toUpperCase());
    model.addObject("lastname", thelast.toUpperCase());
    model.setViewName("second");
    
    return model;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.