有条件渲染的输入组件不更新值 [英] conditionally rendered input component does not update value

查看:24
本文介绍了有条件渲染的输入组件不更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 jsf 2 和 Primefaces 3.4

Using jsf 2 and Primefaces 3.4

我知道有很多类似的问题,但没有一个能解决这个问题.

I am aware that there are many similar questions but none of them work on this one.

当panelGridinner"使用固定值true"呈现时(<h:panelGrid id="inner" render="true">)=> 单击命令按钮后,输入组件正确更新#{tableMatchesBean.mergedAuthorAjax} 的值,

When the panelGrid "inner" is rendered with a fixed value of "true" (<h:panelGrid id="inner" rendered="true">) => after the commandbutton is clicked the input component updates correctly the value of #{tableMatchesBean.mergedAuthorAjax},

但是当这个面板的渲染是条件时:<h:panelGrid render="#{spellCheckBean.renderInputMerge}">=> 那么输入组件静默(没有任何迹象表明 tableMatchesBean.mergedAuthorAjax 已被调用).

but when the rendering of this panel is conditional: <h:panelGrid rendered="#{spellCheckBean.renderInputMerge}"> => then the input component is silent (no trace that tableMatchesBean.mergedAuthorAjax has been invoked).

注意:我使用嵌套的 panelGrids 将渲染条件放在需要条件渲染的组件(输出和输入组件)的父元素中.任何帮助表示赞赏.

Note: I used the nested panelGrids to put the render condition in a parent element of the component that need to be conditionally rendered (the output and input components). Any help appreciated.

[评论后] xhtml 页面的作用:
- 当用户单击 selectOne 按钮中的合并"选项时,ajax 更新会将renderInputMerge"切换为 true,从而显示外部"组件.它工作正常.在这个组件中,有一个输入框,显示为OK.
- 在用户单击 commandLink 按钮后,我需要检索此输入字段的值.如上所述,我对此有疑问.

What the xhtml page does:
- when the user clicks on the "merge" option in the selectOnebutton, an ajax update toggles the "renderInputMerge" to true which causes the "outer" component to be displayed. It works fine. In this component, there is an input field, which is displayed OK.
- after the user clicks on the commandLink button, I need to retrieve the value of this input field. As explained above, I have a problem with that.

xhtml:

<h:body style ="width:100%">


    <h:form id = "currMatch">

        <h:panelGrid>

            <p:selectOneButton id ="selection" value="#{spellCheckBean.optionChosen}">
                <f:selectItem itemLabel="keep both" itemValue="1" />
                <f:selectItem itemLabel="delete both" itemValue="2" />
                <f:selectItem itemLabel="merge" itemValue="3" />
                <p:ajax update="outer" />
            </p:selectOneButton>

        </h:panelGrid>

         <h:panelGrid id="outer" >
            <h:panelGrid id="inner" rendered="#{spellCheckBean.renderInputMerge}">
            <!-- **the line above does not update**, this one does    <h:panelGrid rendered="true">-->

                <h:outputText value="our suggestion:  "  style ="margin-left: auto; margin-right: auto"/> 
                     <h:inputText id ="testinput" value="#{tableMatchesBean.mergedAuthorAjax}">
                     </h:inputText>
                </h:panelGrid>    
        </h:panelGrid>

        <p:commandButton action="#{tableMatchesBean.next_()}" 
                         title="Add"
                         value="next"
                         update ="currMatch"
                         >

        </p:commandButton>




    </h:form>
</h:body>

推荐答案

您的 rendered 条件显然取决于请求范围的变量,例如请求范围托管 bean 的属性或请求参数.表单提交帐户作为完全独立的全新请求,其中所有请求范围的托管 bean 都是新重新创建的,所有属性都设置为默认值.如果在收集提交的值(表单提交的应用请求值阶段)期间 rendered 属性评估为 false,那么它们将不会被收集.

Your rendered condition apparently depends on a request scoped variabe, e.g. a property of a request scoped managed bean or a request parameter. A form submit accounts as a completely independent and brand new request whrein all request scoped managed beans are newly recreated, with all properties set to default. If the rendered attribute evaluates to false during collecting the submitted values (the apply request values phase of the form submit), then they simply won't be collected.

基本上有两种方法可以解决这个问题:

There are basically 2 ways to solve this:

  1. 确保 rendered 属性背后的条件在请求作用域 bean 的(后)构造函数中正确预初始化,以便它在处理请求期间返回完全相同的值在向最终用户显示表单的请求期间提交表单.

  1. Make sure that the condition behind the rendered attribute is properly preinitialized in the (post)constructor of the request scoped bean so that it returns exactly the same value during the request of processing the form submit as it was during the request of displaying the form to the enduser.

将 bean 放入视图范围.这样,只要您通过 ajax 或通过在 action 方法中返回 voidnull 与同一视图交互,bean 实例就会存在.

Put the bean in the view scope instead. This way the bean instance will live as long as you're interacting with the same view by ajax or by returning void or null in action method.

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆