Please have a look at the below code
<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>
I have a bean called PatientBean
. In a servlet, lot of PatientBean
s are populated and added to an ArrayList
. The above code shows how I am trying to access the ArrayList
in JSP and get the beans data inside of it.
Below is the Servlet, which forward the request to the JSP
request.setAttribute("patientBean", patientBeanList);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("patients.jsp");
requestDispatcher.forward(request, response);
However it really didn't work. Non of the data were displayed. What have I done wrong?
Below is the 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;
}
}
You have
<c:forEach var="patientBean" items="${requestScope['PatientBean']}">
but
request.setAttribute("patientBean", patientBeanList);
Note that attribute names are case sensitive. So ${requestScope['PatientBean']}
will not find the request attribute named patientBean
.
Because you previously had
<td><c:out value="${patientBean.dob}"/></td>
where patientBean
would actually refer to the patientBeanList
, you're going to want to rename a few things.
Your request attribute should be called patientBeans
.
request.setAttribute("patientBeans", patientBeanList);
You can then access it
<c:forEach var="patientBean" items="${patientBeans}">
Your var
now allows you to use ${patientBean}
to refer to the individual elements in items
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.