请看下面的代码

<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;
    }


}

最佳答案

你有

<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中的各个元素。

09-06 02:13