Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

h:selectOneMenu with user-defined object [duplicate]

Tags:

jsf

I'm trying to implement two <h:selectOneMenu/>

The first one contains Type 1 object. When the user selects a object from this list, the second <h:selectOneMenu/> shall be displayed.

Here is my code

xhtml

<h:form>
<h:selectOneMenu value="#{handler.selectedType1}" valueChangeListener="#{handler.valueChanged}">
    <f:selectItems value="#{handler.types1}" var="type1" itemValue="#{type1.name}" itemLabel="#{type1.name}" />
    <a4j:ajax event="valueChange" render="type2List" execute="@this"/>
</h:selectOneMenu>
<h:selectOneMenu id="type2List" value="#{handler.selectedType2}" rendered="#{not empty handler.selectedType2}">
    <f:selectItems value="#{handler.types2}" var="type2" itemLabel="#{type2.name}" />
</h:selectOneMenu>
</h:form>

managed bean

@ManagedBean
@ViewScoped
public class Handler {

    private Type1 selectedType1;
    private List<Type1> types1;
    private Type2 selectedType2;
    private List<Type2> types2;

        //getters, setters

        public void valueChanged(ValueChangeEvent event) {
        variants.clear();
        if (event.getNewValue() != null) {
            System.out.println("new value: " + event.getNewValue());
                        // Fill the second list
        }
    }
}

Type1 and Type2 are to custom classes. Their name attribute is a string.

The problem is that when I select an element from the first list, I get

    24 févr. 2013 12:15:26 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer getConvertedValue
GRAVE: Could not instantiate converter for type class com.flightfaq.beans.business.AircraftType: java.lang.NullPointerException
24 févr. 2013 12:15:26 com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer getConvertedValue
GRAVE: 
java.lang.NullPointerException
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:129)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:201)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:318)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at org.richfaces.context.PartialViewExecuteVisitCallback.visit(PartialViewExecuteVisitCallback.java:55)
    at org.richfaces.context.BaseExtendedVisitContext.invokeVisitCallback(BaseExtendedVisitContext.java:321)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1612)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIForm.visitTree(UIForm.java:371)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at org.richfaces.context.ExtendedPartialViewContextImpl.executeComponents(ExtendedPartialViewContextImpl.java:237)
    at org.richfaces.context.ExtendedPartialViewContextImpl.processPartialExecutePhase(ExtendedPartialViewContextImpl.java:217)
    at org.richfaces.context.ExtendedPartialViewContextImpl.processPartial(ExtendedPartialViewContextImpl.java:196)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1167)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)

I guess that the valueChangeListener property on the first select element tries to trigger my managed bean method "valueChanged". But it is stopped before reaching it (I printed something inside, and it is not shown in the java console before the exception is thrown)

Is the problem related with the fact that Type1 and Type2 are not Strings ? How can I fix this ?


EDIT : after BalusC's answer this is my code :

xhtml

    <h:form>
        <h:selectOneMenu value="#{missionHandler.selectedAircraftType}" converter="#{missionHandler.acTypeConverter}">
            <f:selectItems value="#{missionHandler.aircraftTypes}" var="type" itemValue="#{type}" itemLabel="#{type.typeOACI}" />
            <f:ajax listener="#{missionHandler.changeSelectedAircraftType}" render="variantsMenu"/>
        </h:selectOneMenu>
        <h:selectOneMenu id="variantsMenu" value="#{missionHandler.selectedVariant}" converter="#{missionHandler.variantConverter}">
            <f:selectItems value="#{missionHandler.variants}" var="variant" itemValue="#{variant}" itemLabel="#{variant.commercialName}" />
        </h:selectOneMenu>
    </h:form>

managed bean

@ManagedBean
@ViewScoped
public class MissionHandler implements Serializable {

    private static final long serialVersionUID = 2462652101518266609L;

    private Mission mission;

    @EJB
    private AircraftTypeDao aircraftTypeDao;

    @EJB
    private VariantDao variantDao;

    private AircraftType selectedAircraftType;

    private List<AircraftType> aircraftTypes;

    private Variant selectedVariant;

    private List<Variant> variants;

    private AircraftTypeConverter acTypeConverter;

    private VariantConverter variantConverter;

    public MissionHandler() {
        /** Create an empty mission */
        mission = new Mission();
    }

    @PostConstruct
    public void init() {
        aircraftTypes = aircraftTypeDao.findAll();
    }


    public void changeSelectedAircraftType(AjaxBehaviorEvent event) {
        System.out.println("Listener call");
        variants=variantDao.find(selectedAircraftType);
        System.out.println("Listener out");
    }
}

converters

@FacesConverter(forClass=AircraftType.class)
public class AircraftTypeConverter implements Converter, Serializable {

    private static final long serialVersionUID = 7053414108213420057L;

    private final AircraftTypeDao aircraftDao = new AircraftTypeDaoImpl();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {
        System.out.println("convert to Object " + value);
        AircraftType acType = aircraftDao.find(value);
        System.out.println("got ac type " + acType.getTypeOACI());
        return acType;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        System.out.println("convert to String " + value);
        return ((AircraftType) value).getTypeOACI();
    }

}

@FacesConverter(forClass=Variant.class)
public class VariantConverter implements Converter, Serializable {

    private static final long serialVersionUID = 7053414108213420057L;

    private final VariantDao variantDao = new VariantDaoImpl();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String value) {
        System.out.println("convert to Object " + value);
        Variant variant = variantDao.find(value);
        System.out.println("got variant " + variant.getCommercialName());
        return variant;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object value) {
        System.out.println("convert to String " + value);
        return ((Variant) value).getCommercialName();
    }

}

When I execute this code: - the list of aircraft types is correclty loaded from the DB - when I select one, it is correclty converted from string to AircraftType (thanks to the converter) BUT that's all. No call to the listener method, and no update of the "Variant" selectOneMenu

I still need your help :)

like image 522
facewindu Avatar asked Feb 23 '13 20:02

facewindu


1 Answers

Short answer: you need to create a Converter which converts between an AircrafType object for usage in Java side and an unique String for usage in HTML/HTTP side.

Long answer: carefully read Conversion Error setting value for 'null Converter' and Primefaces selectOneMenu listener not called with Objects other than Strings.

Don't forget to implement equals() and hashCode() in AircraftType if not done yet.


Unrelated to the concrete problem, the valueChangeListener is the wrong tool for the job. Use <p:ajax listener> instead. See also When to use valueChangeListener or f:ajax listener? and How to load second selectOneMenu on change of first selectOneMenu?

like image 194
BalusC Avatar answered Dec 30 '22 14:12

BalusC