gpt4 book ai didi

jsf - selectOneMenu 选中值

转载 作者:行者123 更新时间:2023-12-02 22:12:11 26 4
gpt4 key购买 nike

好吧,我需要对 selectOneMenu 标签进行一些说明,使用 EL 表达式来获取/设置值

        <h:selectOneMenu id="variantsMenu"
value="#{missionHandler.selectedVariant}"
converter="#{missionHandler.variantConverter}">
<f:selectItem itemLabel="-- Select a Variant --" />
<f:selectItems value="#{missionHandler.variants}" var="variant"
itemValue="#{variant}" itemLabel="#{variant.commercialName}" />
</h:selectOneMenu>

我有这段代码,它显示一个列表,其中包含(“-- 选择一个变体--”,变体 1,变体 2,...变体 n)当我单击 Variant 时,我希望 missionHandler managedBean 中的 selectedVariant 属性会自动更新。但这种情况并非如此。甚至没有调用 variantConverter 转换器将字符串“variant.commercialName”转换为真正的 Variant 对象。变体对象实现了 hashCode()equals()

  • 我缺少什么 EL 表达式/标签的概念?
  • 我的更新问题的解决方案是什么?

    编辑:managedBean(简化)

    @ManagedBean
    @ViewScoped
    public class MissionHandler implements Serializable {

    private static final long serialVersionUID = 2462652101518266609L;

    private List<FlightFeasibilityException> exceptions;

    @EJB
    private VariantDao variantDao;

    private Variant selectedVariant;

    private List<Variant> variants;

    private VariantConverter variantConverter;

    public MissionHandler() {
    /** Create an empty list of exceptions */
    exceptions = new ArrayList<FlightFeasibilityException>();
    }

    @PostConstruct
    public void init() {
    System.out.println("init done");
    }

    public List<FlightFeasibilityException> getExceptions() {
    return exceptions;
    }

    public void setExceptions(List<FlightFeasibilityException> exceptions) {
    this.exceptions = exceptions;
    }

    public Variant getSelectedVariant() {
    return selectedVariant;
    }

    public void setSelectedVariant(Variant selectedVariant) {
    this.selectedVariant = selectedVariant;
    }

    public List<Variant> getVariants() {
    return variants;
    }

    public void setVariants(List<Variant> variants) {
    this.variants = variants;
    }

    public VariantConverter getVariantConverter() {
    return variantConverter;
    }

    public void setVariantConverter(VariantConverter variantConverter) {
    this.variantConverter = variantConverter;
    }

转换器

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

private static final long serialVersionUID = 7053414108213420057L;

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

}

和xhtml文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Mission Page</title>
<link href="./css/styles.css" rel="stylesheet" type="text/css" />
</h:head>
<h:body>
<rich:panel>
<f:facet name="header">
Mission Information
</f:facet>
<a4j:outputPanel layout="block">
<h:form>
<h:outputText style="font-weight: bold;" value="Mission Id: " />
<h:inputText id="missionId" label="missionId"
value="#{missionHandler.mission.id}" disabled="true"
style=" width : 50px;">
</h:inputText>
<h:outputText style="font-weight: bold;" value="Mission Status: " />
<h:inputText id="missionStatus" label="missionStatus"
value="#{missionHandler.mission.status}" disabled="true">
</h:inputText>
</h:form>
<br />
<h:form>
<h:selectOneMenu value="#{missionHandler.selectedAircraftType}"
converter="#{missionHandler.acTypeConverter}">
<f:selectItem itemLabel="-- Select an A/C Type --" />
<f:selectItems value="#{missionHandler.aircraftTypes}" var="type"
itemValue="#{type}" itemLabel="#{type.typeOACI}" />
<f:ajax listener="#{missionHandler.changeSelectedAircraftType}"
render="@form" />
</h:selectOneMenu>
<h:selectOneMenu id="variantsMenu"
value="#{missionHandler.selectedVariant}"
converter="#{missionHandler.variantConverter}"
rendered="#{not empty missionHandler.selectedAircraftType}">
<f:selectItem itemLabel="-- Select a Variant --" />
<f:selectItems value="#{missionHandler.variants}" var="variant"
itemValue="#{variant}" itemLabel="#{variant.commercialName}" />
</h:selectOneMenu>
</h:form>
</a4j:outputPanel>
</rich:panel>
….. A LOT OF OTHER THINGS …..
</h:body>
</html>

最佳答案

关于您的代码的几点说明:

  1. 在托管 bean 的构造函数中做一些事情==糟糕的设计,请改用 @PostConstructor

  2. 在您的转换器中手动实例化 DAO==糟糕的设计。理想情况下,您的 DAO 应该是一个可以注入(inject)转换器的托管对象(最好是 EJB)。作为一个托管对象意味着它(理论上)可能是一个单例并由容器正确管理。至少这样,你不会为此感到难过。

  3. 尝试手动管理转换器。将转换器声明为独立组件并让容器完成它的工作,这样您就可以避免不必要的调试。

  4. variantsMenu 没有附加 ajax 处理程序,因此在提交整个表单之前,您不会在支持 bean 中更新值

    <h:selectOneMenu id="variantsMenu"
    value="#{missionHandler.selectedVariant}"
    converter="#{variantConverter}"
    rendered="#{not empty missionHandler.selectedAircraftType}">
    <f:ajax listener="#{missionHandler.changeVariant}" render="@form" />
    <f:selectItem itemLabel="-- Select a Variant --" />
    <f:selectItems value="#{missionHandler.variants}" var="variant"
    itemValue="#{variant}" itemLabel="#{variant.commercialName}" />
    </h:selectOneMenu>

关于jsf - selectOneMenu 选中值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15076178/

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