简体   繁体   中英

how to iterate an ArrayList in JSP

I create a JSP which call a java method and get an object ArrayList. I want to display the result in a table but nothing appeared. The jsp is like below:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="SCOfetch.*" %>
<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>First Page</title>
<% JCOtest connection1 = new JCOtest(); %>
    <%
        ArrayList<CompanyRecord> list = new ArrayList<CompanyRecord>();
        list = connection1.step4QueryTable();
    %>
</head>
<body>
    <c:forEach var="row" items="${list.rows}">
        <tr>
           <td><c:out value="${row.getValue(Name)}"/></td>
           <td><c:out value="${row.getValue(Code)}"/></td>     
        </tr>
    </c:forEach>
</body>
</html>

The java code of CompanyRecord class is

public class CompanyRecord {
private String Code;
private String Name;
public void setValue(String value,String column)
{
    if (column.equals("Code"))
    {
        Code=value;
    }
    else 
    {
        Name=value;
    }
}

public String getValue(String column)
{

    if (column.equals("Code"))
    {
        return Code;
    }
    else 
    {
        return Name;
    }
}
}

what's the correct way to call an ArrayList lineitem's method? Thanks.

please find the below code for iterate ArrayList in JSP.

and please don't create the ArrayList in JSP.please create ArrayList and set before calling JSP from the servlet.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="SCOfetch.*" %>
<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>First Page</title>
<% JCOtest connection1 = new JCOtest(); %>
    <%
        ArrayList<CompanyRecord> list = new ArrayList<CompanyRecord>();
        list = connection1.step4QueryTable();
    %>
</head>
<body>
     <c:forEach items="${list}" var="row">
       <tr>
       <td>${row.Name}</td> <!-- use variable name same like in DTO -->
       <td>${row.Code}</td>     
    </tr>
   </c:forEach>

</body>
</html>

AND please generate getter methods in CompanyRecord DTO class the only this code will work fine.

public class CompanyRecord {
private String Code;
private String Name;
public void setValue(String value,String column)
{
    if (column.equals("Code"))
    {
        Code=value;
    }
    else 
    {
        Name=value;
    }
}

public String getValue(String column)
{

    if (column.equals("Code"))
    {
        return Code;
    }
    else 
    {
        return Name;
    }
}

public String getCode()
{
  return this.Code;
 }
public String getName()
{
  return this.Name;
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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