【发布时间】: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