AJAX更新的最佳方法是什么

AJAX更新的最佳方法是什么

本文介绍了验证在JSF 2.0 AJAX更新的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的团队正在编写的第一个JSF 2.0应用程序,因为使用条纹很多年了,我有最好的方法有些问题,使用F:AJAX标签和验证输入

Our team is writing its first JSF 2.0 application since using Stripes for many years, and I have some questions on the best way to use the f:ajax tag and validate the input.

有一个很大的问题,我见过的回答有多个输入表单,然后提交按钮),但我们想维持后,立即改变更新后的各个输入字段,并保存到数据库(没有提交按钮。我们曾在条纹采用Prototype的Ajax.Request的这个工作的罚款,但它是一个额外的步骤,我想,以避免可能的话。

A lot of questions I've seen answered have a form with multiple inputs and then a submit button), but we would like to maintain individual input fields updated immediately upon change and persisted to the database (with no submit button. We had this working fine in Stripes using Prototype's Ajax.Request, but it was an extra step I'd like to avoid if possible.

从本质上讲,我们有一大堆的投入在网页上直接支持豆类,例如:

Essentially we have a page with a bunch of inputs on it directly backed by beans, for example:

<h:inputText id="name" value="#{personController.name}" >
    <f:ajax listener="#{personController.ajax}" />
</h:inputText>

正如你可能知道,通过监听器被调用时,name的值已经被改变了的豆。这将是方便的,但我有几个问题是:

As you may know, by the time the listener is invoked, the value of name has already been changed on the bean. This would be convenient, but I have a few problems with it:

  • 听者不明明知道其中的bean的值更改
  • 的值已经改变了,我不能上执行任何服务器端验证
  • 在我不知道的名老值为偶数,如果我能进行某种形式的验证就可以了,我不知道该怎么该值设置回

现在它看起来像我们将不得不实行某种形式的JavaScript中间人采取什么性质的改变和新的价值,发送到控制器,并将其执行验证,更新数据库,发回的东西渲染等,但就像我说的,这就是我们用来做条纹,我真的很喜欢用一些更原始。

Right now it's looking like we'll have to implement some kind of javascript middleman to take in what property changed and the new value, send that to the Controller, and have it perform validation, updating the database, sending back something to render, etc. But like I said, this is what we used to do with Stripes and I'd really like to use something more native.

我没看到,如果我们想要某种提交,我们可以使用类似的valueChangeListener属性页面上的按钮,但我也想避免大规模的提交。

I did see that if we wanted some kind of Submit button on the page we could use something like the valueChangeListener attribute, but I'd also like to avoid massive submits.

我包括OpenFaces标签,因为我们已经在使用的数据表,所以如果有一些好东西在里面,我们是开放的使用它。但据我可以告诉他们○:AJAX标签是不是更强大的比JSF的F:。AJAX

I included the OpenFaces tag because we're already using that for datatables, so if there's something nice in there we're open to using it. But as far as I can tell their o:ajax tag isn't that much more powerful than JSF's f:ajax.

谢谢!

推荐答案

您正在找错了方向,实现验证输入字段的具体功能要求。您应该使用一般的JSF验证这一点,并不是它运行在错误的时刻(一些Ajax侦听器方法的 INVOKE_ACTION 阶段,而不是 PROCESS_VALIDATIONS 相),并在那里你不直接有一只手在模型的价值。阿贾克斯监听方法仅仅是用来执行的基础上的电流的模型值(S)。

You're looking in the wrong direction to achieve the concrete functional requirement of validating an input field. You should use a normal JSF validator for this, not some ajax listener method which runs at the wrong moment (the INVOKE_ACTION phase instead of PROCESS_VALIDATIONS phase) and where you don't directly have a hand at the model value. The ajax listener method is merely to be used to execute some business logic based on the current model value(s).

JSF具有要求属性和几个后面几个内建的验证&LT; F:validateXxx&GT; 标签。你甚至可以通过实现创建自定义的验证程序的<$c$c>Validator接口。

JSF has several builtin validators behind the required attribute and several <f:validateXxx> tags. You can even create custom validators by implementing the Validator interface.

例如。检查requireness:

E.g. checking the requireness:

<h:inputText ... required="true">
    <f:ajax />
</h:inputText>

或者如果它匹配使用各种<$ C $的模式检查C>&LT; F:validateXxx&GT; 标记:

<h:inputText ...>
    <f:validateRegex pattern="[a-z]+" />
    <f:ajax />
</h:inputText>

或使用自定义的验证:

Or using a custom validator:

<h:inputText ...>
    <f:validator validatorId="myValidator" />
    <f:ajax />
</h:inputText>

@FacesValidator("myValidator")
public class MyValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) {
        if (value is not valid) {
            throw new ValidatorException(new FacesMessage(...));
        }
    }

}

&LT; F:AJAX&GT; 仅仅是存在于HTML DOM 更改期间提交当前输入字段事件(或点击事件情况下的复选框/单选按钮)。为了通过AJAX提交当前输入字段方法; AJAX收听GT:;你不一定需要一个&LT F。如果要勾上的数值变化时,只需使用 valueChangeListener

The <f:ajax> is merely there to submit the current input field during the HTML DOM change event (or click event in case of checkboxes/radiobuttons). You don't necessarily need a <f:ajax listener> method in order to submit the current input field by ajax. If you want to hook on a value change event, just use valueChangeListener.

<h:inputText ... valueChangeListener="#{bean.valueChanged}">
    <f:ajax />
</h:inputText>

public void valueChanged(ValueChangeEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getValue();
    UIComponent component = event.getComponent();
    // ...
}

请注意,这时候验证已通过特定的组件才会被调用。

Note that this will only be invoked when validation has passed on the particular component.

这篇关于验证在JSF 2.0 AJAX更新的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:29