【问题标题】:I have to render @embedded field in jsp我必须在jsp中渲染@embedded字段
【发布时间】:2018-01-23 14:36:57
【问题描述】:

我在 tomcat 8.5 上部署了应用程序。使用spring mvc,休眠。我的应用应该呈现一些关于学生等的信息。

这是我的POJO学生

@Embedded
private Address homeAddress;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "student")
private List<Exam> exams;

public Student() {
    super();
}

public Student(String firstname, String lastname, Calendar dateOfBirth, Address address) {

    super(firstname, lastname, dateOfBirth);
    this.homeAddress = address;

}   

public void setAddress(Address address) {
    this.homeAddress = address;
}

public void setFirstname (String firstname) {
    super.setFirstname(firstname);
}

public void setLastname (String lastname) {
    super.setLastname(lastname);
}   

public void setExams(List<Exam> exams) {
    this.exams = exams;
}

public Address getAddress() {
    return homeAddress;
}

public List<Exam> getExams() {
    return exams;
}

public String getFirstname () {
    return super.getFirstname();
}

public String getLastname () {
    return super.getLastname();
}   

public Calendar getDateOfBirth () {
    return super.getDateOfBirth();
}

地址(嵌入到学生中)

 @Column(name = "country")
  private String country;

  @Column(name = "city")
  private String city;

  @Column(name = "address")
  private String address;

  public Address () {   }

  public Address (String country, String city, String address) {
        this.country = country;
        this.city = city;
        this.address = address;
  }

  public String getCountry() {
    return country;
  }    

  public void setCountry(String country) {
    this.country = country;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "Address [country=" + country + ", city=" + city + ", address=" + address + "]";
  }

学生控制器

@Autowired
private StudentService studentService;

private ModelAndView model;
private List<Student> list;
private Student student;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

@GetMapping("/students")
public ModelAndView getCustomers(){
    model  = new ModelAndView("students");
    list = studentService.getAll();
    model.addObject("students", list);
    return model;
}

@RequestMapping(value="/addStudent", method = RequestMethod.POST)
public ModelAndView addStudent (HttpServletRequest request, HttpServletResponse response) throws ParseException {

    model = new ModelAndView("students");

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    Calendar dateOfBirth = Calendar.getInstance();
    dateOfBirth.setTime(sdf.parse(request.getParameter("dateOfBirth")));
    String country = request.getParameter("country");
    String city = request.getParameter("city");
    String address = request.getParameter("address");

    list = studentService.getAll();
    student = studentService.add(new Student(firstname, lastname, dateOfBirth, new Address(country, city, address)));
    model.addObject("students", list);
    return model;       
}   

@GetMapping("/editStudent/{id}")
public ModelAndView getEditStudentForm(@PathVariable int id) {

    model = new ModelAndView("editStudent");
    student = studentService.getById(id);
    model.addObject("student", student);
    return model;

}

@PostMapping("/editStudent")
public ModelAndView editStudent (HttpServletRequest request, HttpServletResponse response) throws ParseException {

    model = new ModelAndView("redirect:students");

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    Calendar dateOfBirth = Calendar.getInstance();
    dateOfBirth.setTime(sdf.parse(request.getParameter("dateOfBirth")));
    String country = request.getParameter("country");
    String city = request.getParameter("city");
    String address = request.getParameter("address");

    student = studentService.updateObject(new Student(firstname, lastname, dateOfBirth, new Address(country, city, address)));      
    list = studentService.getAll();
    model.addObject("students", list);

    return model;

}   

@PostMapping("/deleteStudent/{id}")
public ModelAndView deleteStudent (HttpServletRequest request, HttpServletResponse response, @PathVariable int id) {

    model = new ModelAndView("redirect:students");
    studentService.delete(id);
    list = studentService.getAll();
    model.addObject("students", list);
    return model;

}

我从 DB (mysql) 获取地址字段并尝试使用 forEach 循环在 jsp 上呈现它们。但是抛出异常

javax.el.PropertyNotFoundException: Property [country] not found on type [model.Student]

forEach 循环是

<table class="table table-striped table-hover">
<thead>
  <tr>
    <td> # </td>
    <td> firstname </td>
    <td> lastname </td>
    <td> date of birth </td>
    <td> country </td>
    <td> city </td>
    <td> address </td>
    <td> Edit </td>
  </tr>
</thead>
<tbody>
  <c:forEach var="student" items="${students}">      
    <tr>
        <td> ${student.id} </td>
        <td> ${student.firstname} </td>
        <td> ${student.lastname} </td>
        <td> ${student.dateOfBirth} </td>
        <td> ${student.country} </td>
        <td> ${student.city} </td>
        <td> ${student.address} </td>           
        <td>
        <a href = "<%= contextPath %>/students/editStudent/${student.id}" class="btn btn-primary btn-xs"> Edit student </a>
        <a href = "<%= contextPath %>/students/deleteStudent/${student.id}" class = "btn btn-danger btn-xs text-danger"> Erase student </a>
    </td>
    </tr>      
  </c:forEach>
</tbody>

删除 Address POJO 等解决方案不适合。 因此,解释如何在 jsp 上呈现这些字段。提前致谢。

【问题讨论】:

    标签: java hibernate jsp spring-mvc


    【解决方案1】:

    您的&lt;td&gt; ${student.country} &lt;/td&gt; 行指的是学生班级的一个属性国家,而您的班级学生没有。

    由于国家/地区包含在学生的 homeAddress 中

    &lt;td&gt; ${student.address.country} &lt;/td&gt;

    应该可以。

    在旁注中,@Embedded 与问题无关,您只是将 POJO 弄错了。

    更新

    真正的问题是属性名称与 setter/getter 名称的名称不匹配(参见 cmets)

    【讨论】:

    • tom,我忘了说 ${student.address.country} 也不起作用。
    • 好的,不确定是否需要(离开 jsp 有一段时间了),但尝试让 setter/getter 匹配属性名称。所以我会尝试将Student.getAddress 重命名为Student.getHomeAddress。如果这不起作用,您收到的错误消息是什么?
    • 尝试让 setter/getter 匹配属性名称。这确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2017-09-23
    • 2022-10-08
    • 2011-05-02
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多