4 Replies Latest reply on Oct 7, 2009 11:55 AM by Val Sw

    Issue with suggestion Box Capital example, autocomplete alwa

    Val Sw Expert


      I am not able to catch why autocomplete() bean method is called twice in capitals sample. Please let me know what I am doing wrong...

      -- When I input anything (e.g. w) in suggestionBox, CapitalsBean.autocomplete() function is called, which calls "getCapitals().iterator()" for the value passed say "w" : OK
      -- Issue is :It calls CapitalsBean.autocomplete() which calls "getCapitals().iterator()" again on the same event with pref and suggest as null.

      I tried providing the condition checking "pref" as well as "suggest" for not null before calling "getCapitals().iterator()"

      CapitalsBean entered >>>>>>>>>>>
      CapitalsBean entered >>>>>>>>>>>
      CapitalsBean.autocomplete(), pref passed is >>>> w
      CapitalsBean.autocomplete(), suggest passed is >>>> w
      CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>>
      In CapitalsBean.getCapitals() result BEFORE return is : 50

      CapitalsBean.autocomplete(), pref passed is >>>>
      CapitalsBean.autocomplete(), suggest passed is >>>>
      CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>>
      In CapitalsBean.getCapitals() result BEFORE return is : 50

      public class CapitalsBean {
       private ArrayList<Capital> capitals = new ArrayList<Capital>();
       private ArrayList<String> capitalsNames = new ArrayList<String>();
       private List<SelectItem> capitalsOptions = new ArrayList<SelectItem>();
       private String capital = "";
       private String currentNameFilterValue;
       public List<Capital> autocomplete(Object suggest) {
       String pref = (String)suggest;
       ArrayList<Capital> result = new ArrayList<Capital>();
       System.out.println("CapitalsBean.autocomplete(), pref passed is >>>> "+pref);
       System.out.println("CapitalsBean.autocomplete(), suggest passed is >>>> "+suggest);
       if(<checked all the conditions for "pref" and "suggest" for not null>){
       System.out.println("CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>> ");
       Iterator<Capital> iterator = getCapitals().iterator();
       while (iterator.hasNext()) {
       Capital elem = ((Capital) iterator.next());
       if ((elem.getName() != null && elem.getName().toLowerCase().indexOf(pref.toLowerCase()) == 0) || "".equals(pref))
       System.out.println("CapitalsBean.autocomplete(), value is NULL DO NOT get Capitals >>>> ");
       return result;
       public CapitalsBean() {
       System.out.println("CapitalsBean entered >>>>>>>>>>> ");
       URL rulesUrl = getClass().getResource("capitals-rules.xml");
       for (Capital cap : capitals) {
       for (Capital cap : capitals) {
       capitalsOptions.add(new SelectItem(cap.getName(),cap.getState()));
       public ArrayList<Capital> getCapitals() {
       System.out.println("In CapitalsBean.getCapitals() result BEFORE return is : "+capitals.size());
       return capitals;

      <h:inputText value="#{capitalsBean.capital}" id="text" />
       <rich:suggestionbox id="suggestionBoxId" for="text" tokens=","
       suggestionAction="#{capitalsBean.autocomplete}" var="result"
       nothingLabel="No capitals found" columnClasses="center"
       <h:outputText value="#{result.name}" />