Search code examples
jsffacelets

How can I present values of <h:selectManyCheckbox> in an input field?


I'm creating a website that is using jsf, html, java, and JavaDB. One of my pages will list a number of images that represents menus a customer can select. At the bottom of the page there are a selectManyCheckbox with the options corresponding with the images. When a customer has selected a combination of menus, an "Order" button would guide the customer to the next page. On this page there are listed additional information about the customer (retrieved from the database) such as: Customer number, Contact name, etc. In additional to that the selected menu should be listed in the table, and the number of row listed should be the same as the number of menus selected. My main problem is that I want to use the data collected from the selectManyCheckbox as my input in the selected menu field, and the rest is collected from the database (That I have done and it works). How can I get the values as separate values? I am only able to use them in a output text and not as values in a input field.

Java code:

        @Named
        @RequestScoped
        public class OrderBean implements java.io.Serializable {
        private Set<String> menus = new TreeSet<String>();

        public Set<String> getMenus() { 
           return menues;
        }

        public void setMenus(Set<String> newValue){
           menus = newValue;
        }

        public SelectItem[] getMenuesItems(){
           return menuesItems;
        }

        private static SelectItem[] menuesItems = {
           new SelectItem("Menu 1"),
           new SelectItem("Menu 2"),
           new SelectItem("Menu 3")
        };
   }

JSF, HTML:

      <div class="clear"></div>
      <div id="frame">
        <div id="frameCheckBox"><h:commandButton value="#{mld.Order}" action="Order"/>
             <h:selectManyCheckbox value="#{orderBean.menus}">
                  <f:selectItems value="#{orderBean.menusItems}"/>
             </h:selectManyCheckbox>
        </div>
       </div>

Solution

  • Replace Set<String> by List<String> or String[], so that you can generate the desired input components with help of an <ui:repeat> as follows:

    <ui:repeat value="#{orderBean.menus}" varStatus="loop">
        <h:inputText value="#{orderBean.menus[loop.index]}" />
    </ui:repeat>
    

    Like <h:dataTable>, it namely doesn't support Set until the upcoming JSF 2.2. Please also note that you really need to reference the input value by list/array index instead of via <ui:repeat var>, for the simple reason that String is immutable and doesn't have a setter method.

    Last but not least, this construct will fail during processing the form submit as your bean is request scoped and thus gets garbaged directly after the form is presented to the enduser. Put it in a bit wider scope, e.g the view, conversation or perhaps the session scope. Otherwise JSF won't be able to set the submitted values in the initial list/array.