h:selectOneMenu具有用户定义的对象

时间:2013-02-23 20:40:12

标签: jsf

我正在尝试实施两个<h:selectOneMenu/>

第一个包含Type 1对象。 当用户从此列表中选择对象时, 将显示第二个<h:selectOneMenu/>

这是我的代码

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>

托管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和Type2是自定义类。他们的name属性是一个字符串。

问题在于,当我从第一个列表中选择一个元素时,我得到了

    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)

我猜第一个select元素的valueChangeListener属性试图触发我的托管bean方法“valueChanged”。但它在到达之前就已经停止了(我在里面打印了一些内容,并且在抛出异常之前它没有在java控制台中显示)

问题是否与Type1和Type2不是字符串有关? 我该如何解决这个问题?


编辑:在BalusC回答之后,这是我的代码:

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>

托管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");
    }
}

转换器

@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();
    }

}

当我执行此代码时: - 从DB加载相关的飞机类型列表 - 当我选择一个时,它会从字符串转换为AircraftType(由于转换器) 但这就是全部。没有调用listener方法,也没有更新“Variant”selectOneMenu

我仍然需要你的帮助:)

1 个答案:

答案 0 :(得分:6)

简短回答:您需要创建一个Converter,它在AircrafType对象之间进行转换,以便在Java端使用,并在唯一的String之间进行转换,以便在HTML / HTTP端使用。

答案很长:仔细阅读Conversion Error setting value for 'null Converter'Primefaces selectOneMenu listener not called with Objects other than Strings

如果尚未完成,请不要忘记AircraftType中的implement equals() and hashCode()


对具体问题

无关valueChangeListener是错误的工具。请改用<p:ajax listener>。另请参阅When to use valueChangeListener or f:ajax listener?How to load second selectOneMenu on change of first selectOneMenu?