0

I've already checked BalusC suggestions on commandButton/commandLink/ajax action/listener method not invoked or input value not updated, But i can't get my commandButton to get fired whenever i add a selectOneMenu dropdownlist

Here is my form:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 template="/template/template.xhtml">

 <ui:define name="content">
  <h:form>
   <h:messages />
   <h:panelGrid columns="2">
    <h:outputLabel value="Cin" />
    <h:inputText value="#{addEmployeeBean.cin}" />
    <h:outputLabel value="Nom" />
    <h:inputText value="#{addEmployeeBean.nom}" />
    <h:outputLabel value="Prenom" />
    <h:inputText value="#{addEmployeeBean.prenom}" />
    <h:outputLabel value="Login" />
    <h:inputText value="#{addEmployeeBean.login}" />
    <h:outputLabel value="Password" />
    <h:inputSecret value="#{addEmployeeBean.password}" />
    <h:outputLabel value="Id_Contrat" />
    <h:inputText value="#{addEmployeeBean.idContrat}" />
    <h:outputLabel value="Salaire" />
    <h:inputText value="#{addEmployeeBean.salaire}" />

    <h:outputLabel value="laboratoire:" />

    <h:selectOneMenu value="#{addEmployeeBean.laboSelectionne}">
     <f:selectItems value="#{addEmployeeBean.labos}" var="lab"
      itemLabel="#{lab.nom}" itemValue="lab">
     </f:selectItems>
    </h:selectOneMenu>

    <h:commandButton action="#{addEmployeeBean.ajouterEmploye()}"
     value="Ajouter" />

   </h:panelGrid>
  </h:form>

 </ui:define>

</ui:composition>

And here is my managed bean:

@ManagedBean
@RequestScoped
public class AddEmployeeBean {

 private String cin;
 private String nom;
 private String prenom;
 private String login;
 private String password;
 private int idContrat;
 private float salaire;
 private List<Labo> labos;
 private Labo laboSelectionne;


 @EJB
 GererLaboServiceLabo gererLaboServiceLocal;
 
 @EJB
 GererEmployeeServiceLocal employeeServiceLocal;

 @PostConstruct
 public void init(){
  labos= gererLaboServiceLocal.listLabo();
  laboSelectionne = new Labo();
  
 }
 
 public String ajouterEmploye() {
  Employe employe = new Employe();
  Contrat contrat = new Contrat();
  contrat.setId(idContrat);
  contrat.setSalaire(salaire);
  employe.setCin(cin);
  employe.setContrat(contrat);
  employe.setLogin(login);
  employe.setNom(nom);
  employe.setPassword(password);
  employe.setPrenom(prenom);
  System.out.println(laboSelectionne.getNom());
  employe.setLabo(laboSelectionne);
  employeeServiceLocal.creerEmployee(employe);
  return null;
 }
Community
  • 1
  • 1
imenb
  • 1
  • 4
  • Check your `f:selectItems` `itemValue` attribute. You have it set to a string literal. Outside of that are you getting anything in your logs or any FacesMessages? – axemoi Apr 07 '17 at 16:13
  • You are right itemValue should be set to #{lab} which i did but i didn't change a thing. I'm not getting anything in the server's log neither any FacesMessages.... – imenb Apr 09 '17 at 20:24
  • Do you have a converter registered for the `selectOneMenu`? – axemoi Apr 10 '17 at 12:49
  • No i don't have any converter – imenb Apr 10 '17 at 18:22

1 Answers1

0

Your application isn't making it past the Apply Request Values Phase because of a conversion error. You need to convert the string #{lab} back into a Labo instance. JSF is only capable of doing implicit conversion for primitive, primitive wrappers, BigDecimal, and BigInteger. For everything else you have to create a custom converter (or make use of one of the <f:convert.../> tags provided by jsf).

Normally in these cases I will put all the selectable objects in a HashMap keyed by their database Id. So instead of private List<Labo> labos; you will have private HashMap<String, Labo> labos; where String is the unique ID. Now your getLabos will be

public Collection<Labo> getLabos(){
    return labos.values();
}

Assuming Labo has a field id for the primary key, <f:selectItems... becomes

<f:selectItems value="#{addEmployeeBean.labos}" var="lab" itemLabel="#{lab.nom}" 
    itemValue="#{lab.id}"/>

Now you can create a converter that will map the String value submitted with the form into a Labo instance. For this example I just created a public method that returns an anonymous instance of Converter; this would go in your ManagedBean.

    public Converter getConverter(){
        return new Converter(){
            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {              
                return labos.get(value);
            }
            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {  
                if(value instanceof Labo){
                    return ((Labo)value).getKey();
                }
                return value == null ? null : value.toString();
            }
        };
    }

Finally you can register the converter on your selectOneMenu tag

<h:selectOneMenu value="#{addEmployeeBean.laboSelectionne}" 
    converter="#{addEmployeeBean.getConverter()">

With all that said, it can also be simpler to just use Strings in your selectOneMenu and then when you go to set the Lab just look it up in your HashMap

employe.setLabo(labos.get(laboSelectionne));
axemoi
  • 211
  • 1
  • 8