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 请求的请求可能不令人满意,而会话将无法在每个会话中使用多个浏览器窗口。
  2. 如何注册我的updateCategory()操作,EntryRecorder以便在更新 bean 时触发它?
4

2 回答 2

0

对于第 1 点,我将使用 Request,因为不需要使用 View 和 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 事件而不是 blur (不是说 blur 不起作用,但我发现 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 回答