简体   繁体   English

如何在 JAVA JSP 页面上检索结果集?

[英]How to retrieve resultset on JAVA JSP page?

I want to show result set passed from controller to JSP page and I'm using Spring framework.我想显示从 controller 传递到 JSP 页面的结果集,我正在使用 Spring 框架。 Anyone have any idea how to display data on my JSP page.任何人都知道如何在我的 JSP 页面上显示数据。 I don't have any idea how to display data on table.我不知道如何在表格上显示数据。

Model/ Domain:型号/领域:

package com.mycompany.domain;

public class Accounts {

private int accNumber;
private String accName;
private int accBalance;

public Accounts(int accNumber, String accName, int accBalance) {

    this.accNumber = accNumber;
    this.accName = accName;
    this.accBalance = accBalance;
}

public int getAccNumber() {
    return accNumber;
}

public String getAccName() {
    return accName;
}

public int getAccBalance() {
    return accBalance;
}

public void setAccNumber(int accNumber) {
    this.accNumber = accNumber;
}

public void setAccName(String accName) {
    this.accName = accName;
}

public void setAccBalance(int accBalance) {
    this.accBalance = accBalance;
}

} }

Controller: From controller I call Account Implement: Controller:从 controller 我调用帐户实现:

@Autowired
AccountInterface ai;

@RequestMapping(value = "/listallaccounts")
public String list(@ModelAttribute("account") AccountCommand ac, Model m) {
    ResultSet rs= ai.listAccount();
    
    if (rs != null) {
        m.addAttribute("title", "List");
        m.addAttribute("AccountList",rs);
        return ("listAccount");
    } else {
        m.addAttribute("title", "List");
        m.addAttribute("message", "No Account exist.");
        return "";
    }

}

DAO: From DAO I retrieve data from database to result set. DAO:从 DAO 我将数据从数据库检索到结果集。

AccountImplement()

public ResultSet listAccount() {
    String sql = "SELECT * FROM `bankdb`.`accounts`";
    ResultSet rs = db.select(sql);
    return rs;
}

My JSP: In this page I want show show account table.我的 JSP:在此页面中,我想显示显示帐户表。

<table class="table table-bordered">
      <thead>
         <tr>
             <th>Account Number</th>
             <th>Account Name</th>
             <th>Account balance</th>
         </tr>
      </thead>
      <tbody>
      <%
          request.getAttribute("AccountList");
      %>
         <c:forEach items="${AccountList}" var="item">
         <tr>
             <td>${item.AccountNumber}</td>
             <td>${item.AccountName}</td>
             <td>${item.AccountBalance}</td>
         </tr>
         </c:forEach>
   </tbody>

i think you should include a script then request the data from the controller using我认为您应该包含一个脚本,然后使用从 controller 请求数据

 <script>
    $.ajax({
    url: "yourhost/listallaccounts",
    type: 'GET',
    data: {data});
    </script>

then you can format it in the table然后你可以在表格中格式化它

Couple of things to notice here.这里有几点需要注意。

  1. request.getAttribute("AccountList"); request.getAttribute("AccountList"); This is unnecessary as the data is in modal attribute but not in the request.这是不必要的,因为数据在模式属性中但不在请求中。 So you can remove this.所以你可以删除它。

  2. You are setting ResultSet as data value but trying to iterate like list or collection which is not right.您将 ResultSet 设置为数据值,但尝试像列表或集合一样进行迭代,这是不正确的。

  3. It is not recommended to put ResultSet in modal attribute, change the ResultSet to List collection by iterating it on controller level and then set the list to modal attribute.不建议将 ResultSet 放在 modal 属性中,通过在 controller 级别上迭代将 ResultSet 更改为 List 集合,然后将列表设置为 modal 属性。

ResultSet to List --> https://stackoverflow.com/a/13010010/6572971结果集到列表-> https://stackoverflow.com/a/13010010/6572971

This will work.这将起作用。

You can directly do this in jsp您可以在 jsp 中直接执行此操作

   <%  
    AccountDAO ad;//intreface
    ad = new AccountDAOImplementation();//repository
    ResultSet rs = ad.ListAllAccount();
    
    while (rs.next()) {
%>
<tr>
    <td><% out.print(rs.getString("accountNumber")); %></td>
    <td><% out.print(rs.getString("accountName")); %></td>
    <td><% out.print(rs.getString("Balance")); %></td>
</tr>
<%
    }

%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM