2008-10-31 50 views
0

我無法在下面的JSP頁面中的ArrayList alt中獲取正確數目的元素。當我查看JSP時,它顯示大小爲1(<%=alt.size()%>),當它應該是3時;我想我補充說,法在發電機類的數組,所以我不明白爲什麼它顯示1未在JSP中獲得ArrayList的正確大小

這是我的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; 
    } 

} 

回答

0

拿到3個警報,你可以重新設計如下注意,只有在那裏。 。Alert類的一個屬性可以爲每個警報創建警報的新實例

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循環之外。

+0

是的..這是一個好主意..感謝您的答覆.. – 2008-11-03 05:04:54

1

爲什麼你希望它返回3當你只有add編輯一項到List

+0

ü可以給我的任何想法,我可以得到它作爲jsp頁面中的三個.. – 2008-10-31 13:33:32

2

問題是您的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> 
+0

可以給我任何想法,我可以得到它作爲三個在jsp頁面.. – 2008-10-31 13:35:08

0

ArrayList中僅包含一個元素警報(元素警報包含三個字符串警報

+0

可以給我任何想法,我可以得到它作爲三個在jsp頁面.. – 2008-10-31 13:36:01

0

改變你的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; 
    } 
} 
+0

是的..謝謝你我完美.. – 2008-11-03 05:33:54