简体   繁体   English

JSF 2.0中的AJAX onSubmit验证

[英]AJAX onSubmit validation in JSF 2.0

I've started learning JSF2.0, and have come across a problem. 我开始学习JSF2.0,并遇到了一个问题。 Any advice on how to proceed would be welcome. 有关如何进行的任何建议都将受到欢迎。

I have renamed form elements and classes for simplicity sake. 为简单起见,我重命名了表单元素和类。

I have a form, for example: 我有一个表格,例如:

<h:form id="frmSearch">
    <h:inputText id="dataPoint1" value="#{bean.dataPoint1}"/>
    <div id="dataPoint1Error" class="msgError">Value not found in database.</div>

    <h:inputText id="dataPoint2" value="#{bean.dataPoint2}"/>
    <div id="dataPoint2Error" class="msgError">Value not found in database.</div>

    <h:commandButton action="#{bean.validate}" type="submit" value="Search"/>
</h:form>

The CSS class "msgError" keeps the element hidden by default. CSS类“msgError”默认隐藏元素。

I would like to basically have a method in the "bean" class that validates the input by checking against the database, then if the value isn't found, unhide the error message, or if it is found, then execute another method which performs the actual functionality. 我想在“bean”类中基本上有一个方法,通过检查数据库来验证输入,然后如果找不到该值,取消隐藏错误消息,或者如果找到它,则执行另一个执行的方法实际的功能。

In my head, it would work sort of like this in the Java (forgive any syntax errors, just typing as I think): 在我看来,它在Java中的工作方式有点类似(原谅任何语法错误,只需按照我的想法输入):

@ManagedBean
public class Bean {
    private String dataPoint1 = "";
    private String dataPoint2 = "";

    public boolean validate() {
        if(dao.fieldExists(this.dataPoint1) && dao.fieldExists(this.dataPoint2)) { //check the database
            performFunctionality();
            return true;
        }
        else {
            return false; //and show error div on screen
        }
    }        

    public void performFunctionality() {
        //do whatever
    }

    //getters and setters
}

Any advice would be very welcome! 任何建议都会非常欢迎! Thanks! 谢谢!

You're not utilizing JSF builtin validation facilities. 您没有使用JSF内置验证工具。 Make use of it. 利用它。

Here's how it can look like: 这是它的样子:

<h:form id="frmSearch">
    <h:inputText id="dataPoint1" value="#{bean.dataPoint1}" validator="#{bean.validateDataPoint}" />
    <h:message for="dataPoint1" />

    <h:inputText id="dataPoint2" value="#{bean.dataPoint2}" validator="#{bean.validateDataPoint}" />
    <h:message for="dataPoint2" />

    <h:commandButton action="#{bean.performFunctionality}" value="Search">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

with

public void validateDataPoint(FacesContext context, UIComponent component, Object convertedValue) {
    if (!dao.fieldExists((String) convertedValue)) {
        throw new ValidatorException(new FacesMessage("Value not found in database."));
    }
}        

That performFunctionality() must be executed by the command button's action method. 必须通过命令按钮的action方法执行performFunctionality()

When validation fails (ie ValidatorException is been thrown), then the message will be displayed in the <h:message> associated with the input component and the action method won't be invoked. 当验证失败时(即抛出ValidatorException ),消息将显示在与输入组件关联的<h:message>中,并且不会调用action方法。 The validator attribute can alternatively also point to a fullworthy class which implements javax.faces.validator.Validator . 或许, validator属性也可以指向一个实现javax.faces.validator.Validator的值得满足的类。 The <f:ajax> is been added to make it an ajax submit. 添加了<f:ajax>以使其成为ajax提交。

See also: 也可以看看:

Wherever you've learnt JSF, make sure that you've also read the chapters about conversion and validation. 无论您何时学习JSF,请确保您还阅读了有关转换和验证的章节。 Don't think too much the PHP/ASP/JSP/jQuery way. 不要过多考虑PHP / ASP / JSP / jQuery的方式。 JSF is a full fledged component based MVC framework. JSF是一个基于MVC框架的完整组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM