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

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

问题描述

使用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.

当panelGrid"inner"呈现为固定值为"true" (<h:panelGrid id="inner" rendered="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 rendered="#{spellCheckBean.renderInputMerge}"> =>,则输入组件是 silent (无提示)(没有跟踪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页面的作用:
-当用户单击selectOnebutton中的合并"选项时,Ajax更新会将"renderInputMerge"切换为true,这将导致显示外部"组件.它工作正常.在此组件中,有一个输入字段,显示为确定".
-用户单击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的(post)构造函数中正确地预先初始化,以便在处理表单提交的请求期间它返回的值完全相同.在向终端用户显示表单的请求期间.

  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或通过在操作方法中返回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天全站免登陆