简体   繁体   English

JSF2-f:ajax元素的范围是什么?

[英]JSF2 - what scope for f:ajax elements?

I have this form: 我有这个表格:

<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>

What I'm trying to achieve: When you type in the "tag" field, the entryRecorder.tag field is updated with what was typed. 我要实现的目标:当您在“标签”字段中键入内容时, entryRecorder.tag字段将使用键入的内容进行更新。 By some logic upon this action the bean also updates its category field. 通过一些逻辑上的动作,bean也会更新其category字段。 This change should be reflected in the form. 此更改应反映在表格中。

Questions: 问题:

  1. What scope shall I use for EntryRecorder ? 我应该为EntryRecorder使用什么范围? Request may not be satisfactory for multiple AJAX requests, while session will not work with multiple browser windows per one session. 对于多个AJAX请求,该请求可能无法令人满意,而对于每个会话,会话将无法与多个浏览器窗口一起使用。
  2. How can I register my updateCategory() action in EntryRecorder so that it is triggered when the bean is updated? 如何在EntryRecorder注册我的updateCategory()操作,以便在更新Bean时触发它?

Answering point 2: 回答点2:

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

Bean: 豆角,扁豆:

@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));
    }
}

Number 1, anybody? 第一,有人吗?

To point 1, I'll use Request since there is no need to use View and Session is, as you well pointed, completely unnecessary. 关于第1点,我将使用Request,因为您无需指出,就不需要使用View和Session。

For point 2, since you are using <f:ajax/> I suggest making full use of it. 对于第2点,由于您正在使用<f:ajax />,因此建议您充分利用它。 Here is my proposal: 这是我的建议:

xhtml: 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>

Note the use of valueChange event instead of blur (not that blur doesn't work but I find valueChange more 'proper' for a value holder component). 请注意使用valueChange事件而不是模糊(不是模糊不起作用,但我发现valueChange对于值持有者组件而言更“合适”)。

bean: 豆角,扁豆:

@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;
    }
}

Unless you really want the tagUpdated method executed only when tag is updated through the view, my proposal looks more clear. 除非您真的希望仅在通过视图更新标记时才执行tagUpdated方法,否则我的建议看起来更加清晰。 You don't have to deal with the events (nor casting) and the tagUpdated method can be private hiding it's functionality from possible misuses. 您不必处理事件(也无需强制转换),并且tagUpdated方法可以是私有的,以防止其功能被滥用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM