在jsp Spring框架中引用路径变量

时间:2014-10-16 04:03:50

标签: java spring jsp

我传递了几个对象" Studentdetails"和字符串 - " dept"到JSP。

问题 -

我如何在JSP文件中引用studentdetail.st.fname?即;需要从Student对象中引用firstname。

我试过这个并且失败了 -

<form:label path="s.studentdetail.st.fname" class="labels">First Name</form:label>
AND 
<form:label path="studentdetail.st.fname" class="labels">First Name</form:label>



   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
       Map<String, Object> model = new HashMap<String, Object>();
       model.put("studentdetail", new Studentdetails());
       model.put("department", "dept");
       return new ModelAndView("student", "s", model);
  }

Studentdetails:

public class Studentdetails {
    public ContactInfo getCi() {
        return ci;
    }
    public void setCi(ContactInfo ci) {
        this.ci = ci;
    }
    public Student getSt() {
        return st;
    }
    public void setSt(Student st) {
        this.st = st;
    }
    ContactInfo ci;
    Student st;
}

学生:

public class Student {
    private Integer age;
    private String fname;
    private String mname;
    private String lname;
    private String dob;
    private String 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 String getGender() {
        return gender;
    }

    public void setGender(String 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;
    }


}

CONTACTINFO

public class ContactInfo {
    String addr1;
    String addr2;
    String city;
    String state;
    String pin;
    String country;
    String phone;
    String mobile;
    String email;
    public String getAddr1() {
        return addr1;
    }
    public void setAddr1(String addr1) {
        this.addr1 = addr1;
    }
    public String getAddr2() {
        return addr2;
    }
    public void setAddr2(String addr2) {
        this.addr2 = addr2;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getPin() {
        return pin;
    }
    public void setPin(String pin) {
        this.pin = pin;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

工作代码:---

<h2>Student Information</h2>
<form:form method="POST" action="/SpringMVC/addStudent" modelAttribute="studentdetail">

    <br><br>
    <form:label path="st.fname" class="labels">First Name</form:label>
    <form:input path="st.fname" class="textbox" />

    <br><br>
    studentdetail:${studentdetail.st.fname}
    <br>
    Department:${department}
    <br><br>

</form>

控制器: -

@RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
       Map<String, Object> model = new HashMap<String, Object>();
       model.put("studentdetail", new Studentdetails());
       model.put("department", "dept");
       return new ModelAndView("student",model);

3 个答案:

答案 0 :(得分:0)

尝试:

<form:label path="s['studentdetail'].st.fname" class="labels">First Name</form:label>

答案 1 :(得分:0)

如果您使用的是Studentdetails数组,则必须在modelAndAttribute字段的表单声明中使用该数组,然后使用foreach迭代所有人,并使用索引。

     <form:form
            id=""
            method="post"
           action=""
            modelAttribute="studentdetail">


    <form:input type="text"   id=""
        path="studentdetail.st.fname"/>

    </form:form>

答案 2 :(得分:0)

首先,构建ModelAndView至少是不常见的。您可以通过在字符串模型中添加包含实际模型的唯一属性s来添加不必要的间接级别。在JSP中,您必须使用${s["studentdetail"].st.fname} ...如果您已初始化studentdetail.st ...

恕我直言,您最好将方法更改为:

public ModelAndView student() {
   Map<String, Object> model = new HashMap<String, Object>();
   Studentdetails studentdetail = new Studentdetails());
   studentdetail = new Studentdetails());
   studentdetail.setSt(new Student()); //put a Student in studentdetail
   model.put("studentdetail", studentdetail);
   model.put("department", "dept");
   return new ModelAndView("student", model); // directly use model in ModelAndView

}

然后在JSP中,您只需使用${studentdetail.st.fname}