Meta refresh on session.maxInactiveInterval and AJAX don't get along - ajax

I have JSF web page using PrimeFaces 5.1 and I want to redirect redirect to some xhtml page once session is expired.
I have set session timeout to 1 minute (for testing it will be set to 30 after that) in web.xml
<session-config>
<session-timeout>1</session-timeout>
</session-config>
And i have added
<meta http-equiv="refresh" content="#{session.maxInactiveInterval};url=#{request.contextPath}/season_expired.xhtml"/>
to <h:head> of the default template which is used on every page of the application.
Everything works fine for the most of the time (when I move between pages etc..) except when I'm using using some form where every refresh is made by AJAX. Then i get redirected after one minute.
Is there a way to "refresh" session with AJAX?

You can use JS setTimeout() for that which you can clear out with clearTimeout() when an ajax request is made. You can use jsf.ajax.addOnEvent() to register a global ajax event listener function.
<script>
var redirectToExpired = function() { window.location='#{request.contextPath}/expired.xhtml' };
var expireTimeMillis = 5000;
window.redirectOnExpire = setTimeout(redirectToExpired, expireTimeMillis);
jsf.ajax.addOnEvent(function(data) {
if (data.status == 'begin') {
clearTimeout(window.redirectOnExpire);
window.redirectOnExpire = setTimeout(redirectToExpired, expireTimeMillis);
}
});
</script>
Like as with the meta refresh header, this is a bit naive as it doesn't work nicely when you have the same webapp open in multiple browser tabs. In a "forgotten" tab, the redirect will still happen in the background while you're active on the same webapp in another tab.
Better is to just not do that all, but rely on ViewExpiredException during a real user action. You can find in javax.faces.application.ViewExpiredException: View could not be restored how to register an error page for that and also how the meta refresh header could be used there.
See also:
Execute JavaScript after every JSF ajax postback

Related

Reload page not working after validation using JS

So I have a form that has Laravel validation on it. Page reload is working well if there're no problems with the form such as required fields, but if validation fails, the reload page is not working. I am forcing the page to reload if the back button is clicked. Here's the code have.
<script type="text/javascript">
$(window).load(function() {
$('form').get(0).reset();
});
</script>
All fields should be blank after successful submission.
What should be the other things to do to achieve it in laravel blade file?
Have you tried location.reload(); ?
Thanks for the help.
I have done it this way:
<script>
if (!!window.performance && window.performance.navigation.type === 2) {
//navigation type 2 is accessed using back button while,
//0 is accessed using the link or redirected after validation and 1 is refresh page
window.location.reload();
}
</script>

JQuery Mobile events does not fire after ajax call

I'm JQM newbe and I'm using it for our ASP.NET MVC 4 project. Here's a simplified version of code that I'm using:
Controller:
public ActionView ShowModel()
{
return View("ShowModel", new Model());
}
View (ShowModel.cshtml Javascript):
<script>
$('#page').bind('pageinit', function() {
$('#spanSubmit').click(function () { $('form').submit(); });
});
<script>
<form action="#Url.Action("ShowModel")">
<span id="spanSubmit">Submit</span>
</form>
After AJAX query I can't fire form submitting again also no other callbacks that were attached in pageinit callback are working.
Everything starts working when I'm adding data-ajax="false" to form tag. But I really want it to work with AJAX, so can anyone explain me what should I do.
I know that my question was asked several times but I still can't make this page work. Thanks in advance.
I needed to look more into documentation and HTML code after AJAX call. After AJAX completed JQM creates an additional page with same id. So calling $('#page').bind fails because there are two pages now with page id.

AngularJS and AJAX injection - Manually start the App?

Currently my angular app is dynamically loaded into the current webpage. This means that as well as all scripts (angular.min.js / controllers etc) and is loaded with the Wicket AJAX request and injected in the current webpage.
The scripts are included in the head, the div injected in some form in the body.
At this point Angular should detect the div and start up the app, but nothing happens. when i try to use console.log(angular) i get angular just like with an normal app. When i try to load the same webpage (without the AJAX injection) the app starts up fine.
How can i manually start AngularJS, or notify to start?
http://docs.angularjs.org/guide/bootstrap
Manual Initialization
If you need to have more control over the initialization process, you can use a manual bootstrapping method instead. Examples of when
you'd need to do this include using script loaders or the need to
perform an operation before Angular compiles a page.
<!doctype html>
<html xmlns:ng="http://angularjs.org">
<body>
Hello {{'World'}}!
<script src="http://code.angularjs.org/angular.js"></script>
<script>
angular.element(document).ready(function() {
angular.bootstrap(document);
});
</script>
</body>
</html>
In short: Remove ngApp directive from your html and manually bootstrap
Developer guide has the most you need, I suggest everyone to read it.

How to scrape ajax generated content from JSF-Site?

I am currently playing around with different scraping techniques and found out, that it can get pretty complicated quickly when a lot of javascript is involved.
I had some success with HTMLUnit which seems to interpret javascript rather well, but I am looking for a more lightweight solution.
So the problem I am facing now is: I want to retrieve the results of a specific page, which is generated by an ajax call by a click on a certain button.
The call itself is rather simple, just a HTTP Post to a certain URL with a few parameters submitted in the post body. The problem I have now is that the server complains when I submit the HTTP Post to the ajax function without really opening the containing site.
What I basically do for testing is:
curl -v -d "AJAXREQUEST=..." https://myhost/ajaxurl
An what I get is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Ajax-Response" content="true" />
<meta name="Ajax-Expired" content="View state could't be restored - reload page ?" />
</head>
</html>
The server is running JSF 1.2. What do I have to do, to get the results from the AJAX call? I am not really a JSF expert...
If I had to guess, JSF doesn't have a session associated with the request being sent with curl and therefore the objects associated with the page don't exist. For curl look at http://curl.haxx.se/docs/httpscripting.html section 10, cookies. You would have to pull the page, get the cookies then do the http post with the cookies (starts being a lot of work with curl).
However I would instead suggest looking at Selenium, which has a IDE that generates Java to interact with JavaScript.

JSF 2.0 Ajax and javascript reloading

I use JSF Ajax on my menu links to load the content of the page.
content is loaded with ui:insert and ui:composition with template attribute.
ajax is work and load the content with no reloadin the all page but I have a problem with my java scripts.
when I click menu Items the script reload with every new ajax request.
what should I do to prevent script reloading?
I'm not sure I understood your problem clearly, but maybe you should try to play with the rendered attribute of the h:outputScript component, for instance:
<h:outputScript library="js" name="ajax-util.js"
rendered="#{myBean.renderAjaxFunctions}" />

Resources