Using jquery ajax to call the jsf managed bean method (AjaxBehaviorEvent listener handler)

I would like to know if there is a way to run the jsf-driven bean method (with an parameter of type AjaxBehaviorEvent: the same one that was launched when using f: ajax) directly using the jQuery request of the ajax server., Ima jsf developper and I did not find an example about using jquery ajax with Java EE as a server structure, all the examples I found were with php..so I want to get a complete example about this. I think another way to solve it might be to make commandLink sent with jquery on the client side and passing parameters through this call, but I prefer the old solution and I want it to work.

Thank you for help!

+6
source share
3 answers

Here you go:

<script type="text/javascript"> doAwesomeness(); </script> 

On your page:

 <a4j:jsFunction name="doAwesomeness" action="#{awesomeBean.awesomeMethod}"/> 

Good luck

+7
source

jQuery ajax request and JSF request ajax uses a different js library, I don’t think it makes sense to try to mix them too much ...

If you want to run the JSF-controlled bean action from jQuery, you'd better use the hidden h:commandButton for this purpose ...

JSF:

 <h:commandButton id="someId" action="#{someBean.someMethod}" style="display:none"> <f:ajax render="someId" execute="someId"/> </h:commandButton> 

if you want to pass some more hidden arguments, you can add more hidden components of the JSF components to the hidden attribute h:commandButton execute , so their corresponding properties will be updated on the server side ...

Js

 $("#someId").click(); 

On the other hand, if you want to use managed bean data in servlets that match your jQuery calls, you can always access this managed JSF data, for example: JSF - get a managed bean by name

+6
source

In the same vein as the islandguy pointer, if you use Primefaces, you use the <p:remoteCommand/> , as shown below:

 <script type="text/javascript"> doAwesomeness(); </script> 

with:

  <p:remoteCommand name="doAwesomeness" actionListener="#{awesomeBean.awesomeMethod}" oncomplete="jsToExecuteOnAjaxSuccess()" /> 

Hope this helps.

+2
source

All Articles