ユーザーが製品名を入力する入力フィールドがあります。入力した名前に応じて、この製品の特定の詳細を表示する必要があります。入力を変更すると、現在のフィールド値によってデータソースから製品を取得し、を使用して製品の詳細を再レンダリングしますAJAX
。これまでのところ、ユーザーは正しい名前を入力するまで製品の詳細を表示できません。
コードサンプルは次のとおりです。
<h:form>
<h:inputText id="product" value="#{myBean.product}" required="true" converter="productConverter">
<a4j:ajax event="change" render="product, details"/>
</h:inputText>
<rich:message for="product" ajaxRendered="true"/>
<h:panelGroup id="details">
<h:outputText rendered="#{not empty myBean.product}" value="#{myBean.product.details}"/>
</h:panelGroup>
<a4j:commandButton execute="@form" render="@form" value="Validate" action="validate"/>
</h:form>
コンバーター:
public class ProductConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component, String name) {
return (name != null && ProductDataSource.projectExist(name)) ? new Product(name) : null;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value instanceof Product) ? ((Product) value).getName() : "";
}
製品は必須フィールドであるため、正しい名前を入力するまでフォームを送信できません。これまでのところ、ユーザーが間違った名前を入力すると、コンバーターはnullを返し、検証は失敗します。しかし、私が欲しいのは、フォームの送信時にのみこのフィールドを検証し、入力フィールドを再レンダリングするときに検証をスキップすることです。immediate="true"
私は両方a4j:ajax
に適用しようとしh:inputText
ましたが、何も役に立ちませんでした。何か案は?
前もって感謝します!
(私はJSF 2、Richfaces 4を使用しています)