JSTL sql:查询变量

发布于 2024-11-09 05:22:00 字数 1267 浏览 0 评论 0原文

我在 JSP 文件中编写了以下代码:

<c:catch var="e">
    <%
        int accountNumber = Integer.parseInt(request.getParameter("accountNumber"));
        int depositAmount = Integer.parseInt(request.getParameter("depositAmount"));
    %>
    <sql:query var='account' dataSource="jdbc/bank">
        select * from account where AccountNumber=<%= accountNumber %>      
    </sql:query>
    <c:choose>
        <c:when test="${account.first() == false}">
            <p>Account not found</p>
        </c:when>
        <c:otherwise>
            <h3>Deposit Made</h3>
            <p>Account number: <%= accountNumber %></p>
            <p>Deposit amount: <%= depositAmount %></p>
            <p>New balance: </p>
        </c:otherwise>
    </c:choose>
</c:catch>


<c:if test="${e != null}">
    error
</c:if>

我遇到的问题是以下代码抛出 javax.el.MethodNotFoundException: Unable to find method [first] with [0]parameters 异常:

<c:when test="${account.first() == false}">
  <p>Account not found</p>
</c:when>

我需要访问 account 变量sql:query 这样我就可以检查第一行是否存在。

I wrote the following code in a JSP file:

<c:catch var="e">
    <%
        int accountNumber = Integer.parseInt(request.getParameter("accountNumber"));
        int depositAmount = Integer.parseInt(request.getParameter("depositAmount"));
    %>
    <sql:query var='account' dataSource="jdbc/bank">
        select * from account where AccountNumber=<%= accountNumber %>      
    </sql:query>
    <c:choose>
        <c:when test="${account.first() == false}">
            <p>Account not found</p>
        </c:when>
        <c:otherwise>
            <h3>Deposit Made</h3>
            <p>Account number: <%= accountNumber %></p>
            <p>Deposit amount: <%= depositAmount %></p>
            <p>New balance: </p>
        </c:otherwise>
    </c:choose>
</c:catch>


<c:if test="${e != null}">
    error
</c:if>

The problem I am having is that the following code throws an javax.el.MethodNotFoundException: Unable to find method [first] with [0] parameters exception:

<c:when test="${account.first() == false}">
  <p>Account not found</p>
</c:when>

I need to access the account variable in the sql:query so I can check to see if a first row exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲墙角沉默 2024-11-16 05:22:00
<sql:query var='account' dataSource="jdbc/bank">

根据 ;文档帐户是一个javax.servlet.jsp.jstl.sql.Result

<c:when test="${account.first() == false}">

根据类的 javadoc,该类没有 first() 方法。但是有一个 getRowCount() 方法。我建议用它来代替。

<c:when test="${account.rowCount == 0}">
<sql:query var='account' dataSource="jdbc/bank">

As per the <sql:query> documentation, the account is a javax.servlet.jsp.jstl.sql.Result class.

<c:when test="${account.first() == false}">

As per the class' javadoc, that class doesn't have a first() method. There is however a getRowCount() method. I'd suggest to use that instead.

<c:when test="${account.rowCount == 0}">
烦人精 2024-11-16 05:22:00

首先,我强烈建议您永远不要使用 scriptlet。如果您将此示例重写为 Java Servlet,那么您至少可以进行编译时检查,并且还可以编写逻辑的 JUnit 测试。

接下来,我绝对讨厌他们将 扔进 JSTL,这是对 模型视图控制器模式,因为您将数据访问代码放入前端。这造成了维护噩梦,因此我将使用 重写该模型代码Java Persistence API (JPA) 或 Hibernate 框架。

话虽这么说:如果您必须让示例正常工作,那么您的问题是 account.first() 不是有效的方法调用。即使您只能返回 1 个结果,查询也会返回结果列表。尝试如下操作。

<c:forEach var="account" begin="0" items="${account.rows}">
<h3>Deposit Made</h3>
<p>${account.depositAmount}</p>
</c:forEach>

First off, I highly recommend that you NEVER use scriptlets. If you rewrote this example as a Java Servlet, you'd at least have compile-time checking and it would also give you the ability to write a JUnit test of the logic.

Next, I absolutely hate that they threw <sql> into JSTL, which is a blatant disregard for the Model View Controller pattern, since you are putting data access code into the front-end. This makes for a maintenance nightmare, so I would rewrite that model code using the Java Persistence API (JPA) or the Hibernate framework.

That being said: if you must get the example working, then your problem is that account.first() is not a valid method call. Even though you can only ever return 1 result, the query is returning a list of results. Try something like the following.

<c:forEach var="account" begin="0" items="${account.rows}">
<h3>Deposit Made</h3>
<p>${account.depositAmount}</p>
</c:forEach>
々眼睛长脚气 2024-11-16 05:22:00

实现这一点的最简单方法是使用第一个变量和 getFirst() 方法来实现 DTO。

public class account {
    private String first;
    public String getFirst(){
        return first;
    }
    ....
}

当您在 JSP 中调用它时,它应该如下所示:

test="${!account.first}"

它将调用 account.getFirst()

您的 SQL 数据将需要映射到此帐户对象,您将在其中执行所有验证,确保没有空值等。

The most foolproof way to implemt this is to implement a DTO with a first variable and then a getFirst() method.

public class account {
    private String first;
    public String getFirst(){
        return first;
    }
    ....
}

And when you call it in JSP, it should look this:

test="${!account.first}"

which will call account.getFirst()

Your SQL data will need to be mapped to this account object in which you would do all the validation, make sure there are no null values etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文