简体   繁体   English

显示来自ArrayList的数据(EJB + Servlet + JSP(JSTL))

[英]Displaying data from ArrayList (EJB + Servlet + JSP(JSTL))

I have a stateless Session Bean in which I get the values ​​from the database and write them down in Arraylist, who returns to the servlet. 我有一个无状态的Session Bean,从数据库中获取值并将它们写到Arraylist中,然后Arraylist返回到servlet。 In a servlet I send ArrayList on JSP page and try to show the value, but displays only the line "dataList", what is my mistake? 在servlet中,我在JSP页面上发送ArrayList并尝试显示该值,但仅显示“ dataList”行,我的错误是什么?

Here is my code: 这是我的代码:

ViewBean.java ViewBean.java

//imports
@Stateless
@LocalBean
public class ViewBean {
    public List getData() {
        ResultSet rs = null;
        List list = null;
        try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con =              DriverManager.getConnection("jdbc:mysql://localhost:3306/c_store", "root", "root");
        Statement stmt = con.createStatement();
        String query = "SELECT product.name_prod, product.price_prod, buy.number_buy, client.name_client, client.date_client FROM product, buy, client WHERE product.id_prod = buy.id_buy = client.id_client;";
        stmt.executeQuery(query);
        rs = stmt.getResultSet();
        list = new ArrayList();

        while(rs.next()){
            list.add(rs.getString(1));
            list.add(rs.getInt(2));
            list.add(rs.getInt(3));
            list.add(rs.getString(4));
            list.add(rs.getString(5));
        }
        rs.close();
        stmt.close();
        con.close();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return list;
}

}

ViewServlet.java ViewServlet.java

//imports
@WebServlet("/ViewServlet")
public class ViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@EJB
ViewBean viewBean;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    List data = new ArrayList();
    data = viewBean.getData();
    request.setAttribute("dataList", data);

    for(int i = 0; i<data.size(); i++){
        out.println(data.get(i));
    }
}
   }

view.jsp view.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Records</title>
</head>
<body>
<c:forEach var="item" items="dataList">
    <b><c:out value="${item}" /></b>
</c:forEach>
</body>
</html>

You forgot to declare dataList as a variable, look at the items attribute in the loop: 您忘记将dataList声明为变量,请在循环中items属性:

<c:forEach var="item" items="${dataList}">
                             ^ here is the bug, add the dollar sign and the curly braces
    <b><c:out value="${item}" /></b>
</c:forEach>

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

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