我无法在下面的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;
    }

}
有帮助吗?

解决方案

要获得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;
  }
}

在AlertGenerator中:

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循环中。

其他提示

问题是您的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>

当你只3将一个项目输入add时,为什么期望它返回List

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

将您的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;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top