tags:

views:

1064

answers:

5

I have problem in getting the number of elements in the array that i have highlighted as bold in the jsp page( i.e, <%=alt.size()%> ) if i execute this i m gettin the value as 1(but it should rather print 3) because i think i m adding that method to the array in the generator class thats y it is taking an only 1. can u please help me how do i get the right output for this..

This is my jsp page

<%
   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>

This is my arraygenerator class

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;
    }

}

This is my bean class

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;
    }

}
+1  A: 

Why are you expecting it to return 3 when you've only added one item to the List?

insin
Can u gimme any idea that i can get it as three in the jsp page..
Vinayak.B
+2  A: 

The problem is you have only one Alert instance in your ArrayList, but that single Alert has 3 properties: alert1, alert2, and alert3.

Take a look at the line:

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

You only have one add line, and it is not in a loop.

A possible solution:

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>
toolkit
Can u gimme any idea that i can get it as three in the jsp page..
Vinayak.B
A: 

The ArrayList contains ONLY one element Alert (the element Alert contains three Strings alerts.

David Santamaria
Can u gimme any idea that i can get it as three in the jsp page..
Vinayak.B
A: 

To get 3 Alerts you can redesign as follows. Notice that there is only one property of the alert class. You can create a new instance of the Alert for each 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;
  }
}

In the AlertGenerator:

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

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

return alt;

And on the 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>

Notice the ul's are outside the forEach loop.

Vincent Ramdhanie
Yea.. that was a good idea.. thanks for your reply..
Vinayak.B
A: 

Change your JSP to:

<%
    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>

Change AlertGenerator.java to:

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;
    }
}

Change Alert.java to:

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;
    }
}
bmatthews68
Yea..Thank you I got it perfectly..
Vinayak.B