0

Mi actionListener in commandButton is not called after i select an item in a h:selectOneMenu, only it works when i press button witout any selection in selectOneMenu Below you can see my code:

<h:form id="formBtnBusquedaAnomalias">
                <table>
                    <tr>

                        <td>
                                <h:outputLabel id="idEstatusAnomalias" value="Estatus anomalias." />                                     
                                <h:selectOneMenu id="selectOneMenuEstatusAnomalias" 
                                    value = "#{busquedaAnomaliasManageBean.selectedEstatusAnomaliaVO}" >
                                    <f:converter converterId="estatusAnomaliaConverter"/>
                                    <f:selectItem itemLabel="Select one" />
                                    <f:selectItems value="#{busquedaAnomaliasManageBean.lstEstatusAnomaliaVOs}" var="estatusAnomalia"
                                        itemLabel="#{estatusAnomalia.descripcionEstatusAnomalia}" />
                                </h:selectOneMenu>                      
                        </td>

                    </tr>
                    <tr>
                        <td>    
                            <p:commandButton value="Buscar anomalias."
                                    actionListener="#{busquedaAnomaliasManageBean.buscarAnomalias()}" />
                        </td>
                    </tr>
                </table>
            </h:form>

converter class:

@FacesConverter(value = "estatusAnomaliaConverter")
public class EstatusAnomaliaConverter implements Converter{

@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    EstatusAnomaliaVO estatusAnomaliaVO = new EstatusAnomaliaVO();
    estatusAnomaliaVO.seDescripcionEstatusAnomalia(value);
    return estatusAnomaliaVO;
}

@Override
public String getAsString(FacesContext ctx, UIComponent component, Object object) {
    String temp = "";
            if(!object == null)
            {
                 temp = object.toString();
            }
    return temp;
}

}

ManageBean:

@ManagedBean(name=ConstantesManageBeans.MANAGE_BEAN_BUSQUEDA_ANOMALIAS)
@RequestScoped
public class BusquedaAnomaliasManageBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -1955257440354260445L;


public void buscarAnomalias(){
             //...some code here
}
]
Aredalo
  • 23
  • 2

1 Answers1

0

Your buscarAnomalias method doesn't follow the signature of an ActionListener method. This is the signature:

public void actionListener(ActionEvent event) {
    // ...
}

In order to make it work, just add the ActionEvent parameter to your method:

public void buscarAnomalias(ActionEvent event) {
    //...some code here
}

If I were you, I would use action instead of actionListener to perform managed bean operations:

<p:commandButton value="Buscar anomalias."
   action="#{busquedaAnomaliasManageBean.buscarAnomalias()}" />

Note that using this approach, you don't need to add the ActionEvent event parameter to your method.

More info:

Another problem in your code is that your FacesConverter is failing when retrieving the value of the selected item. This is because you haven't set itemValue in your <f:selectItems>:

<h:selectOneMenu id="selectOneMenuEstatusAnomalias" 
    value="#{busquedaAnomaliasManageBean.selectedEstatusAnomaliaVO}">
    <f:converter converterId="estatusAnomaliaConverter"/>
    <f:selectItem itemLabel="Select one" />
    <f:selectItems value="#{busquedaAnomaliasManageBean.lstEstatusAnomaliaVOs}"
         var="estatusAnomalia" itemValue="#{estatusAnomalia}"
         itemLabel="#{estatusAnomalia.descripcionEstatusAnomalia}" />
</h:selectOneMenu>
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • This restriction no longer persists : https://javaserverfaces.java.net/nonav/docs/2.0/pdldocs/facelets/h/commandButton.html. Might also depend on OP's version of JSF though – kolossus Apr 24 '13 at 03:21
  • @kolossus is your link broken? I try to access but it only shows a blank page and keeps loading. – Luiggi Mendoza Apr 24 '13 at 14:22
  • Can't believe it myself but it seems that link died overnight. Try this https://javaserverfaces.java.net/nonav/docs/2.0/vdldocs/facelets/h/commandButton.html – kolossus Apr 24 '13 at 14:36
  • @Luiggi Mendoza i made changes as your answer and the same error continue. let me explain better: when i press the button the action method buscarAnomalias() works fine then when y select an item in the selectOneMenu and press again button the action method dosen´t no anything – Aredalo Apr 24 '13 at 14:51
  • @Aredalo I have also added a section about the ``, have you tried it? – Luiggi Mendoza Apr 24 '13 at 14:53
  • @Luiggi Mendoza yes, i changed the attribute actionListener to action="#{busquedaAnomaliasManageBean.buscarAnomalias()}" and also i added itemValue="#{estatusAnomalia}" attribute in tag and tried again – Aredalo Apr 24 '13 at 15:53
  • @Aredalo looks like a problem in your `FacesConverter`. Something is failing when converting the selected object into an instance of `estatusAnomalia`. Also, make sure that `value=#{busquedaAnomaliasManageBean.selectedEstatusAnomaliaVO}` is from the same type of `#{estatusAnomalia}`. – Luiggi Mendoza Apr 24 '13 at 16:19
  • @LuiggiMendoza what does itemValue = "#{estatusAnomalia}" mean? because i just added because of your answer but i dont really understand it. Converter looks ok because i put some system.out inside and this method is working but this method is called when i press the commandButton, this converter is not called whean i change selection in selectOneMenu – Aredalo Apr 24 '13 at 16:43
  • @LuiggiMendoza is it necesary also to implement hashcode and equals method in EstatusAnomaliaVO? – Aredalo Apr 24 '13 at 20:03