[英]How to update the property member variable of a managed bean?
我在托管bean中有一个成员变量,这个成员变量通过getter和setter绑定到XHTML中的组件。 如果我在函数调用中设置成员变量,当此成员变量的getter是触发器时,此成员变量仍将保留旧值。 我可以知道如何更新此成员变量,以便组件可以获取最新的更新值?
管理bean:
@ManagedBean(name = "myBean")
@SessionScoped
public class MyBean {
public boolean show = false;
/** getter and setter **/
public void theFunc() {
this.show = true;
}
}
XHTML代码
<h:panelGroup id="Panel_1" rendered="#{myBean.show == true}">
...
Some rubbish here
...
</h:panelGroup>
<h:panelGroup id="Panel_2">
<h:commandLink action="#{myBean.doFunc}">
<f:ajax event="action" render="Panel_1"/>
<h:outputText value="XX" />
</h:commandLink>
</h:panelGroup>
从此示例中,即使theFunc()
已设置为true, show
变量也显示为false。
更新于2012年10月6日我删除了commandButton并用commandLink替换,我认为它在使用方面应该没问题。
如果要调用它,请更改方法返回类型和名称 。
改变这个
public void theFunc() {
this.show = true;
}
对此
public String doFunc() {
this.show = true;
return null;
}
否则此动作无效。
<h:commandLink action="#{myBean.doFunc}">
然后像这样更新面板的父级
<h:panelGroup id="parentPanelGroupId">
<h:panelGroup id="Panel_1" rendered="#{myBean.show}">
...
Some rubbish here
...
</h:panelGroup>
</h:panelGroup>
<h:panelGroup id="Panel_2">
<h:commandLink action="#{myBean.doFunc}">
<f:ajax render="parentPanelGroupId"/>
<h:outputText value="SHOW/HIDE PANEL 1" />
</h:commandLink>
</h:panelGroup>
注意:
采用
渲染= “#{myBean.show}”
代替
rendered =“#{myBean.show == true}”
试试这种方式:
<h:panelGroup id="toUpdate">
<h:panelGroup id="Panel_1" rendered="#{myBean.show}">
...
</h:panelGroup>
</h:panelGroup>
<h:commandButton action="#{theBean.doCalculation}">
<f:ajax render="toUpdate" />
</h:commandButton>
有两点需要注意:
<h:panelGroup id="Panel_1">
包装在另一个<h:panelGroup>
或其他始终呈现的文件中。 否则,如果show
变量最初为false
,则ajax更新可能不起作用,因为当您使用<f:ajax render="Panel_1" />
时,JSF渲染器无法找到id="Panel_1"
的组件。 rendered="#{myBean.show}"
足够好了:P。 由于show
是一个布尔变量,因此您不需要rendered="#{myBean.show == true}"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.