Using jquery ajax to call a jsf managed bean method (an AjaxBehaviorEvent listener handler)
Asked Answered
C

3

6

i would like to know if there is a way to fire a jsf managed bean method (with an AjaxBehaviorEvent type parameter: the same triggered when using f:ajax) directly by using a jquery ajax server request.By the way , i m a jsf developper and i didn't find an example about using jquery ajax with Java EE as a server-side framework, all examples i found were with php..so i wish to get a complete example about doing that. i think the other workaround maybe is to make a commandLink being submitted with jquery on the client side and passing parameters through that call but i prefer the former solution and i wish it work.

Thanks very much for help !

Colleen answered 23/1, 2013 at 9:11 Comment(0)
I
7

Here you go:

<script type="text/javascript">

    doAwesomeness();

</script>

In your page:

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

Good luck!

Immerse answered 5/4, 2013 at 22:43 Comment(2)
Thnx awesome stackoverflow member :)Colleen
But this is not pure JSF. The tag comes from RichFaces.Odometer
V
6

jQuery ajax request and JSF ajax request uses different js library's , I don't think there is a point in trying to mix those to too much...

If you whish to fire a JSF managed bean action from jQuery you better use a hidden h:commandButton for that 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 args you can add some more hidden JSF components ids in the hidden h:commandButton execute attribute, that way their corresponding properties will be updated on server side...

js

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

On the other side , if you want to use managed bean data in servlets that corresponds to your jQuery calls you can always access that JSF managed data, like this : JSF - get managed bean by name

Visitant answered 23/1, 2013 at 9:40 Comment(5)
Thank you for your answer but i still think that even jQuery ajax and JSF ajax use different js library's a request can be made from one to the other becose i guess that both of them are actually dealing with HTTP request's so they can communicate..Colleen
well, now i surrender .. i was tying to detect on firebug how a commandLink with an <f:ajax> request URL is formed for i can do the same with jquery-ajax , i found this :Colleen
j_idt7=j_idt7&j_idt7%3Aj_idt11=mary&j_idt7%3Aj_idt13=mary&s_var1=&s_proclength=&s_taskId=&javax.faces.ViewState=-1257726806850207849%3A3895641171480667070&javax.faces.source=j_idt7%3Aj_idt50&javax.faces.partial.event=click&javax.faces.partial.execute=j_idt7%3Aj_idt50&javax.faces.partial.render=j_idt7%3Acontent&taskname=marytask&javax.faces.behavior.event=click&javax.faces.partial.ajax=trueColleen
: There re alot of jsf specific parameters sent, it s really hard to personalise this without to know all the theory behind it..Colleen
I don't think you should go there... anyway you should not use that hidden button too much... and when you do , its better than all the js code...Visitant
A
3

In the same vein as pointed out by islandguy, if you use Primefaces, you woud use the <p:remoteCommand/> command, as follows :

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

with :

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

Hope this helps..

Airscrew answered 19/4, 2014 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.