JSF2-f:ajax元素的作用域是什么?

JSF2-f:ajax元素的作用域是什么?,ajax,jsf-2,Ajax,Jsf 2,我有以下表格: <h:form> <h:outputText value="Tag:" /> <h:inputText value="#{entryRecorder.tag}"> <f:ajax render="category" /> </h:inputText> <h:outputText value="Category:" /> <h:inputTex

我有以下表格:

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

我试图实现的目标是:当您键入“tag”字段时,
entryRecorder.tag
字段将用键入的内容更新。根据此操作的某些逻辑,bean还更新其
类别
字段。这种变化应反映在表格中

问题:

  • 我应该为入口记录器使用什么范围?对于多个AJAX请求,请求可能不令人满意,而会话将不能在每个会话中使用多个浏览器窗口
  • 如何在
    EntryRecorder
    中注册我的
    updateCategory()
    操作,以便在bean更新时触发它
  • 回答第二点:

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

    第一,有人吗?

    对于第1点,我将使用Request,因为不需要使用View,正如您所指出的,会话是完全不必要的

    对于第2点,由于您正在使用,我建议您充分利用它。以下是我的建议:

    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>
    

    除非您真的希望tagUpdated方法仅在通过视图更新标记时执行,否则我的建议看起来更清晰。您不必处理事件(也不必强制转换),TagUpdate方法可以是私有的,可以隐藏其功能以避免可能的误用。

    我会使用
    ViewScoped
    ,但我非常希望看到是否有人对第1点有更多的意见。如果由于某种原因,这个bean很重,那么请求范围方法似乎不是最优的。“如果这个bean由于某种原因很重,那么请求作用域方法似乎不是最优的。”--当然。如果你有一个很重的bean,你应该考虑将它拆分,甚至将标记类别输入封装到一个自定义组件中,而不是仅仅因为它使你的应用程序运行更平稳而改变bean的反作用域(您是否运行了一些性能测试?)。
    <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>
    
    @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;
        }
    }