1 Reply Latest reply on Feb 16, 2008 10:13 AM by Sten Karlson

    Update problem with Seam contexts, Bindings, JSF Components

    Sten Karlson Newbie

      Hello!

      I have:
      - One LocationBean javabean with session context that holds my current location (I use selectOneMenu on the page to select my location)
      - One DDAirportMenu JSF component that I use to dynamically render a richfaces dropdown menu based on the Location defined in the LocationBean (using the binding attribute)

      The first time I load the page the DDAirportMenu calls the getAirportmenu and the dropdown menu is created witht the correct location.

      However, after this when I change the location from the selectOneMenu, the dropdown menu is not updated. Inside the DDAirportMenu the getAirportmenu method is never called (it was called on the first page load)

      The LocationBean value is updated successfully and I also retrieve data from the db.. which is also updated successfuly.

      LocationBean:

      
      @Stateful
      @Scope(ScopeType.SESSION)
      @Name("locationBean")
      public class LocationBean implements Location {
      
       @Logger private Log log;
      
       @In FacesMessages facesMessages;
      
       protected String name;
       protected String airport;
       protected String busstation;
       protected String trainstation;
       protected String harbor;
       ArrayList<String> locations = new ArrayList<String>();
       Hashtable<String, ArrayList<String>> airports_cache = new Hashtable<String, ArrayList<String>>();
       Hashtable<String, ArrayList<String>> busstations_cache = new Hashtable<String, ArrayList<String>>();
       Hashtable<String, ArrayList<String>> trainstations_cache = new Hashtable<String, ArrayList<String>>();
       Hashtable<String, ArrayList<String>> harbors_cache = new Hashtable<String, ArrayList<String>>();
      
       public LocationBean() {
       /* Locations */
       locations.add("City1");
       locations.add("City2");
      
      //City1
       l = new ArrayList<String>();
       l.add("City1: Airport 2");
       l.add("City1: Airport 1");
       airports_cache.put("City1", l);
      
       /* City 2 */
       l = new ArrayList<String>();
       l.add("City2: Airport 2");
       l.add("City2: Airport 1");
       airports_cache.put("City2", l);
      
      
       // Set defaults
       name = "city";
       airport = "City2: Airport 1";
      
      
      
       }
      
       public void location()
       {
       //implement your business logic here
       log.info("location.location() action called");
       facesMessages.add("location");
      
       }
      
       @Destroy @Remove
       public void destroy() {}
      
       //add additional action methods
      
       public String getName() {
       System.out.println("----- getting name " + name);
       return name;
       }
      
       public void setName(String name) {
       System.out.println("----- setting name to " + name);
      
      
       this.name = name;
       this.airport = this.getAirports().get(0);
       }
      
       public ArrayList<String> getLocations() {
       System.out.println("Returning locations-------------------");
       return locations;
       }
      
       public ArrayList<String> getAirports() {
       System.out.println("Returning aiports list for city: " + name + " .. size=" + airports_cache.get(name).size());
       return airports_cache.get(name);
       }
      
       public ArrayList<String> getRemainingLocations() {
       System.out.println("Returning locations-------------------");
       java.util.Iterator<String> i = locations.iterator();
       ArrayList<String> l = new ArrayList<String>();
       while (i.hasNext()) {
       String t = i.next();
       if (!t.equals(name)) {
       l.add(t);
       }
       }
      
       return l;
       }
      
       public ArrayList<String> getRemainingAirports() {
       System.out.println("Returning aiports list for city: " + name + " .. size=" + airports_cache.get(name).size());
      
       java.util.Iterator<String> i = airports_cache.get(name).iterator();
       ArrayList<String> l = new ArrayList<String>();
       while (i.hasNext()) {
       String t = i.next();
       if (!t.equals(airport)) {
       l.add(t);
       }
       }
       return l;
       }
      
       public String getAirport() {
       return airport;
       }
      
       public void setAirport(String airport) {
       this.airport = airport;
       }
      }
      
      




      JSF-component
      @Name("ddairportmenu")
      @Scope(ScopeType.EVENT)
      @AutoCreate
      public class DDAirportMenu {
      
       private org.richfaces.component.html.HtmlDropDownMenu airportmenu;
       @In(create=true)
       private Location locationBean;
      
      public HtmlDropDownMenu getAirportmenu() {
      
       System.out.println("Pimping out the menu ...");
       airportmenu= new HtmlDropDownMenu();
       HtmlOutputLabel p = new HtmlOutputLabel();
       HtmlOutputText ot = new HtmlOutputText();
       ot.setValue(locationBean.getAirport());
       HtmlGraphicImage og = new HtmlGraphicImage();
       og.setUrl("img/arrow_down.gif");
       og.setStyle("vertical-align: bottom;");
       p.getChildren().add(ot);
       p.getChildren().add(og);
       airportmenu.getFacets().put("label", p);
      
       List<String> l = locationBean.getRemainingAirports();
       Iterator<String> i = l.iterator();
      
       while (i.hasNext()) {
       HtmlMenuItem m = new HtmlMenuItem();
       String tmp = i.next();
       m.setValue(tmp);
       airportmenu.getChildren().add(m);
       }
       return airportmenu;
       }
      
      ......
      


      jsf page:
      <h:form>
       <h:selectOneMenu id="test" value="#{locationBean.name}" style="barsearch" onchange="submit();">
       <s:selectItems var="car" value="#{locationBean.locations}">
       </s:selectItems>
       </h:selectOneMenu>
      
      
      
      .....
       <h:column><rich:spacer></rich:spacer></h:column>
       <h:column>
       <rich:dropDownMenu binding="#{ddairportmenu.airportmenu}"></rich:dropDownMenu></h:column>
       <h:column><rich:spacer></rich:spacer></h:column>
      
      .....
      </h:form>