7

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

第一个包含类型 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>

托管豆

@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 用于自定义类。他们的名字属性是一个字符串。

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

    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)

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

问题是否与 Type1 和 Type2 不是 Strings 的事实有关?我怎样才能解决这个问题 ?


编辑:在 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>

托管豆

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

}

当我执行此代码时: - 飞机类型列表是从数据库中正确加载的 - 当我选择一个时,它是从字符串正确转换为 AircraftType(感谢转换器)但仅此而已。不调用侦听器方法,也不更新“变体”selectOneMenu

我仍然需要你的帮助:)

4

1 回答 1

7

简短的回答:您需要创建一个在 Java 端使用的对象和HTML/HTTP 端使用的唯一对象Converter之间进行转换。AircrafTypeString

长答案:仔细阅读“null Converter”和Primefaces selectOneMenu 侦听器的转换错误设置值,该侦听器未使用 Strings 以外的 Objects 调用。

如果尚未完成,请不要忘记实施equals()hashCode()in 。AircraftType


与具体问题无关valueChangeListener,这是工作的错误工具。改为使用<p:ajax listener>。另请参阅何时使用 valueChangeListener 或 f:ajax 侦听器?以及如何在更改第一个 selectOneMenu 时加载第二个 selectOneMenu?

于 2013-02-24T11:59:20.547 回答