Calling Managed Bean Method From JavaScript

Jason picture Jason · Mar 8, 2012 · Viewed 60.1k times · Source

I have an application with a client-side image map with multiple sections defined. I need to call a method in the Managed Bean from the <area> onclick attribute.

This doesn't work:

<area id="ReviewPerson" shape="rect" coords="3, 21, 164, 37" href="#" 
    onclick="#{personBean.method}" alt="Review Person" id="reviewPersonArea"
    title="Review Person" />

Since my hands are tied on the image map (unfortunately), how can I call a managed bean method from within the <area> tag?

Answer

Matthew Oakley picture Matthew Oakley · Feb 17, 2015

If you use primefaces, you don't need jquery. You use remoteCommand with a name attribute, and call that name from javascript, so:

<area... onclick="somejavascript();"... />

<p:remoteCommand name="myCommand" actionListener="#{personBean.method}" style="display: none;" />

Javascript:

function somejavascript(){
   myCommand();
}