1

私はこのフォームを持っています:

<h:form>
    <h:outputText value="Tag:" />   
    <h:inputText value="#{entryRecorder.tag}">
        <f:ajax render="category" />
    </h:inputText>
    <h:outputText value="Category:" />
    <h:inputText value="#{entryRecorder.category}" id="category" />
</h:form>

私が達成しようとしていること:「タグ」フィールドに入力すると、entryRecorder.tagフィールドは入力された内容で更新されます。このアクションのロジックによって、Beanはそのcategoryフィールドも更新します。この変更はフォームに反映する必要があります。

質問:

  1. どのスコープを使用しEntryRecorderますか?リクエストは複数のAJAXリクエストに対して満足のいくものではないかもしれませんが、セッションは1つのセッションごとに複数のブラウザウィンドウで機能しません。
  2. Beanが更新されたときにトリガーされるように、updateCategory()アクションを登録するにはどうすればよいですか?EntryRecorder
4

2 に答える 2

0

ポイント1には、Viewを使用する必要がないため、Requestを使用します。Sessionは、ご指摘のとおり、まったく不要です。

ポイント 2 については、<f:ajax/> を使用しているので、それを最大限に活用することをお勧めします。これが私の提案です:

xhtml:

<h:form>
    <h:outputText value="Tag:" />
    <h:inputText value="#{entryRecorder.tag}">
        <f:ajax render="category" event="valueChange"/>
    </h:inputText>
    <h:outputText value="Category:" />
    <h:inputText value="#{entryRecorder.category}" id="category" />
</h:form>

ぼかしの代わりに valueChange イベントを使用していることに注意してください (ぼかしが機能しないわけではありませんが、値ホルダー コンポーネントには valueChange の方が「適切」であることがわかります)。

豆:

@ManagedBean
@RequestScoped
public class EntryRecorder {
    private String tag;
    private String category;

    public String getCategory() {
        return category;
    }

    public String getTag() {
        return tag;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setTag(String tag) {
        this.tag = tag;
        tagUpdated();
    }

    private void tagUpdated() {
        category = tag;
    }
}

ビューを介してタグが更新された場合にのみ tagUpdated メソッドを実行することが本当に必要でない限り、私の提案はより明確に見えます。イベントを処理する (キャストもしない) 必要はありません。また、tagUpdated メソッドを非公開にして、誤用の可能性から機能を隠すことができます。

于 2010-03-31T03:34:16.570 に答える
0

回答ポイント 2:

<h:inputText styleClass="id_tag" value="#{entryRecorder.tag}"
    valueChangeListener="#{entryRecorder.tagUpdated}">
    <f:ajax render="category" event="blur" />
</h:inputText>

豆:

@ManagedBean
@ViewScoped
public class EntryRecorder {
    private String tag;
    private String category;
    @EJB
    private ExpenseService expenseService;

    public void tagUpdated(ValueChangeEvent e) {
        String value = (String) e.getNewValue();
        setCategory(expenseService.getCategory(value));
    }
}

ナンバー1、誰か?

于 2010-03-23T19:59:41.543 に答える