从多个ArrayList在jsp中使用for循环的项的大小

时间:2018-04-13 07:45:57

标签: java jsp for-loop arraylist

我创建了一个页面(比如说query.jsp)请求过滤,然后用户键入相应的值,然后将值提交给java类。然后该类处理过滤条件,然后再将多个ArrayList形式的结果传递给query.jsp。我设法以对象的形式传递数据并将它们转发回query.jsp中的ArrayList,但我真的不知道如何让它们迭代到每个索引中。

铸造后的产品数据样本

[GE1, T1, S1, GE1, GE4, GL1, T1, GE3, R1, GL5, E2, S6, GL5, GE1, GL1, GL5, S4, E5, S3, S6, GL6, T1, GL5, GL1, GE2, T1, R2, E1, E1, GL1, S2, S2, S6, GL3, S3, GE3, E1, S4, S3, GL2, R2, S6, S2, GL1, T4, GE2, S5, R1, GE1, S3, GL2, GE4, GL3, GE3, GL3, T3, GE5, E3, E2, T1, E2, E2, R2, GE5, GE1, GE1, GL1, GL1, GE3, T1] 

投射后的每个数据表示某些人的位置。假设产品[0]的索引是“a”,与公司相同,公司[0]是“a”,CName,有效性等数据。

query.jsp

<input type="date" placeholder="Date from" id="datefrom" class="form-control" name="datefrom" required>
<input type="date" placeholder="Date to" id="dateto" class="form-control" name="dateto" required>
<select class="form-control" name="status" required>
<option value="">&nbsp;</option>
<option value="New">New</option>
<option value="Renew">Renew</option>    
</select>
<table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
 <thead>
    <tr>
        <th>Browser</th>
        <th class="hidden-xs">Creator</th>
        <th>Cost (USD)</th>
        <th class="hidden-xs"> Software license</th>
        <th>Current layout engine</th>
    </tr>
</thead>
<tbody>
    <!-- START DUMPING DATA -->
    <% 
        ArrayList<String> product = (ArrayList<String>) request.getAttribute("product"); 
        ArrayList<String> company = (ArrayList<String>) request.getAttribute("company");
        ArrayList<String> CName = (ArrayList<String>) request.getAttribute("CName");
        ArrayList<String> validity = (ArrayList<String>) request.getAttribute("validity");
        ArrayList<String> unit = (ArrayList<String>) request.getAttribute("unit");
        ArrayList<String> totPrice = (ArrayList<String>) request.getAttribute("totPrice");

        for(int i=0, i<product.size(),i++)//what size should i put index?
        {%>
            <tr>product</tr>
            <tr>company</tr>
            <tr>CName</tr>
            <tr>validity</tr>
            <tr>unit</tr>
            <tr>totPrice</tr>
        %>

1 个答案:

答案 0 :(得分:0)

也许您需要重新设计代码,因为所有6个列表都是不相关的,并且具有不同的大小,这会导致循环迭代问题。更好的方法是创建POJO,然后创建一个包含该POJO的多个实例的列表,如下所示:

Product.java

package com.test;

public class Product {
    private String product;
    private String company;
    private String CName;
    private String validity;
    private String unit;
    private String totPrice;

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getCName() {
        return CName;
    }

    public void setCName(String cName) {
        CName = cName;
    }

    public String getValidity() {
        return validity;
    }

    public void setValidity(String validity) {
        this.validity = validity;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getTotPrice() {
        return totPrice;
    }

    public void setTotPrice(String totPrice) {
        this.totPrice = totPrice;
    }

    @Override
    public String toString() {
        return "Product [product=" + product + ", company=" + company + ", CName=" + CName + ", validity=" + validity
                + ", unit=" + unit + ", totPrice=" + totPrice + "]";
    }

}

query.jsp

<!-- START DUMPING DATA -->
<% 
    ArrayList<Product> products = (ArrayList<Product>) request.getAttribute("products"); 

    for(Product product : products)
    {%>
      <tr>
        <td>product.getProduct()</td>
        <td>product.getCompany()</td>
        <td>product.getCName()</td>
        <td>product.getValidity()</td>
        <td>product.getUnit()</td>
        <td>product.getTotPrice()</td>
      </tr>
    %>