【问题标题】:Unable to access beans using JSTL无法使用 JSTL 访问 bean
【发布时间】:2015-04-04 07:10:04
【问题描述】:

请看下面的代码

<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 中,许多PatientBeans 被填充并添加到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;
    }


}

【问题讨论】:

  • @SotiriosDelimanolis:错误。在 JSP 页面中?没有在那个页面上,这就是我得到的全部..
  • 那么你有一个名为PatientBean的请求属性吗?
  • @SotiriosDelimanolis:请检查已编辑的问题,..
  • 请注意,属性名称区分大小写。您可能应该调用属性patientBeans(复数),在这种情况下,您可以使用items="${patientBeans} 并拥有var="patientBean"。
  • @SotiriosDelimanolis:太棒了..请提供答案..

标签: java jsp servlets arraylist jstl


【解决方案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 中的各个元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2013-08-12
    相关资源
    最近更新 更多