EL 式を使用して値を取得/設定する、selectOneMenu タグについて明確にする必要があります。
<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>