1

primeFaces select oneMenu Show Cases has select advance .and it has one attribute ( converter ) .

primeFaces Code:

 <p:selectOneMenu id="advanced" value="#{selectOneMenuView.theme}" converter="#{themeConverter}" panelStyle="width:180px"
                     effect="fade" var="t" style="width:160px" filter="true" filterMatchMode="startsWith">
        <f:selectItems value="#{selectOneMenuView.themes}" var="theme" itemLabel="#{theme.displayName}" itemValue="#{theme}" />

        <p:column style="width:10%">
            <h:graphicImage name="showcase/images/themeswitcher/themeswitcher-#{t.name}.png" alt="#{t.name}" styleClass="ui-theme" />
        </p:column>

        <p:column>
           <f:facet name="header">
               <h:outputText value="Name"/>
            </f:facet>
            <h:outputText value="#{t.displayName}" />
        </p:column>
         
        <f:facet name="footer">
           <p:separator />
           <h:outputText value="#{selectOneMenuView.themes.size()} themes" style="font-weight:bold;"/>
        </f:facet>
    </p:selectOneMenu>

i'm looking for where's converter="#{themeConverter}" in the code (page.xhtml and manage Bean , class) i don't find it . they don't mention it(themeConverter) in other class whether annotation or class name or variable. anybody has idea how use it ? with example thank you

1 Answers1

1

ThemeConverter source code is right here:

https://github.com/primefaces/primefaces-showcase/blob/master/src/main/java/org/primefaces/showcase/convert/ThemeConverter.java

package org.primefaces.showcase.convert;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.showcase.domain.Theme;
import org.primefaces.showcase.service.ThemeService;

@Named
@FacesConverter(value = "themeConverter", managed = true)
public class ThemeConverter implements Converter<Theme> {

    @Inject private ThemeService themeService;

    @Override
    public Theme getAsObject(FacesContext context, UIComponent component, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                return themeService.getThemes().get(Integer.parseInt(value));
            } catch(NumberFormatException e) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Theme value) {
        if(value != null) {
            return String.valueOf(value.getId());
        }
        else {
            return null;
        }
    }   
}
Melloware
  • 10,435
  • 2
  • 32
  • 62