2

嗨,我是 JSP 和 JavaBean 的新手。我正在通过编写一个应用程序来练习,其中多个页面将共享一个 javabean 组件。页面“check.jsp”实例化了 bean 并设置了一个属性,没有任何错误。但是每当我尝试在另一个 jsp 中获取属性时survey.jsp,我都会收到错误消息

HTTP 状态 500 - file:/survey.jsp(16,15) jsp:getProperty 用于名称为“survey”的 bean。以前没有按照 JSP.5.3 引入名称

我已经仔细检查了 get 和 set 属性中的名称是否与 action 元素中的 bean id 完全相同。请我需要帮助

检查.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <jsp:useBean id="survey" scope="application" class="appScope.SurveyBean"/>
        <jsp:setProperty name="survey" property="quantity" value='<%= request.getParameter("title")%>' />

        <form action="/appScope/survey.jsp" method="POST">
            <h3> Thanks for your input</h3>
            <h3> Please check the survey summary status if you want</h3><br/>
            <input type="submit" value="Check Status" />
        </form>
    </body>
</html>

这是我的 javabean:SurveyBean.java

 package appScope;

    public class SurveyBean {
    private int javaQuantity = 0;
    private int csQuantity = 0;

    public int getJavaQuantity()
    {
        return javaQuantity;
    }

    public int getCsQuantity()
    {
        return csQuantity;
    }

    public void setQuantity(String bookTitle)
    {
        try
        {
            if(bookTitle.equals("java"))
            {
                javaQuantity++;
            }
            if (bookTitle.equals("c"))
            {
                csQuantity++;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是我得到错误的survey.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <h1>Survey Summary</h1>
           //ERROR IS HERE
        Java = <jsp:getProperty name="survey" property="javaQuantity" /> <br/> 
        C# = <jsp:getProperty name="survey" property="csQuantity" /> 
    </body>
</html>
4

1 回答 1

0

名字之前没有介绍过

这表明您还没有将这个 bean 告诉您的 JSP。<jsp:getProperty>在让 JSP 知道 bean 之前,您是直接使用。

您需要使用<jsp:useBean>标签在survey.jsp中定义bean 。标签的name属性getProperty必须与标签的id属性匹配useBean

<jsp:useBean id="survey" class="appScope.SurveyBean" scope="request">

但这不起作用,除非您在check.jspsurvey文件中设置请求范围内命名的 bean 并将该请求发布到survey.jsp

于 2013-07-01T06:27:45.750 回答