[英]How to update the property member variable of a managed bean?
I have a member variable in a managed bean, and this member variable is tied to the component in XHTML with a getter and setter. 我在托管bean中有一个成员变量,这个成员变量通过getter和setter绑定到XHTML中的组件。 If I set the member variable in a function call, when the getter of this member variable is trigger, this member variable will still hold the old value.
如果我在函数调用中设置成员变量,当此成员变量的getter是触发器时,此成员变量仍将保留旧值。 May I know how could I update this member variable so that the component could get the latest updated value?
我可以知道如何更新此成员变量,以便组件可以获取最新的更新值?
The manage bean: 管理bean:
@ManagedBean(name = "myBean")
@SessionScoped
public class MyBean {
public boolean show = false;
/** getter and setter **/
public void theFunc() {
this.show = true;
}
}
XHTML code 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>
From this sample, the show
variable is showing false even though theFunc()
has already set to true. 从此示例中,即使
theFunc()
已设置为true, show
变量也显示为false。
Update on 06 Oct 2012 I have remove commandButton and replace with commandLink, I think it should be fine in term of usage. 更新于2012年10月6日我删除了commandButton并用commandLink替换,我认为它在使用方面应该没问题。
Change your methods return type and name if you want it to be invoked. 如果要调用它,请更改方法返回类型和名称 。
Change this 改变这个
public void theFunc() {
this.show = true;
}
to this 对此
public String doFunc() {
this.show = true;
return null;
}
otherwise this action can not work. 否则此动作无效。
<h:commandLink action="#{myBean.doFunc}">
then update parent of your panel like this 然后像这样更新面板的父级
<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>
NOTE: 注意:
use 采用
rendered="#{myBean.show}"
渲染= “#{myBean.show}”
instead of 代替
rendered="#{myBean.show == true}"
rendered =“#{myBean.show == true}”
Try this way: 试试这种方式:
<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>
There are 2 things to note: 有两点需要注意:
<h:panelGroup id="Panel_1">
inside another <h:panelGroup>
or something eles which is always rendered. <h:panelGroup id="Panel_1">
包装在另一个<h:panelGroup>
或其他始终呈现的文件中。 Otherwise, if the show
variable is initially false
, the ajax update may not work since JSF renderer cannot find the the component with id="Panel_1"
when you use <f:ajax render="Panel_1" />
. show
变量最初为false
,则ajax更新可能不起作用,因为当您使用<f:ajax render="Panel_1" />
时,JSF渲染器无法找到id="Panel_1"
的组件。 rendered="#{myBean.show}"
is good enough :P. rendered="#{myBean.show}"
足够好了:P。 Since show
is a boolean variable, you don't need rendered="#{myBean.show == true}"
. show
是一个布尔变量,因此您不需要rendered="#{myBean.show == true}"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.