【问题标题】:Jakarta Server Pages doesnt find propertyJakarta Server Pages 找不到属性
【发布时间】:2021-07-14 13:04:38
【问题描述】:

我正在尝试将 jsp 与 Java 一起使用,因此我创建了函数 getBestellung() 。此函数连接到我的数据库并从中获取一些条目。该方法本身有效,但是当我尝试获取返回值时,我的 Tomcat 服务器上出现以下错误:org.apache.jasper.JasperException: Cannot find any information on property [data] in a bean of type [com.example.CSA_Server.DBFunc]

这是类的样子:

import com.example.CSA_Server.Bestellung;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.sql.*;

public class DBFunc implements secrets{

    public static Connection conn() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("Verbindung wurde hergestellt");
        return conn;
    }

    public static String getBestellung() throws SQLException{
        Connection conn = conn();
        String query = "SELECT * FROM Bestellung";
        Statement statement = conn.createStatement();
        ResultSet result = statement.executeQuery(query);
        Bestellung b1 = null;
        while(result.next()) {
            long bID = result.getLong("B-ID");
            long kID = result.getLong("K-ID");
            b1 = new Bestellung(bID, kID);
        }
        String string = String.valueOf(b1);
        conn.close();
        //System.out.println(string);
        return string;
    }


}

JSP 看起来像:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="time" class="com.example.CSA_Server.TimeServlet" scope="request" />
<jsp:useBean id="data" class="com.example.CSA_Server.DBFunc" scope="request" />
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<p>Das ist ein Probetext</p>
<br/>
<a href="hello-servlet">Hello Servlet</a>
<LI>Hallo du da</LI>
<LI><jsp:getProperty name="time" property="time"/></LI>
<LI>Du bist: <%request.getParameter("name");%></LI>

<p><jsp:getProperty name="data" property="data"/></p>
<p>Data: <%request.getParameter("data");%></p>
</body>
</html>

大多数时候,当我刷新页面时没有错误。没有错误时显示以下值:class com.example.CSA_Server.DBFunc

多次刷新后出现上述错误。

getBestellung() 函数有问题还是 jsp 语法错误?

【问题讨论】:

  • 这段代码 和另一个类似的是scriptlet。 Scriptlet 不打印任何内容。此外,按照您编写它们的方式,它们什么也不做。您的意思是让它们成为“JSP 表达式”吗?

标签: java jsp web tomcat


【解决方案1】:

您没有正确使用&lt;jsp:getProperty&gt; 标记:属性property 必须是您要检索的JavaBean 属性的名称(例如this tutorial)。在您的情况下,您想要检索属性 “bestellung”,因此请使用:

<jsp:getProperty name="data" property="bestellung"/>

备注:您也可以改用EL表达式${data.bestellung}

【讨论】:

  • 我更改了它,但现在我收到以下错误:org.apache.jasper.JasperException: Cannot find any information on property [bestellung] in a bean of type [com.example.CSA_Server.DBFunc]。它的行为仍然像我之前提到的那样。所以有时它会显示class com.example.CSA_Server.DBFunc,有时会显示错误。
  • 抱歉,我没有注意到 getBestellung 被声明为 static。 JSP 正在寻找带有签名public String getBestellung()instance 方法。您应该删除 static 修饰符。
  • 好的,我也改了。现在我得到同样的错误或没有 "class com.example.CSA_Server.DBFunc" 的页面。当我打印出结果时,我得到“Bestellung{bID=2, kID=2}”,但这并没有出现在网页上。,,,
  • 清空work目录,检查你的Tomcat是否没有运行旧代码。
猜你喜欢
  • 2020-03-24
  • 2021-06-27
  • 1970-01-01
  • 2021-12-27
  • 2021-02-02
  • 2018-05-06
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多