jstl - forEach, bean having a list of beans as property

11,127

I have just attempted to replicate the issue that you are having and find that it works without error. Here is my code:

The Assay class:

 public class Assay {

     private String decision;
     private String comment;    

     Assay(){}

     Assay(String decision, String comment){
         setComment(comment);
         setDecision(decision);
     }

     public String getComment() {
         return comment;
     }
     public void setComment(String comment) {
         this.comment = comment;
     }
     public String getDecision() {
         return decision;
     }
     public void setDecision(String decision) {
         this.decision = decision;
     }  
 }

The Experiment class:

 public class Experiment {

     private ArrayList<Assay> assays = new ArrayList<Assay>();

     public Experiment() {      
         assays.add(new Assay("c1","d1"));
         assays.add(new Assay("c2","d2"));          
     }

     public ArrayList<Assay> getAssays() {
         return assays;
     }

         public void setAssays(ArrayList<Assay> assays) {
             this.assays = assays;
         }  
 }

The JSP:

 <%
      Experiment e = new Experiment();
      pageContext.setAttribute("exp", e);
 %>

 <c:forEach items="${exp.assays}" var="ay">
      <tr><td>${ay.decision}</td><td>${ay.comment}</td></tr>
 </c:forEach>

The output is: c1d1 c2d2

Compare what I have done above with your code and see if you can identify any differences. If you continue to have problems post your code here.

Share:
11,127

Related videos on Youtube

numfar
Author by

numfar

Updated on September 14, 2022

Comments

  • numfar
    numfar over 1 year

    I have a bean called Experiment which has a property that is a arraylist of beans of a type called Assay.

    In a jsp I want to iterate over the list of assays stored in an Experiment bean, which I try to do by doing this:

                    <c:forEach items="${exp.assays}" var="ay">
                        <tr><td>${ay.decision}</td><td>${ay.comment}</td></tr>
                    </c:forEach>
    

    'descision' and 'comment' are properties of the Assay bean.

    But this doesn't work. I get the error:

    SEVERE: javax.el.PropertyNotFoundException: The class 'beans.Experiment' does not have the property 'decision'.
    

    as if the items in the 'exp.assays' list where of bean type Experiment and not of Assay as I would expect.

    So I tried accessing Experiment property (eid, which I know I can access by directly writing ${exp.eid}) in the forEach-loop instead, like this:

                    <c:forEach items="${exp.assays}" var="ay">
                        <tr><td>${ay.eid}</td></tr>
                    </c:forEach>
    

    but then I got the error message:

    SEVERE: javax.el.PropertyNotFoundException: The class 'beans.Assay' does not have the property 'eid'. 
    

    Anyone who knows what is going on or how I could fix it?

    The full bean code is pretty long but here are the parts that I think are relevant here

    Experiment:

    public class Experiment extends dataItem implements Serializable {
    
        private String eid;
        private List<Assay> assays = new ArrayList();
    
        public String getEid() {
            return eid;
        }
    
        public void setEid(String id) {
            eid = id;
        }  
    
        public List<Assay> getAssays() {
            return assays;
        }
    
        public void setAssays(List<Assay> assays) {
            this.assays = assays;
        }
    
    }
    

    And Assay:

    public class Assay extends dataItem implements Serializable {
    
        private Integer id;
        private stock antigen;
        private String decision;
        private String comment;
        private String freeAb;    
        private List<AssayData> data;
        private List<conjugate> conjugates = new ArrayList();
    
        public List<conjugate> getConjugates() {
            return conjugates;
        }
    
        public void setConjugates(List<conjugate> conjugates) {
            this.conjugates = conjugates;
        }   
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public stock getAntigen() {
            return antigen;
        }
    
        public void setAntigen(String id) {
            stock s = new stock();
            s.setSID(id);
    
            this.antigen = s;
        }
    
        public String getDecision() {
            return decision;
        }
    
        public void setDecision(String decision) {
            this.decision = decision;
        }
    
        public String getComment() {
            return comment;
        }
    
        public void setComment(String comment) {
            this.comment = comment;
        }
    
        public String getFreeAb() {
            return freeAb;
        }
    
        public void setFreeAb(String freeAb) {
           this.freeAb = freeAb;
        }
    
        public List<AssayData> getData() {
           return data;
        }
    
        public void setData(List<AssayData> data) {
           this.data = data;
        }   
    }