0 Replies Latest reply on Jun 27, 2012 12:35 AM by Abdur Rahman

    h:selectManyCheckbox last selected items are not checked in JSF2

    Abdur Rahman Newbie

      I opened the xhtml page. Selected some checkboxes(services) and saved it. Then opened it in the Edit mode. But it does not check last selected checkboxes(services) even though selected items are available in the bean. Can anyone help me in this regard? Thanks.

       

      xhtml:

      --------------------------------------------------------------

       

      <h:selectManyCheckbox value="#{contractBean.selectedServices}" layout="pageDirection">

          <f:converter converterId="ServiceConverter" />

          <f:selectItems

                      value="#{contractBean.allServices}" var="service" itemLabel="#{service.name}" itemValue="#{service.name}" />   

      </h:selectManyCheckbox>

       

       

      Converter:

      --------------------------------------------------------------

       

      package com.mycompany.controller;

       

      import javax.faces.component.UIComponent;

      import javax.faces.context.FacesContext;

      import javax.faces.convert.Converter;

      import javax.faces.convert.FacesConverter;

       

      import com.mycompany.model.member.Service;

       

      @FacesConverter("ServiceConverter")

      public class ServiceConverter extends AbstractController implements Converter{

         

          public Object getAsObject(FacesContext facesContext, UIComponent component, String s) {

             

              for(Service service : UIDataUtil.getAllServices()){

                  if(s.equals(service.getName())){

                      return service;

                  }

              }

             

              return null;

          }

       

          public String getAsString(FacesContext facesContext, UIComponent component, Object o) {

              if (o == null) return null;

              return o.toString();

          }

      }

       

       

      Bean/Controller:

      --------------------------------------------------------------

      package com.mycompany.controller;

       

      import java.util.ArrayList;

      import java.util.List;

       

      import com.mycompany.controller.UIDataUtil;

      import com.mycompany.model.member.Service;

       

      public class ContractBean extends ContractController {

       

          private List<Service> selectedServices = new ArrayList<Service>(0);   

       

          public List<Service> getSelectedServices() {

              return selectedServices;

          }

       

          public void setSelectedServices(List<Service> selectedServices) {

              this.selectedServices = selectedServices;

          }

       

          public List<Service> getAllServices() {

              return UIDataUtil.getAllServices();

          }

      }