1 Reply Latest reply on Aug 9, 2013 2:16 AM by Dominik Grupp

    Reuse a bean in viewScope on the same page several times

    Dominik Grupp Newbie

      On a JSF page data is displayed which comes from a bean in view scope. Now I would like to display the same area twice on the same page containing different data, e.g. in the first section a firstCar and in the second area a secondCar. The bean containing the information about a single car is in view scope and the EL name is "car".

       

      The problem is that the carBean has only one EL name. How do I address it in the JSF page to refer to different instances? Would it be ok to use "search.carFirstArea.name" and "search.carSecondArea.name"? Will there really be two instances of the view scoped carBean? Will I have to duplicate the CarBean and give the second instance another EL name?

       

      Example code:

       

      -- first area
      <h:outputText value=#{car.name}">
      -- second area
      <h:outputText value=#{car.name}">
      

       

      @ManagedBean(name = "search")
      @ViewScoped
      public class SearchBean {
      
      
                @ManagedProperty(value = "#{car}")
                private Car carFirstArea;
        
                @ManagedProperty(value = "#{car}")
                private Car carSecondArea;
        
                public String search() {
                          carFirstArea = ... dao search ...
                          carSecondArea = ... dao search ...
                          return null;
                }
        
                ... getters / setters ...
      }
      

       

       

      @ManagedBean(name = "car")
      @ViewScoped
      public class Car {
                private String name;
        
                public String getName() {
                          return name;
                }
        
                public String setName(String name) {
                          this.name = name;
                }
      }