gpt4 book ai didi

jsp - 如何在 JSP 页面上显示 bean 对象的 Arraylist

转载 作者:行者123 更新时间:2023-12-04 04:59:22 24 4
gpt4 key购买 nike

我想将 bean 对象的 Arraylist 显示到 HTML 表(JSP 页面)中。
请帮助我哪里出错了。
这是创建具有数据库数据的 bean 对象的代码。最后,所有 bean 对象都被添加到一个数组列表中。使用 RequestDispatcher 我将 arraylist 发送到 ViewData.jsp
现在请告诉如何将arraylist数据解释为表。

package com.dsr.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import com.dsr.beans.EmpBean;
import com.dsr.db.DB;
import javax.servlet.RequestDispatcher;
import java.sql.*;

/**
* Servlet implementation class for Servlet: CheckConn
*
*/
public class CheckConn extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public CheckConn() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

ArrayList<EmpBean> ebList= new ArrayList<EmpBean>();
System.out.println(request.getParameter("date"));
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;



try
{
DB dao=new DB();
conn=dao.getConnection();
stmt=conn.createStatement();

rs = stmt.executeQuery("exec select_details");
while(rs.next()){
EmpBean eb = new EmpBean();
eb.setEmp_id(rs.getString("emp_id"));
eb.setFirst_name(rs.getString("first_name"));
eb.setLast_name(rs.getString("last_name"));
ebList.add(eb);
}
System.out.println(ebList.size());
for (int i=0; i<ebList.size(); i++){
EmpBean eb1= ebList.get(i);
System.out.println(eb1.getEmp_id());
System.out.println(eb1.getFirst_name());
System.out.println(eb1.getLast_name());
}
// stmt.executeQuery("commit");

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try {
conn.close();
stmt.close();
} catch (SQLException e) {

e.printStackTrace();
}


}
request.setAttribute("mybean",ebList);
RequestDispatcher dispatcher = request.getRequestDispatcher("viewData.jsp");
if (dispatcher != null){
dispatcher.forward(request, response);
}

}
}

现在告诉我如何将 arraylist 数据解释为 html 表。

最佳答案

使用 JSTLJSP页面:

例如:

<table .... >
<c:forEach var="empBean" items="${requestScope['mybean']}">
<tr>
<td>
<c:out value="${empBean.first_name}"/>
</td>
</tr>
</c:forEach>
</table>

另见:
  • https://stackoverflow.com/tags/servlets/info
  • 关于jsp - 如何在 JSP 页面上显示 bean 对象的 Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16331462/

    24 4 0