【问题标题】:Not getting the proper size of an ArrayList in JSP在 JSP 中没有获得正确大小的 ArrayList
【发布时间】:2008-10-31 13:15:54
【问题描述】:

我无法在下面的 JSP 页面中的 ArrayList alt 中获取正确数量的元素。当我查看 JSP 时,它显示大小是 1 (<%=alt.size()%>),而它应该是 3;我想我正在将该方法添加到生成器类中的数组中,所以我不明白为什么它显示为 1。

这是我的jsp页面:

<%
   ArrayList<Alert> a = AlertGenerator.getAlert();
   pageContext.setAttribute("alt", a);
%>
   <c:forEach var="alert" items="${alt}" varStatus="status" >
      <p>You have <%=alt.size()%> Active Alert(s)</p>
      <ul>
      <li><a href="#" class="linkthree">${alert.alert1}</a></li>
      <li><a href="#" class="linkthree">${alert.alert2}</a></li>
      <li><a href="#" class="linkthree">${alert.alert3}</a></li>
      </ul>
  </c:forEach>

这是生成警报的类:

package com.cg.mock;

import java.util.ArrayList;

public class AlertGenerator {

    public static ArrayList<Alert> getAlert() {

        ArrayList<Alert> alt = new ArrayList<Alert>();

        alt.add(new Alert("alert1","alert2","alert3"));

        return alt;
    }

}

这是我的 bean 类:

package com.cg.mock;

public class Alert {
    String alert1;
    String alert2;
    String alert3;
    public Alert(String alert1, String alert2,String alert3) {
        super();
        this.alert1 = alert1;
        this.alert2 = alert2;
        this.alert3 = alert3;
    }
    public String getAlert1() {
        return alert1;
    }
    public void setAlert1(String alert1) {
        this.alert1 = alert1;
    }
    public String getAlert2() {
        return alert2;
    }
    public void setAlert2(String alert2) {
        this.alert2 = alert2;
    }
    public String getAlert3() {
        return alert3;
    }
    public void setAlert3(String alert3) {
        this.alert3 = alert3;
    }

}

【问题讨论】:

    标签: java jsp arraylist


    【解决方案1】:

    问题是您的 ArrayList 中只有一个 Alert 实例,但该单个 Alert 具有 3 个属性:alert1、alert2 和 alert3。

    看看线:

    alt.add(new Alert("alert1","alert2","alert3"));
    

    你只有一个添加行,它不是循环的。

    一个可能的解决方案:

    public class Alert {
        private String description;
        private String status;
        private Date raisedOn;
        public Alert(String description, String status) {
            this.description = description;
            this.status = status;
            this.raisedOn = new Date();
        }
        public String getDescription() { return description; }
        public String getStatus() { return status; }
        public Date getRaisedOn() { return raisedOn; }
    }
    
    
    ....
    alt.add(new Alert("Disk Almost Full", "Warning"));
    alt.add(new Alert("Disk Full", "Severe"));
    ...
    
    ...
    <table>
        <tr><th>Description</th><th>Status</th><th>Raised</th></td>
        <c:forEach var="alert" items="${alt}">
            <tr>
                <td><c:out value="${alert.description}"/></td>
                <td><c:out value="${alert.status}"/></td>
                <td><c:out value="${alert.raisedOn}"/></td>
            </tr>
        </c:forEach>
    </table>
    

    【讨论】:

      【解决方案2】:

      当您只将added 一项到List 时,您为什么期望它返回3

      【讨论】:

        【解决方案3】:

        要获得 3 个警报,您可以重新设计如下。请注意,警报类只有一个属性。您可以为每个警报创建一个新的警报实例。

        package com.cg.mock;
        
        public class Alert {
          String alert1;
          public Alert(String alert1) {
            super();
            this.alert1 = alert1;       
          }
          public String getAlert1() {
            return alert1;
          }
          public void setAlert1(String alert1) {
            this.alert1 = alert1;
          }
        }
        

        在警报生成器中:

        ArrayList<Alert> alt = new ArrayList<Alert>();
        
        alt.add(new Alert("alert1");
        alt.add(new Alert("alert2");
        alt.add(new Alert("alert3");
        
        return alt;
        

        在 JSP 上:

        <p>You have <%=alt.size()%> Active Alert(s)</p>
        <ul>
        <c:forEach var="alert" items="${alt}" varStatus="status" >    
        
             <li><a href="#" class="linkthree">${alert.alert1}</a></li>
        
          </c:forEach>
         </ul>
        

        注意 ul 在 forEach 循环之外。

        【讨论】:

          【解决方案4】:

          ArrayList 仅包含一个元素 Alert(元素 Alert 包含三个字符串警报。

          【讨论】:

            【解决方案5】:

            将您的 JSP 更改为:

            <%
                ArrayList<Alert> a = AlertGenerator.getAlert();
                pageContext.setAttribute("alt", a);
            %>
            <p>You have <%=alt.size()%> Active Alert(s)</p>
            <ul>
                <c:forEach var="alert" items="${alt}" varStatus="status" >
                    <li><a href="#" class="linkthree">${alert.alert}</a></li>
                </c:forEach>
            </ul>
            

            将 AlertGenerator.java 更改为:

            package com.cg.mock;
            
            import java.util.ArrayList;
            
            public class AlertGenerator {
            
                public static ArrayList<Alert> getAlert() {
            
                    ArrayList<Alert> alt = new ArrayList<Alert>();
            
                    alt.add(new Alert("alert2"));
                    alt.add(new Alert("alert2"));
                    alt.add(new Alert("alert3"));
            
                    return alt;
                }
            }
            

            将 Alert.java 更改为:

            package com.cg.mock;
            
            public class Alert {
                String alert;
                public Alert(String alert) {
                    this.alert = alert;
                }
                public String getAlert() {
                    return alert;
                }
                public void setAlert(String alert) {
                    this.alert = alert;
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-04-21
              • 1970-01-01
              • 1970-01-01
              • 2023-04-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多