【问题标题】:size of item for using for loop in jsp from multiple ArrayList来自多个ArrayList的jsp中使用for循环的项目大小
【发布时间】:2018-09-23 12:12:36
【问题描述】:

我创建了一个页面(比如说 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] 

施法后的每个数据都代表特定人的位置。假设product[0]的索引是针对“a”的,company[0]是针对“a”的数据,CName,validity等。

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>
        %>

【问题讨论】:

  • 能否支持 Map 直到 6 个字符串?好奇
  • 不,算了,哈哈,我得到了答案,所以谢谢你看到这篇文章:)

标签: java jsp for-loop arraylist


【解决方案1】:

也许你需要重新设计你的代码,因为所有 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>
    %>

【讨论】:

  • 我也认为创建这个类是明智的,但是逻辑上每个数组都包含相同的数量索引。那么如果我只得到product.size()会不会是错的?我的意思是它的性能明智吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2014-11-07
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 2013-02-07
  • 2014-11-13
相关资源
最近更新 更多