如何使 Java ResultSet 在我的 jsp 中可用? [英] How do I make a Java ResultSet available in my jsp?
问题描述
我想为一些构建具有多个参数的复杂查询的 Java 代码替换 sql:query.当前的sql是一个简单的select.
<前><sql:查询变量=结果"数据源="${dSource}"sql="select * from TABLE "></sql:查询>如何获取我的 Java ResultSet(即 rs = stmt.executeQuery(sql);)并使结果在我的 JSP 中可用,以便我可以编写这本教科书 JSP?
为了更清楚,我想删除上面的查询并用Java替换它.
<前><%结果集 rs = stmt.executeQuery(sql);//杂乱的代码会出现在一些控制器中%><前><c:forEach var="row" items="${result.rows}"><c:out value="${row.name}"/></c:forEach>
我是否在 Java 部分设置了 session/page 变量,或者是否有一些 EL 技巧可以用来访问该变量?
模型(行):
公共类行{私人字符串名称;//添加/生成构造函数、getter 和 setter.}
DAO:
public Listlist() 抛出 SQLException {连接连接=空;语句语句 = null;结果集 resultSet = null;列表<行>行 = 新的 ArrayList();尝试 {connection = database.getConnection();语句 = connection.createStatement();resultSet = statement.executeQuery(SQL_LIST);而(resultSet.next()){行行 = new Row();row.setName(resultSet.getString("name"));//...行.添加(行);}} 最后 {if (resultSet != null) try { resultSet.close();} catch (SQLException logOrIgnore) {}if (statement != null) try { statement.close();} catch (SQLException logOrIgnore) {}if (connection != null) try { connection.close();} catch (SQLException logOrIgnore) {}}返回行;}
控制器(servlet):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {尝试 {列表<行>行 = someDAO.list();request.setAttribute("行", 行);} catch (SQLException e) {request.setAttribute("error", "检索行失败.");e.printStackTrace();}request.getRequestDispatcher("page.jsp").forward(request, response);}
视图(page.jsp):
<c:out value="${row.name}"/>...</c:forEach><c:if test="${not empty error}">错误:${error}</c:if>
I'd like to swap out an sql:query for some Java code that builds a complex query with several parameters. The current sql is a simple select.
<sql:query var="result" dataSource="${dSource}" sql="select * from TABLE "> </sql:query>
How do I take my Java ResultSet (ie. rs = stmt.executeQuery(sql);) and make the results available in my JSP so I can do this textbook JSP?
To be more clear, I want to remove the above query and replace it with Java.
<% ResultSet rs = stmt.executeQuery(sql); // Messy code will be in some Controller %>
<c:forEach var="row" items="${result.rows}"> <c:out value="${row.name}"/> </c:forEach>
Do I set the session/page variable in the Java section or is there some EL trick that I can use to access the variable?
Model (Row):
public class Row {
private String name;
// Add/generate constructor(s), getters and setters.
}
DAO:
public List<Row> list() throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<Row> rows = new ArrayList<Row>();
try {
connection = database.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(SQL_LIST);
while (resultSet.next()) {
Row row = new Row();
row.setName(resultSet.getString("name"));
// ...
rows.add(row);
}
} finally {
if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return rows;
}
Controller (servlet):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Row> rows = someDAO.list();
request.setAttribute("rows", rows);
} catch (SQLException e) {
request.setAttribute("error", "Retrieving rows failed.");
e.printStackTrace();
}
request.getRequestDispatcher("page.jsp").forward(request, response);
}
View (page.jsp):
<c:forEach items="${rows}" var="row">
<c:out value="${row.name}" />
...
</c:forEach>
<c:if test="${not empty error}">Error: ${error}</c:if>
这篇关于如何使 Java ResultSet 在我的 jsp 中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!