JSP에서 Arraylist의 적절한 크기를 얻지 못함
문제
Arraylist에서 올바른 수의 요소를 얻는 데 어려움이 있습니다. alt
아래 JSP 페이지에서. 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;
}
}
이것은 내 콩 수업입니다.
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 인스턴스가 하나만 있지만 Alert1, Alert2 및 Alert3의 3 가지 속성이 있습니다.
라인을 살펴보십시오.
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
하나의 항목에 ed List
?
ArrayList에는 하나의 요소 경보 만 포함됩니다 (요소 경보에는 3 개의 문자열 알림이 포함되어 있습니다.
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;
}
}
제휴하지 않습니다 StackOverflow