使用 Primefaces JavaScript 在服务器上的 bean 上调用 JSF 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4797505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:09:12  来源:igfitidea点击:

Using Primefaces JavaScript to call a JSF method on a bean on the server

javascriptajaxjsfprimefaces

提问by Mark Stang

In the Primefaces User Guide it shows examples of how to make AJAX calls to the server

在 Primefaces 用户指南中,它显示了如何对服务器进行 AJAX 调用的示例

PrimeFaces.ajax.AjaxRequest('/myapp/createUser.jsf',
{
    formId: 'userForm',
    oncomplete: function(xhr, status) {alert('Done');}
});

What I can't figure out is how to call a particular method. My goal is to invalidate the session from the client using JavaScript.

我想不通的是如何调用特定的方法。我的目标是使用 JavaScript 使来自客户端的会话无效。

回答by Danubian Sailor

RemoteCommandis a nice way to achieve that because it provides you a JavaScript function that does the stuff (calling backing bean, refreshing, submitting a form etc., everything that command link can do).

RemoteCommand是实现这一目标的好方法,因为它为您提供了一个 JavaScript 函数来执行这些操作(调用支持 bean、刷新、提交表单等,命令链接可以执行的所有操作)。

From PrimeFaces 3.4 documentation:

来自PrimeFaces 3.4 文档

<p:remoteCommand name="increment" actionListener="#{counter.increment}"
out="count" />

<script type="text/javascript">
function customFunction() {
    //your custom code
    increment(); //makes a remote call
}
</script>

回答by wrschneider

What I've typically done is put a hidden p:commandLink on the page, then have Javascript call the click() event on it.

我通常所做的是在页面上放置一个隐藏的 p:commandLink,然后让 Javascript 在其上调用 click() 事件。

<p:commandLink id="hiddenLink" 
   actionListener="#{bean.methodToInvoke}" style="display:none"/>

Then

然后

$('#hiddenLink').click();

回答by BalusC

Do it in the @PostConstructmethod of the request scoped bean which is associated with the requsted JSF page by EL like #{bean}.

@PostConstruct在请求范围 bean的方法中执行此操作,该bean 与 EL 请求的 JSF 页面相关联,例如#{bean}

@ManagedBean
@RequestScoped
public class Bean {

    @PostConstruct
    public void init() {
        // Here.
    }

}


Unrelated to the question, I only wonder why you would ever do it that way? JSF/PrimeFaces offers much nicer ways using <f:ajax>and <p:ajax>and consorts.

与问题无关,我只是想知道你为什么会那样做?JSF/PrimeFaces 提供了更好的使用<f:ajax>and<p:ajax>和 consorts 的方法。

Is it the intent to run this during Window's unloador beforeunloadevents? If so, then I have to warn you that this is not reliable. It's dependent on the browser whether such a request will actually reach the server or not. More than often it won't. Use it for pure statistical or premature cleanup purposes only, not for sensitive business purposes.

是否有意在 Windowunloadbeforeunload事件期间运行它?如果是这样,那么我必须警告您,这是不可靠的。这种请求是否会真正到达服务器取决于浏览器。更多时候它不会。仅将其用于纯粹的统计或过早清理目的,而不是用于敏感的业务目的。