我在 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>
我需要访问 sql:query 中的帐户变量,以便检查第一行是否存在。
最佳答案
<sql:query var='account' dataSource="jdbc/bank">
根据 <sql:query>
documentation , account
是 javax.servlet.jsp.jstl.sql.Result
class .
<c:when test="${account.first() == false}">
根据类的 javadoc,该类没有 first()
方法。但是有一个 getRowCount()
method .我建议改用它。
<c:when test="${account.rowCount == 0}">
关于java - JSTL sql :query variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6090862/