我必须上课: Student 和 Address ,具有 OneToMany 关系,所以每个学生都可以有一个或多个地址,
现在我尝试在 JSP 页面中显示给定的学生详细信息(其学生 + 其地址详细信息),但仅显示学生信息而不显示其地址。
这是 sevlet 页面:
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
public class TestServlet extends HttpServlet {
@EJB
private StudentDaoLocal studentDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String studentIdStr = request.getParameter("studentId");
if (studentIdStr != null && !studentIdStr.equals("")) {
int studentId = Integer.parseInt(studentIdStr);
Student student = studentActions(studentId);
request.setAttribute("student", student);
request.getRequestDispatcher("info.jsp").forward(request, response);
}
}
private Student studentActions(int studentId)
throws ServletException, IOException {
Student student;
student = studentDao.getStudent(studentId);
return student;
}
JSP 页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> Student Details</h1>
Student Details
<br>
<table border="1">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Year Level</th>
</tr>
<c:forEach>
<tr>
<td>${student.studentId}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.yearLevel}</td>
</tr>
</c:forEach>
</table>
</br>
Student'Adresse
<table border="1">
<tr>
<th>Street</th>
<th>City</th>
<th>Country</th>
</tr>
<h1>${student.addresses}</h1>
<c:forEach items="${student.addresses}" var="address">
<tr>
<td>${address.street}</td>
<td>${address.city}</td>
<td>${address.country}</td>
</tr> </c:forEach>
</table>
<br>
</body>
</html>
学生班:
@Entity
@Table
@NamedQueries({@NamedQuery(name="Student.getAll",query="SELECT e FROM Student e") })
public class Student implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private int studentId;
@Column
private String firstname;
@Column
private String lastname;
@Column
private int yearLevel;
@OneToMany(mappedBy="student", cascade=CascadeType.ALL)
private List<Address> addresses;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getYearLevel() {
return yearLevel;
}
public void setYearLevel(int yearLevel) {
this.yearLevel = yearLevel;
}
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public Student(int studentId, String firstname, String lastname, int yearLevel) {
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
}
public Student(int studentId, String firstname, String lastname, int yearLevel, List<Address> addresses) {
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
this.addresses = addresses;
}
public Student(){}
public void addAddress(Address address){
this.addresses.add(address);
if(address.getStudent() != this){
address.setStudent(this);
}
}
}
和地址类:
@Entity
@Table
public class Address implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private int addressId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="studentId")
private Student student;
@Column
private String street;
@Column
private String city;
@Column
private String country;
public Address(){}
public Address(int addressId, String street, String city, String country) {
this.addressId = addressId;
this.street = street;
this.city = city;
this.country = country;
}
public Address(int addressId, Student student, String street, String city, String country) {
this.addressId = addressId;
this.student = student;
this.street = street;
this.city = city;
this.country = country;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return street + ", " + city + ", " + country;
}
public boolean isEmptyAddress(){
return this.getStreet().isEmpty() && this.getCity().isEmpty() && this.getCountry().isEmpty();
}
public void updateAddress(Address address){
this.street = address.street;
this.city = address.city;
this.country = address.country;
}
这就是问题所在;地址部分未加载。
1: - 那么现在我如何打印 servlet 中的地址
那么现在如何在将学生对象传递给jsp之前首先检查地址是否正在加载?一位朋友告诉我,我可以在 servlet 中打印地址,但我不知道该怎么做。
2: - JSP 代码可以显示学生的地址吗?
(两者之间,我是 Java EE 世界的新手,所以......)