[英]call a method when opening a page xhtml [primefaces]
我需要調用方法而不單擊任何Boutton,但是我沒有意識到。
我以傳統方式調用該方法,方法是創建一個具有稱為
這是按鈕的代碼:
<p:commandButton process="@form" value="ecouter" action="#{alarmeBean.alarme}" />
但是我需要在打開頁面時調用該方法(方法是alarme)?
這是在每個HTTP請求(包括頁面刷新)中在backing bean中調用方法的一種方法:
<f:metadata>
<f:event listener="#{alarmeBean.alarme}" type="preRenderView" />
</f:metadata>
否則,要在僅加載頁面(第一次)后調用方法,您應該通過“后構造”來對其進行注釋,以便在構造的Bean階段中調用該方法(不應使用@RequestScoped
以避免將請求發送至服務器端,這將刷新頁面,然后再次調用該方法,這是不希望的):
@PostConstruct
public void alarme(){
...
}
像Omar所說的那樣使用preRenderView。 我的意思是:
<f:metadata>
<f:event listener="#{alarmeBean.alarme}" type="preRenderView" />
</f:metadata>
為了避免在每次輪詢刷新時調用它,請檢查這是來自輪詢的ajax請求還是完整的Get請求(如下面alarme
方法中所述)。
FacesContext fc = FacesContext.getCurrentInstance();
if (fc.getPartialViewContext().isAjaxRequest()) {
// ignore this request, we are only interested in GET full page requests
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.