[英]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.