使用jsf 2和Primefaces 3.4
我知道有很多类似的问题,但没有一个在这个问题上有效.
当panelGrid“inner”呈现固定值“true”时(< h:panelGrid id =“inner”rendered =“true”>)
=>单击命令按钮后,输入组件正确更新#{tableMatchesBean.mergedAuthorAjax}的值,
但是当这个面板的渲染是有条件的时:< h:panelGrid rendered =“#{spellCheckBean.renderInputMerge}”>
=>然后输入组件是静默的(没有跟踪tableMatchesBean.mergedAuthorAjax已被调用).
注意:我使用嵌套的panelGrids将渲染条件放在需要有条件渲染的组件的父元素(输出和输入组件)中.任何帮助赞赏.
[评论后编辑:] xhtml页面的作用:
– 当用户单击selectOnebutton中的“merge”选项时,ajax更新会将“renderInputMerge”切换为true,从而导致显示“外部”组件.它工作正常.在此组件中,有一个输入字段,显示OK.
– 在用户单击commandLink按钮后,我需要检索此输入字段的值.如上所述,我有一个问题.
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>
最佳答案 您的渲染条件显然取决于请求范围变量,例如请求范围的托管bean或请求参数的属性.表单提交帐户作为完全独立且全新的请求,所有请求范围内的托管bean都是新重新创建的,所有属性都设置为默认值.如果在收集提交的值(表单提交的应用请求值阶段)期间,呈现的属性的计算结果为false,则不会收集它们.
基本上有两种方法可以解决这个问题:
>确保在请求范围bean的(post)构造函数中正确初始化了渲染属性背后的条件,以便在处理表单提交请求期间返回完全相同的值,就像在显示表单请求期间一样到最终用户.
>将bean放在视图范围内.这样,只要您通过ajax与同一视图交互或在action方法中返回void或null,bean实例就会存在.
也可以看看:
> commandButton/commandLink/ajax action/listener method not invoked or input value not updated(第5点适用于您).
> How to choose the right bean scope?