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,
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
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
Related
When I submit a form to the browser,the form is submitted twice.The access log shows there are two requests arise at the same time.Two different threads executing it.
The thing is,the first request contains the attribute values that the user entered while the second request call contains only null values in request object.
Not all the times this issue occur.It happens only sometimes and not reproducible at all.Both in IE8 and IE9,I got these issues.
Do anyone know why does it happen?
Maybe the problem is that you are using submit button and document.forms[0].submit within the javascript onclick event handler .
Thus action is called twice:
First time by
document.forms[0].submit
Second time by
submit button action
Do you use struts1 or struts2? Anyway, they both have a mechanism to avoid repeat submit: token.
For struts1, you need call saveToken() in the first action (the action for form page), and 'html:form' tag of struts will automatically add this token to your page; in your submit action (the action which dealing form), invoke isTokenValid(request, true), and this will validate the token from your page & your session.
For struts2, add the interceptor ref token for your submit action, and add 's:token' tag in your form page.
The above solutions restrict repeat submitting in server side, that the second request will cause a exception and handled as invalid request. But if you wanna restrict this in UI side, you need some javascript, like: when you click the submit button, disable it to avoid repeat submitting.
I have a jsp page which contains a form which calls a servlet, after inserting data into the database by a servlet hot to display successs alert to the user on the page?
Thanks in advance for answering.
Either you call the servlet asynchronously with AJAX and have a success callback function (see jQuery example: http://api.jquery.com/category/ajax/ )
Or you do it the old fashioned way with a synchronous Post which loads a success page, or the same page with for example a query parameter that states that you have succeed. This could then be used to activate a script block in your JSP (or whatever template language is your flavour).
You can use a javascript alert box. See http://www.w3schools.com/js/js_popup.asp
Is there a way to attach a global listener to all AJAX calls in JSF? Maybe through a phase listener or something?
Here is the conundrum... Lets say you're using f:ajax tags and something like apache shiro and you let your session expire. Then you come back and click a button that has an f:ajax attached to it. The server will respond with a 302 redirect to the login page.
The user sees nothing. They can repeatedly click and invoke the ajax call, but to them the app is just "dead."
So, my though is, is there a way to attach a listener to all ajax calls in JSF? If so, what I'd like to do is monitoring the response code. If it's a redirect, use a window.navigate to send them along their way.
I'm always open to hear how others have solved this problem!
Is there a way to attach a global listener to all AJAX calls in JSF? Maybe through a phase listener or something?
Yes, a PhaseListener can do it. A SystemEventListener also. A Filter also.
If you're inside JSF context, then you can check as follows whether the current request is an ajax request or not.
if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
// It's an ajax request.
}
If you're not inside JSF context, e.g. inside a Filter, then you can check as follows whether the current request is a JSF ajax request or not.
if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
// It's a JSF ajax request.
}
Here is the conundrum... Lets say you're using f:ajax tags and something like apache shiro and you let your session expire. Then you come back and click a button that has an f:ajax attached to it. The server will respond with a 302 redirect to the login page.
The user sees nothing. They can repeatedly click and invoke the ajax call, but to them the app is just "dead."
Forcing a redirect on an ajax request requires a special XML response. When you're inside JSF context, then ExternalContext#redirect() already takes this implicitly into account. All you need to do is to write this:
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
If you're not inside JSF context, e.g. inside a Filter, then you'd need to write the whole XML response yourself. E.g.
response.setContentType("text/xml");
response.getWriter()
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", url);
To redirect a jsf ajax request you need xml as follows
<?xml version="1.0" encoding="UTF-8"?>
<partial-response>
<redirect url="XXX">
</redirect>
</partial-response>
Here XXX is url you want redirect to happen.
On ajax call redirect sent is not as above hence no redirect.
To get the desired result have a filter for all jsf request except few
pages(login page) and check session is valid and if it is really jsf
ajax call by checking header "Faces-Request", its value should be
"partial/ajax". If session has expired and is ajax request send above
xml as response.
It should work.
Is it possible to call a spring controller from javascript included in a jsp?
I'm trying to call it like this:
form.action='${pageContext.request.contextPath}/spring/myController';
I can see that the control passes throught the lines, but nothing is happening.
Also I get messages like get or post is not supported.
when I submit the form with a post method I get error message post is not supported.
I use the annotations like this in controller.
#RequestMapping(method = RequestMethod.GET)
How can I handle both get and post in spring controllers?
Your javascript is not actually calling anything. Rather, it is setting the "action" attribute of (I assume) a <form> element in your web page to some URL assembled by the JSP. The "call" to your server will happen later ... when the user clicks some button that causes the form to be submitted.
My title maybe confusing so please read on. I'm using the following technologies if you may. Spring, Hibernate, JSF (RichFaces), MySQL, Internet Explorer.
I have a List of items which is displayed in a RichFaces datatable like so:
item a
item b
item c
item d
item e
On the same page I have the following buttons: search, edit, add, delete and new.
When an user enters a search string, e.g. "item c", and press search button, then it displays a list of matching items, e.g:
item c
When the user presses the new button, the request will be redirected to another page using:
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().redirect(page + ".jsf" );
When the browser back button of IE is been pressed on that page, the page displays "web page expired". What is this and how can I avoid this?
web page expired
You will get this error when you're trying to obtain a non-cached POST request from the browser history. This behaviour is fully expected. To fix this "problem", you need to either turn the cache on or to replace POST by GET.
Enabling the browser cache is actually easy: just remove the Cache-Control: no-cache and related headers from the HTTP response of the POST request in question. The enduser will then only get a warning dialog that the POST data will be resent to the server, which in case of fully non-idempotent requests like placing an order or deleting an item is really not desirable. Replacing POST by GET is then a better solution. Getting searchresults (like as Google does) should really be done by GET.
Replacing POST by GET isn't easy in JSF prior to version 2.0. Best what you can do is to fire a redirect after the POST and pass the data of interest as request parameter which you retain from #{param} as managed property (more recommended) or store the data of interest in session scope (not recommended). A completely different alternative is to replace the JSF <h:form> by a simple HTML <form action="searchresults.jsf"> and do the search job in a #PostConstruct method in the backing bean associated with searchresults.jsf, after the submitted query has been gathered as managed property or from request parameter map.