请查看以下代码
<c:forEach var="patientBean" items="${requestScope['PatientBean']}">
<tr>
<td><img src="images/img1.jpg" width="30px" height="30px"/></td>
<td><c:out value="${patientBean.FirstName}"/><c:out value="${patientBean.MiddleName}"/><c:out value="${patientBean.LastName}"/></td>
<td><c:out value="${patientBean.dob}"/></td>
<td><c:out value="${patientBean.sex}"/></td>
<td><form name="form1" method="post" action="allergies.jsp">
<label>
<input type="submit" name="view" id="1" value="View">
</label>
<input name="idPatient" type="hidden" value=<c:out value="${patientBean.idPatient}"/>>
</c:forEach>
我有一个名为PatientBean
的bean。在servlet中,很多PatientBean
被填充并添加到ArrayList
。上面的代码显示了我如何尝试访问JSP中的ArrayList
并获取其中的bean数据。
下面是Servlet,它将请求转发给JSP
request.setAttribute("patientBean", patientBeanList);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("patients.jsp");
requestDispatcher.forward(request, response);
然而它确实没有用。没有显示数据。我做错了什么?
以下是PatientBean
/**
*
* @author Yohan
*/
public class PatientBean implements Serializable {
private int idPatient;
private String firstName;
private String middleName;
private String lastName;
private Date dob;
private String sex;
/**
* @return the idPatient
*/
public int getIdPatient() {
return idPatient;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the middleName
*/
public String getMiddleName() {
return middleName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the dob
*/
public Date getDob() {
return dob;
}
/**
* @return the sex
*/
public String getSex() {
return sex;
}
/**
* @param idPatient the idPatient to set
*/
public void setIdPatient(int idPatient) {
this.idPatient = idPatient;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @param middleName the middleName to set
*/
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @param dob the dob to set
*/
public void setDob(Date dob) {
this.dob = dob;
}
/**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
}
答案 0 :(得分:1)
你有
<c:forEach var="patientBean" items="${requestScope['PatientBean']}">
但
request.setAttribute("patientBean", patientBeanList);
请注意,属性名称区分大小写。因此${requestScope['PatientBean']}
将找不到名为patientBean
的请求属性。
因为你以前有过
<td><c:out value="${patientBean.dob}"/></td>
patientBean
实际上会引用patientBeanList
,您可能想要重命名一些内容。
您的请求属性应调用patientBeans
。
request.setAttribute("patientBeans", patientBeanList);
然后您可以访问它
<c:forEach var="patientBean" items="${patientBeans}">
您的var
现在允许您使用${patientBean}
来引用items
中的各个元素。