如何使用< f:ajax>当值< h:inputText>设置为0时,在托管Bean中设置更新的值.改变了

如何使用< f:ajax>当值< h:inputText>设置为0时,在托管Bean中设置更新的值.改变了

问题描述:

我有一个<h:inputText>的JSF页面.我想在更改值时设置绑定到<h:inputText>的值.

I have a JSF page with <h:inputText>. I want to set the value bound to the <h:inputText> when the value is changed.

Bean:

@ManagedBean
@SessionScope
public class MyBean {

    private String in;
    //getter and setter

}

查看:

<h:inputText value="#{myBean.in}" />

如何为此使用<f:ajax>?

只需将<f:ajax>标记嵌套在<h:inputText>标记内.

Just nest the <f:ajax> tag within the <h:inputText> tag.

<h:inputText value="#{myBean.in}">
    <f:ajax />
</h:inputText>

它将在HTML DOM change事件发生时(即,在编辑字段然后将其模糊时)提交值.

It'll submit the value when the HTML DOM change event has occurred (i.e. when the field was edited and then blurred).

event属性已经默认为valueChange,因此将其省略.它的execute属性已经默认为@this,因此将其省略.如果您想完全更新其他组件,请设置render属性.例如

The event attribute already defaults to valueChange, so it's omitted. Its execute attribute already defaults to @this, so it's omitted. In case you'd like to update other component on complete, set render attribute. E.g.

<h:inputText value="#{myBean.in}">
    <f:ajax render="msg" />
</h:inputText>
<h:message id="msg" />

如果要在成功设置侦听器后调用它,请设置listener属性:

If you want to invoke a listener when it has been successfully set, set the listener attribute:

<h:inputText value="#{myBean.in}">
    <f:ajax listener="#{myBean.changeIn}" />
</h:inputText>

public void changeIn() {
    System.out.println("in has been changed to " + in);
}

另请参见: