问题
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?
回答1:
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.
Basic usage example:
The view:
<p:remoteCommand name="remote" actionListener="#{bean.listener}" update="text"/>
<h:outputText id="text" value="#{bean.text}/>
<div onclick="remote()">...<div>
The bean:
private String text = "Starting text";//getter + setter
public void listener(ActionEvent event) {
text = "Text was changed via remote command";
}
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.
Also, it would be wise to check out the Primefaces documentation.
来源:https://stackoverflow.com/questions/15152314/calling-managed-bean-methods-in-javascript-in-with-normal-html-button