gpt4 book ai didi

jsf - h :selectOneMenu with user-defined object

转载 作者:行者123 更新时间:2023-12-04 10:36:28 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to populate options of h:selectOneMenu from database?

(5 个回答)


5年前关闭。




我正在尝试实现两个 <h:selectOneMenu/>
第一个包含类型 1 对象。
当用户从此列表中选择一个对象时,
第二个 <h:selectOneMenu/>应显示。

这是我的代码

html

<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 不是 Strings 的事实有关?
我怎样才能解决这个问题 ?

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

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

}

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

我仍然需要你的帮助:)

最佳答案

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

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

别忘了implement equals() and hashCode() AircraftType如果还没有完成。

无关 到具体问题,valueChangeListener是错误的工作工具。使用 <p:ajax listener>反而。另见 When to use valueChangeListener or f:ajax listener?How to load second selectOneMenu on change of first selectOneMenu?

关于jsf - h :selectOneMenu with user-defined object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com