我正在学习JSF + Seam + AJAX4JSF并构建一个应用程序。我的应用程序有一个名为条形码的输入字段,用户可以扫描/输入。一旦输入条形码,我就使用ajax4jsf调用ajax来调用侦听器方法。侦听器方法应该验证输入的条形码。它根据数据库验证条形码,如果它无效,它将返回一个FacesMessage。如果条形码是有效的,那么它用条形码更新数据库并返回到JSF。应该调用ajax4jsf中提到的呈现块来呈现页面,并执行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>My Seam Bean:
@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.");
}
}}
我所面对的问题是:
带有自定义验证器的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"));
}
}
}任何帮助和建议都将不胜感激。
发布于 2017-09-10 06:01:56
可以在oncomplete之前检查验证,最好创建一个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 + seam示例(相册)
发布于 2017-09-15 14:38:57
你能做到的..。
<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),这是错误的,您应该去掉@无状态注释,除非您真的希望它成为无状态的,我对此表示怀疑。
https://stackoverflow.com/questions/46065415
复制相似问题