Vijaykumar Ponnusamy
Vijaykumar Ponnusamy

Reputation: 225

Calling managed bean methods in JavaScript in with normal HTML button

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?

Upvotes: 3

Views: 4518

Answers (1)

skuntsel
skuntsel

Reputation: 11742

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.

Upvotes: 7

Related Questions