好吧,我需要澄清一下 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>
我有这段代码,它显示一个列表,其中包含 ("-- Select a Variant--", Variant 1, Variant 2, ... Variant n) 当我单击 Variant 时,我希望managedBean 中的selectedVariant
属性是missionHandler
自动更新。但这种情况并非如此。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>