Vikas V
Vikas V

Reputation: 3186

Calling Bean method from Jquery

Is it possible to call Bean from Jquery? My requirement is like below,

I have a JSF 1.2 based Servlet.

Am invalidating a user's session if he is idle for some time. Am showing Jquery dialog box 1 minute before invalidating the session. A user has 2 options in the dialog box. "Yes I want to continue" will extend the session. "No I want to logout" will logout the user.

When user clicks on "No I want to logout", I want to call bean method where I update the Database & invalidate the session.

Below is the code,

'No, Log out': function(){
 $j.idleTimeout.options.onTimeout.call($j.post('//This is where am stuck',function()

I want to call bean in $j.post so that I can do some clean up activities in my bean.

How this can be done?

Regards,

Upvotes: 1

Views: 5045

Answers (2)

Daniel
Daniel

Reputation: 37051

You can either use just use a hidden(style="display:none") commandButton with an action pointing to a method in your bean , and call a .click() on it from jquery

something like this

<h:commandButton id="myButton" action="#{myBean.myInvalidateMethod}" style="display:none"/>

jquery

&("#myButton").click();//possible myForm prefix appear before the id so use #myForm\\:myButton selector

Or you can call servlet from your jsf page , similar to this answer Calling a Servlet from a JSP page using jQuery Ajax

Upvotes: 1

Asciiom
Asciiom

Reputation: 9975

You can't directly access the methods, you'll have to make your servlet handle your request and call the method for you and return the data in json format for example

Upvotes: 0

Related Questions