Question

Je ne parviens pas à obtenir le bon nombre d'éléments dans ArrayList alt dans la page JSP ci-dessous. Lorsque je visualise le fichier JSP, il indique que la taille est 1 (<%=alt.size()%>), alors qu'il devrait être 3; Je pense que j'ajoute cette méthode au tableau de la classe générateur, donc je ne comprends pas pourquoi elle montre 1.

Ceci est ma page 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>

Cette classe génère les alertes:

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

}

Ceci est ma classe de haricots:

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

}
Était-ce utile?

La solution

Pour obtenir 3 alertes, vous pouvez modifier la conception comme suit. Notez qu'il n'y a qu'une seule propriété de la classe d'alerte. Vous pouvez créer une nouvelle instance de l'alerte pour chaque alerte.

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

Dans AlertGenerator:

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

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

return alt;

Et sur le 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>

Notez que les ul sont en dehors de la boucle forEach.

Autres conseils

Le problème est que vous n’avez qu’une seule instance d’alerte dans votre ArrayList, mais cette seule alerte a 3 propriétés: alert1, alert2 et alert3.

Regardez la ligne:

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

Vous n'avez qu'une seule ligne d'ajout, et ce n'est pas dans une boucle.

Une solution possible:

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>

Pourquoi vous attendez-vous à ce qu'il soit renvoyé 3 lorsque vous avez add édité un seul élément sur List?

La liste de tableaux contient UNIQUEMENT un élément Alert (l'élément Alert contient trois alertes Strings.

Modifiez votre JSP en:

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

Remplacez AlertGenerator.java par:

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

Remplacez Alert.java par:

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;
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top