使用普通的HTML按钮在JavaScript中调用托管bean方法 [英] Calling managed bean methods in JavaScript in with normal HTML button
问题描述
我需要在单击常规HTML按钮的同时使用JavaScript调用托管bean方法.如果我使用JSF2.x和Primefaces,是否可以这样做?
I need to call managed bean methods in JavaScript while clicking the normal HTML button. Is it possible to do that, provided that I use JSF2.x and Primefaces?
推荐答案
是的,有可能. Primefaces通过其组件 <p:remoteCommand>
提供了一个有用的钩子.它基本上为您提供了可以与您的bean通信的javascript函数.
Yes, it is possible. Primefaces provides for a useful hook to do that with its component <p:remoteCommand>
. It basically offers you with a javascript function that will be able to communicate with your bean.
基本用法示例:
视图:
<p:remoteCommand name="remote" actionListener="#{bean.listener}" update="text"/>
<h:outputText id="text" value="#{bean.text}/>
<div onclick="remote()">...<div>
豆:
private String text = "Starting text";//getter + setter
public void listener(ActionEvent event) {
text = "Text was changed via remote command";
}
在上面的示例中,每当单击<div>
时,都会执行远程命令.当然,可以通过单击按钮来调用相同的功能.
In the above example the remote command is executed whenever your <div>
is clicked. Of course, the same function could be called by clicking on your button.
此外,请查看 Primefaces文档.
这篇关于使用普通的HTML按钮在JavaScript中调用托管bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!