首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ajax4jsf actionlistener方法中的jsf输入验证

ajax4jsf actionlistener方法中的jsf输入验证
EN

Stack Overflow用户
提问于 2017-09-06 01:22:37
回答 2查看 239关注 0票数 0

我正在学习JSF + Seam + AJAX4JSF并构建一个应用程序。我的应用程序有一个名为条形码的输入字段,用户可以扫描/输入。一旦输入条形码,我就使用ajax4jsf调用ajax来调用侦听器方法。侦听器方法应该验证输入的条形码。它根据数据库验证条形码,如果它无效,它将返回一个FacesMessage。如果条形码是有效的,那么它用条形码更新数据库并返回到JSF。应该调用ajax4jsf中提到的呈现块来呈现页面,并执行oncomplete中提到的javascript方法。

我的代码如下。

JSF代码:

代码语言:javascript
复制
<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>

My Seam Bean:

代码语言:javascript
复制
@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方法仍然被执行。我们是否可以在actionlistener方法中进行验证,并避免调用oncomplete方法?
  2. 我们可以在ajax动作侦听器方法中进行输入验证吗?这是正确的做法吗?我正在经历一些堆栈溢出问题,有些问题建议我们应该使用jsf自定义验证器来代替?自定义验证器是验证输入值的唯一方法吗?输入值有一些业务逻辑来验证它吗?我尝试添加自定义验证器,当我这样做时,将执行自定义验证器并显示错误。但是,a:ajax actionlistener没有被调用--这正是我所期望的。但是,在render (plateGrid)中提到的呈现部分没有呈现。但是,即使客户验证器返回验证错误,也会调用oncomplete (renderData)中提到的javascript代码。使用自定义验证器的JSF代码,我的客户验证器方法如下:

带有自定义验证器的JSF代码

代码语言:javascript
复制
<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>

我的自定义验证器:

代码语言:javascript
复制
@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"));

        }
    }
}

任何帮助和建议都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2017-09-10 06:01:56

可以在oncomplete之前检查验证,最好创建一个ajax4jsf命令按钮,其中没有显示,如下所示:

代码语言:javascript
复制
<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按钮。

代码语言:javascript
复制
<h:inputText id="inputBarcode" value="#{myBean.inputBarcode}" immediate="true" onchange="onChangeBarcode();"> 

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

希望能帮上忙。欲了解更多信息,请访问richfaces + seam示例(相册)

票数 0
EN

Stack Overflow用户

发布于 2017-09-15 14:38:57

你能做到的..。

代码语言:javascript
复制
<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组件被注释为@无状态以及@Scope(ScopeType.CONVERSATION),这是错误的,您应该去掉@无状态注释,除非您真的希望它成为无状态的,我对此表示怀疑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46065415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档