1

Bean にユーザーの情報があり、このユーザーを更新したいと考えています。しかし、私の問題は、inputtextの値が変更されたときに、検証を行いたいということです。新しい値が間違っている場合は、古い値をリセットしたいと思います。

誰でも私を助けてください

4

1 に答える 1

2

「InputText」タグに ValueChangeListener プロパティが必要です。リスナーとして宣言されたメソッドには、古い値を含む ValueChangeEvent オブジェクトがあります。次のようなことができます。

public void myValChanged(ValueChangeEvent event) {
 try {
   validate(event.getNewValue());
   myValue = event.getNewValue();
 } catch (Exception ex) {
    /*
    Listeners are called before update model values in the request lifecycle so any changes you make in that phase are overwritten by the actual values in the page.
    By changing the event's phase to UPDATE_MODEL_VALUES or INVOKE_APPLICATION your changes will overwrite those currently set in the page, which is what you need.
     */
            myValue = event.getOldValue();
    if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }       
 }

}

PhaseId 操作の考え方は、ValueChangeListener が変数セット " myValue = event.getOldValue();"をオーバーライドできないようにすることです。

于 2009-05-07T10:48:44.700 に答える