Primefaces Converter not working with dialog

155 views Asked by At

I have a problem with a SelectOneMenu Converter in Primefaces because not display the correct value in a dialog box. The next code shows what is the problem.

SELECTONEMENU CALL

**<p:column headerText="Editar" styleClass="centrarColumna">
                 <p:commandButton icon="pi pi-pencil" update=":edicionSindicatura:contenidoEdicionSindicatura"
                    oncomplete="PF('edicionSindicatura').show()" process="@this">
                    <f:setPropertyActionListener value="#{listadoSI}" target="#{busquedaMb.sindicatura}" /> 
                    <p:resetInput target=":edicionSindicatura:contenidoEdicionSindicatura" />
                </p:commandButton>
        </p:column>**

DIALOG BOX

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    
    >


<h:form id="edicionSindicatura">
        <p:dialog header="Edición de Datos" showEffect="fade" modal="true" responsive="true" widgetVar="edicionSindicatura">
            
        
             <p:outputPanel id="contenidoEdicionSindicatura">
                <p:outputPanel rendered="#{not empty busquedaMb.sindicatura}">
               
                                        
               
               <div class="ui-fluid">
        
               <h:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank ui-fluid">
               
               <h:panelGroup>
             
        ......                          
                
        <h:panelGroup>
               
        <p:outputLabel value="#{mens.edicionSindicaturaOnomasticos}"/>
                
        <p:selectOneMenu id="onomastico" value="#{busquedaMb.sindicatura.auxOnomastico}" converter="selectOneMenuConverter" autoWidth="false">
        <f:selectItem itemLabel="--Elegir Onomástico--" itemValue=""/>
        <f:selectItems value="#{onomasticoMb.listaOnomastico}" var="lista" itemLabel="#{lista.descripcion}" itemValue="#{lista}"/>
                                
        </p:selectOneMenu>
                                
        </h:panelGroup>
                                                            
        .....
               
               </h:panelGrid>     
                </div>    
              
            </p:outputPanel>
            </p:outputPanel>
                        
        <f:facet name="footer">
                <p:commandButton value="Guardar" icon="pi pi-check" actionListener="#{busquedaMb.guardarSindicatura()}" update="contenidoEdicionSindicatura" process="contenidoEdicionSindicatura @this" />
                <p:commandButton value="Cancel" icon="pi pi-times" onclick="PF('edicionSindicatura').hide()" type="button" />
            </f:facet>
        </p:dialog>
      

        
    </h:form>

</ui:composition>

CONVERTER

package es.cortes.archivoCortes.view.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItem;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * The Class SelectOneMenuConverter.
 */
@FacesConverter("selectOneMenuConverter")
public class SelectOneMenuConverter implements Converter {
 
    @Override
    public Object getAsObject(final FacesContext arg0, final UIComponent arg1, final String objectString) {
        if (objectString == null) {
            return null;
        }
 
        return fromSelect(arg1, objectString);
    }
 
    /**
     * Serialize.
     *
     * @param object
     *            the object
     * @return the string
     */
    private String serialize(final Object object) {
        if (object == null) {
            return null;
        }
        return object.getClass() + "@" + object.hashCode();
    }
 
    /**
     * From select.
     *
     * @param currentcomponent
     *            the currentcomponent
     * @param objectString
     *            the object string
     * @return the object
     */
    private Object fromSelect(final UIComponent currentcomponent, final String objectString) {
 
        if (currentcomponent.getClass() == UISelectItem.class) {
            final UISelectItem item = (UISelectItem) currentcomponent;
            final Object value = item.getValue();
            if (objectString.equals(serialize(value))) {
                return value;
            }
        }
 
        if (currentcomponent.getClass() == UISelectItems.class) {
            final UISelectItems items = (UISelectItems) currentcomponent;
            final List<Object> elements = (List<Object>) items.getValue();
            for (final Object element : elements) {
                if (objectString.equals(serialize(element))) {
                    return element;
                }
            }
        }
 
 
        if (!currentcomponent.getChildren().isEmpty()) {
            for (final UIComponent component : currentcomponent.getChildren()) {
                final Object result = fromSelect(component, objectString);
                if (result != null) {
                    return result;
                }
            }
        }
        return null;
    }
 
    @Override
    public String getAsString(final FacesContext arg0, final UIComponent arg1, final Object object) {
        return serialize(object);
    }

When the button is fired the dialog is open but the selectonemenu value isn't show in the correct form. DialogThis is in the attached file.

Any idea for to solve this question? I have wasted a lot of time trying to find the solution.

0

There are 0 answers