1

I have a problem with the selectOneMenu component, I'm trying to call a method in the change event of the selectOneMenu but unfortunately something is wrong.
When I try the code bellow calling the method actualizaDistritosNaturalidade from the ajax listener the method is not called. It only calls the method when I choose the noSelectionOption.

When I see the HTTP response I see the following error :

<extension ln="primefaces" type="args">{"validationFailed":true}</extension>

I try the approach bellow and even with the converter bellow.

I have the following template file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <h:outputStylesheet name="style.css" library="default" />
</h:head>
<h:body>
    <div id="page">
        <div id="header">
            <ui:insert name="header">
                <h:form>                    
                    <p:menubar id="menu">
                        <p:menuitem>
                            <h:link outcome="/default.xhmtl">                               
                                <p:outputLabel value="FuncionariosGest" />
                            </h:link>
                        </p:menuitem>
                        <p:submenu label="Colaborador" icon="ui-icon-document">
                            <p:menuitem value="Criar Colaborador"
                                url="/create-colaborator/create-colaborator.xhtml" />                       
                        </p:submenu>
                    </p:menubar>
                </h:form>
            </ui:insert>
        </div>
        <div id="contentWrap" class="ui-fluid">
            <div id="content">
                <ui:insert name="content"></ui:insert>
            </div>
        </div>
        <div id="footer">
            <ui:insert name="footer">
                bla bla 
            </ui:insert>
        </div>
    </div>
</h:body>
</html>

And the following view :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
    <ui:define name="content">
        <h:form>
            <h:messages id="messages" />
            <div class="ui-g-2">
                <p:outputLabel for="birthProvince" value="Provincia :" />
            </div>
            <div class="ui-g-4">
                <p:selectOneMenu id="birthProvince"
                    value="#{criarProfessionalController.profissional.provinciaDeNaturalidade}">
                    <p:ajax event="change" process="@this"
                        listener="#{criarProfessionalController.actualizaDistritosNaturalidade}" />
                    <f:selectItem itemLabel="Selecione uma opção" itemValue="#{null}"
                        noSelectionOption="true" />
                    <f:selectItems
                        value="#{criarProfessionalController.provinciasItem}" />
                </p:selectOneMenu>
            </div>
        </h:form>
    </ui:define>
</ui:composition>
</html>

In my Managed bean I have the following method(removed other pieces) :

@PostConstruct
public void init() {
    this.profissional = new Professional();
    this.setTipoDeDocumentos(tipoDocumentoDAO.buscaTodos());
    this.setProvincias(provinciaDAO.buscaTodos());
    this.fillProvinciasItem();
}

public void actualizaDistritosNaturalidade() {
    if (this.profissional.getProvinciaDeNaturalidade() != null) {
        this.setDistritosNaturalidade(
                this.distritoDAO.buscaTodosPorProvincia(this.profissional.getProvinciaDeNaturalidade()));
    }
}
private void fillProvinciasItem()
{
    this.setProvinciasItem(new ArrayList<SelectItem>());
    for(Provincia provincia : this.getProvincias())
    {
        this.getProvinciasItem().add(new SelectItem(provincia, provincia.getNome()));
    }
}

the converter :

@Override
    public Object getAsObject(FacesContext ctx, UIComponent arg1, String provinceID) {

        try {
            Long id = Long.valueOf(provinceID);
            ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(),
                    "#{criarProfessionalController}", CriarProfessionalController.class);

            CriarProfessionalController profissionalController = (CriarProfessionalController) vex
                    .getValue(ctx.getELContext());
            return profissionalController.getProvinciaByID(id);
        } catch (NumberFormatException e) {

        }
        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

        if (arg2 != null) {
            return arg2.toString();
        }
        return null;

    }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Rabbit
  • 152
  • 2
  • 11
  • update the h:messages component in the ajax response and see what it says there. And try a plain jsf h:selectOneMenu. Does it fail too? Most likely your converter is failing... – Kukeltje Oct 04 '17 at 07:52
  • thank you . It helped. The problem now is that in my converter I was expected to receive the ID of my entity(provincia), but JSF is returning the toString of the provincia entity Object. Should my toString return the ID of the Entity? When I try to set the ItemValue to the ID of the Entity, it does not work, because JSF cannot set the value using the setProvincia(Provincia). Whats the best approach for this situation? – Rabbit Oct 04 '17 at 08:12
  • Search stackoverflow... Lots of related/similar questions now you know what to look for... – Kukeltje Oct 04 '17 at 11:09

0 Answers0