JSF 2.0中的AJAX onSubmit验证

时间:2021-09-26 20:00:51

I've started learning JSF2.0, and have come across a problem. Any advice on how to proceed would be welcome.

我已经开始学习JSF2.0,并遇到了一个问题。任何有关如何进行的建议都将受到欢迎。

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!

任何建议都会非常欢迎!谢谢!

1 个解决方案

#1


7  

You're not utilizing JSF builtin validation facilities. Make use of it.

您没有使用JSF内置验证工具。利用它。

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 (i.e. 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. The validator attribute can alternatively also point to a fullworthy class which implements javax.faces.validator.Validator. The <f:ajax> is been added to make it an ajax submit.

当验证失败时(即抛出ValidatorException),消息将显示在与输入组件关联的 中,并且不会调用action方法。或许,validator属性也可以指向一个实现javax.faces.validator.Validator的值得满足的类。添加了 以使其成为ajax提交。

See also:

Wherever you've learnt JSF, make sure that you've also read the chapters about conversion and validation. Don't think too much the PHP/ASP/JSP/jQuery way. JSF is a full fledged component based MVC framework.

无论您何时学习JSF,请确保您还阅读了有关转换和验证的章节。不要过多考虑PHP / ASP / JSP / jQuery的方式。 JSF是一个基于MVC框架的完整组件。

#1


7  

You're not utilizing JSF builtin validation facilities. Make use of it.

您没有使用JSF内置验证工具。利用它。

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 (i.e. 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. The validator attribute can alternatively also point to a fullworthy class which implements javax.faces.validator.Validator. The <f:ajax> is been added to make it an ajax submit.

当验证失败时(即抛出ValidatorException),消息将显示在与输入组件关联的 中,并且不会调用action方法。或许,validator属性也可以指向一个实现javax.faces.validator.Validator的值得满足的类。添加了 以使其成为ajax提交。

See also:

Wherever you've learnt JSF, make sure that you've also read the chapters about conversion and validation. Don't think too much the PHP/ASP/JSP/jQuery way. JSF is a full fledged component based MVC framework.

无论您何时学习JSF,请确保您还阅读了有关转换和验证的章节。不要过多考虑PHP / ASP / JSP / jQuery的方式。 JSF是一个基于MVC框架的完整组件。