簡體   English   中英

ajax4jsf actionlistener方法中的jsf輸入驗證

[英]jsf input validation inside ajax4jsf actionlistener method

我正在學習JSF + Seam Framework + AJAX4JSF並構建一個應用程序。 我的應用程序有一個稱為條形碼的輸入字段,用戶可以對其進行掃描/輸入。 輸入條形碼后,我將使用ajax4jsf進行ajax調用以調用偵聽器方法。 偵聽器方法應驗證輸入的條形碼。 它根據數據庫驗證條形碼,如果無效,則返回FacesMessage。 如果條形碼有效,則它將使用條形碼更新數據庫並返回JSF。 應該調用ajax4jsf中提到的render塊來渲染頁面,並且應該執行oncomplete中提到的javascript方法。

我的代碼如下。

JSF代碼:

<html ....
  xmlns:a="http://richfaces.org/a4j"
  xmlns:p="http://primefaces.org/ui">   

//javascript function
function renderPlate(data) {
    ......          
}

<h:form id="myForm">
    <h:outputText value="Scan barcode:" />
    <h:inputText id="inputBarcode" value="#{myBean.inputBarcode}"> 
        <a:ajax event="change" render="plateGrid" listener="#{myBean.addBarcodedItemToCheckout}" oncomplete="renderPlate()"/>
    </h:inputText>
</h:form>

<a:outputPanel id="plateGrid">
    <h:messages id="messages" style="color:red;margin:8px;" autoUpdate="true"/>
    ....
</a:outputPanel>

我的煤豆:

@Scope(ScopeType.CONVERSATION)
@Name("plateMakerBean")
@Stateless 
public class PlateMakerAction {//implements PlateMaker {

private String inputBarcode;

public void addBarcodedItemToPlate() {

    boolean invalidBarcode = false;

    List<Item> items = em.createQuery("select m from Item i where barcode= #{inputBarcode}").getResultList();

    if(items == null || items.size() == 0) {
        invalidBarcode = true;
    } 

    if(invalidBarcode == true) {
        FacesMessages.instance().add("Barcode is not a valid barcode.");
    }
}

}

我面臨的問題/問題是這樣的:

  1. 當我返回FacesMessages時,oncomplete中提到的javascript方法仍在執行。 有什么方法可以在Ajax actionlistener方法中進行驗證並避免調用oncomplete方法?

  2. 我們可以在ajax動作偵聽器方法中進行輸入驗證嗎? 這是正確的做法嗎? 我正在經歷一些stackoverflow問題,有些建議我們應該改用jsf自定義驗證器? 定制驗證器是唯一驗證輸入值的唯一方法,它具有一些業務邏輯來驗證輸入值? 我嘗試添加自定義驗證器,當我這樣做時,自定義驗證器將被執行並顯示錯誤。 但是,a:ajax actionlistener沒有得到調用,這是我期望的。 但是,提到的要在render(plateGrid)中渲染的部分未渲染。 但是,即使客戶驗證程序返回驗證錯誤,也會調用oncomplete(renderData)中提到的javascript代碼。 我的JSF代碼帶有自定義驗證器,我的客戶驗證器方法如下:

帶有定制驗證器的JSF代碼

<h:form id="myForm">
    <h:outputText value="Scan barcode:" />
    <h:inputText id="inputBarcode" value="#{myBean.inputBarcode}" immediate="true"> 
        <f:validator validatorId="barcodeValidator" />
        <a:ajax event="change" render="plateGrid" listener="#{myBean.addBarcodedItemToCheckout}" oncomplete="renderPlate()"/>
    </h:inputText>
</h:form>

<a:outputPanel id="plateGrid">
    <h:messages id="messages" style="color:red;margin:8px;" autoUpdate="true"/>
    ....
</a:outputPanel>

我的自定義驗證器:

@Name("barcodeValidator")
@Scope(ScopeType.CONVERSATION)
@org.jboss.seam.annotations.faces.Validator
@BypassInterceptors
public class BarcodeValidator implements Validator, Serializable {

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        if(value != null) {
        String barcode = value.toString();

        if(barcode.equals("M0"))
            throw new ValidatorException(new FacesMessage("Invalid Barcode"));

        }
    }
}

任何幫助和建議,將不勝感激。

可以在完成之前檢查驗證。創建一個不顯示任何內容的ajax4jsf命令按鈕可能是一個好主意,如下所示:

<div style="display:none">
<a:commandButton data="#{albumManager.validationSuccess}"
                                value="#{messages['album.store']}"
                                actionListener="#{albumManager.addAlbum(album)}"
                                id="storebutton"
                                reRender="treePanel, mainArea, menu" 
oncomplete = "if(data)$('albumModalPanel').component.hide()" styleClass="album"/>
</div>

讀取條形碼后,創建一個js函數並使用jQuery單擊該按鈕

<h:inputText id="inputBarcode" value="#{myBean.inputBarcode}" immediate="true" onchange="onChangeBarcode();"> 

function onChangeBarcode()
{
  jQuery("storebutton").click();
}

希望對您有所幫助。 有關更多信息,請訪問richfaces +接縫樣本(相冊)

你可以這樣做...

<h:inputText id="inputBarcode" value="#{myBean.inputBarcode}"> 
    <a:ajax event="change" render="plateGrid" 
        listener="#{myBean.addBarcodedItemToCheckout}" 
        oncomplete="if(!#{myBean.invalidBarcode}){renderPlate()}"/>
</h:inputText>

不過,使用JSF驗證程序將是執行此操作的“正確”方法

順便說一句:您的PlateMakerAction組件使用@Stateless和@Scope(ScopeType.CONVERSATION)進行注釋,這是錯誤的,除非您真的希望它成為無狀態的,否則您應該放棄@Stateless注釋,我對此表示懷疑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM