Nicht immer die richtige Größe einer Arraylist in JSP
Frage
Ich habe Probleme mit der richtigen Anzahl von Elementen in der Arraylist alt
in der JSP-Seite immer unten. Wenn ich die JSP anzuzeigen zeigt es die Größe 1 (<%=alt.size()%>
), wenn es 3 sein soll; Ich glaube, ich bin auf diese Methode das Array in der Generator-Klasse hinzufügen, so dass ich nicht verstehen, warum es 1 zeigt.
Das ist meine jsp Seite:
<%
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>
Das ist die Klasse, die die Warnungen generiert:
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;
}
}
Das ist meine Bean-Klasse:
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;
}
}
Lösung
Um 3 Alerts Sie neu gestalten können wie folgt. Beachten Sie, dass es nur eine Eigenschaft der Alarmklasse ist. Sie können für jeden Alarm eine neue Instanz des Alert erstellen.
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;
}
}
Im AlertGenerator:
ArrayList<Alert> alt = new ArrayList<Alert>();
alt.add(new Alert("alert1");
alt.add(new Alert("alert2");
alt.add(new Alert("alert3");
return alt;
Und auf der 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>
Beachten Sie die UL außerhalb der foreach-Schleife sind.
Andere Tipps
Das Problem ist, dass Sie nur eine Alert-Instanz in Ihrer Arraylist, aber der einzige Alarm hat 3 Eigenschaften:. Alert 1, alert 2 und alert3
Werfen Sie einen Blick auf die Zeile:
alt.add(new Alert("alert1","alert2","alert3"));
Sie haben nur eine Zeile hinzufügen, und es ist nicht in einer Schleife.
Eine mögliche Lösung:
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>
Warum erwarten Sie es 3
zurückzukehren, wenn Sie nur ein Element zum add
List
ed haben?
Die Arraylist enthält nur ein Element Alert (das Element Benachrichtigung enthält drei Strings Warnungen.
Ändern Sie Ihre 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>
Ändern AlertGenerator.java an:
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;
}
}
Ändern Alert.java an:
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;
}
}