简体   繁体   English

打开页面xhtml时调用方法[primefaces]

[英]call a method when opening a page xhtml [primefaces]

I need to call a method without clicking any Boutton but I failed to realized. 我需要调用方法而不单击任何Boutton,但是我没有意识到。

I call the method with the traditional way I create a button that has a method called 我以传统方式调用该方法,方法是创建一个具有称为

this is the code of button: 这是按钮的代码:

 <p:commandButton process="@form" value="ecouter"  action="#{alarmeBean.alarme}" />

but my need is to call the method when opening my page (the methode is alarme )?? 但是我需要在打开页面时调用该方法(方法是alarme)?

Here's a way to call method in the backing bean at every HTTP request (including page refreshing) : 这是在每个HTTP请求(包括页面刷新)中在backing bean中调用方法的一种方法:

<f:metadata>
    <f:event listener="#{alarmeBean.alarme}" type="preRenderView" />
</f:metadata>

Otherwise, to call a method once the page is only loaded (for 1st time), you should annotate it by "post constructed" in order to invoke it in the construction's bean phase (which shouldn't be @RequestScoped to avoid sending requests to the server side, which makes page refreshing and then invokes the method again, which is not desired): 否则,要在加载页面(第一次)后调用方法,您应该通过“后构造”来对其进行注释,以便在构造的Bean阶段中调用该方法(不应使用@RequestScoped以避免将请求发送至服务器端,这将刷新页面,然后再次调用该方法,这是不希望的):

@PostConstruct
public void alarme(){
    ...
}

Use preRenderView like Omar said. 像Omar所说的那样使用preRenderView。 I mean: 我的意思是:

<f:metadata>
    <f:event listener="#{alarmeBean.alarme}" type="preRenderView" />
</f:metadata>

in order to avoid calling it on each poll refresh check if this is an ajax request from poll or it's a complete Get request like below in alarme method. 为了避免在每次轮询刷新时调用它,请检查这是来自轮询的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM