java JSTL sql:查询变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6090862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSTL sql:query variable
提问by Brian
I wrote the following code in a JSP file:
我在 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>
The problem I am having is that the following code throws an javax.el.MethodNotFoundException: Unable to find method [first] with [0] parameters exception:
我遇到的问题是以下代码抛出 javax.el.MethodNotFoundException: Unable to find method [first] with [0] parameters 异常:
<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.
我需要访问 sql:query 中的 account 变量,以便我可以检查第一行是否存在。
采纳答案by BalusC
<sql:query var='account' dataSource="jdbc/bank">
As per the <sql:query>
documentation, the account
is a javax.servlet.jsp.jstl.sql.Result
class.
根据<sql:query>
文档,这account
是一个javax.servlet.jsp.jstl.sql.Result
类。
<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.
根据类的 javadoc,该类没有first()
方法。不过有一个getRowCount()
方法。我建议改用它。
<c:when test="${account.rowCount == 0}">
回答by Drew
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.
首先,我强烈建议您永远不要使用 scriptlets。如果您将此示例重写为 Java Servlet,那么您至少可以进行编译时检查,并且它还可以让您编写逻辑的 JUnit 测试。
Next, I absolutely hate that they threw <sql>
into JSTL, which is a blatant disregard for the Model View Controllerpattern, 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.
接下来,我非常讨厌他们<sql>
投入 JSTL,这是对模型视图控制器模式的公然无视,因为您将数据访问代码放入前端。这使维护成为噩梦,因此我将使用Java Persistence API (JPA)或 Hibernate 框架重写该模型代码。
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 listof results. Try something like the following.
话虽如此:如果您必须让示例工作,那么您的问题是这account.first()
不是有效的方法调用。即使您只能返回 1 个结果,查询也会返回结果列表。尝试类似以下内容。
<c:forEach var="account" begin="0" items="${account.rows}">
<h3>Deposit Made</h3>
<p>${account.depositAmount}</p>
</c:forEach>
回答by Jan Vladimir Mostert
The most foolproof way to implemt this is to implement a DTO with a first variable and then a getFirst() method.
实现这一点的最简单的方法是使用第一个变量和 getFirst() 方法实现 DTO。
public class account {
private String first;
public String getFirst(){
return first;
}
....
}
And when you call it in JSP, it should look this:
当你在 JSP 中调用它时,它应该是这样的:
test="${!account.first}"
which will call account.getFirst()
这将调用 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.
您的 SQL 数据将需要映射到此帐户对象,您将在其中执行所有验证,确保没有空值等。