JSF Ajax refresh issue - ajax

In my JSF page, I have a dropdown that needs to be populated onload of the page which is set in request scope, now I have a Ajax call that refreshes a part of the page.
The Ajax piece works fine but the issue is on refresh it also calls the getter for the Dropdown and returns null ( because its in request scope). To me it makes no sense to call the getter for a component that's not part of the Ajax refresh. Am I doing it correct , please suggest.

You'd have to specify which component you'd like to refresh. Depending on whether you are using jsf2 or richfaces (or any other technology), it is different, but it is often said in an attribute of the action component called render / reRender

Change the scope of managed bean to Session...

Related

Is there a way to dynamically configure error pages for Exceptions in JSF/AJAX?

I would like to dynamically set an error page in JSF. Specifically what I would like to do is whenever I get a ViewExpiredException to refresh the page. I am currently using omnifaces FullAjaxExceptionHandler to manage my exception but it reads the error pages from web.xml.
Is there an easy way to do this?
Should I consider client side navigation by using onError event or just overwrite FullAjaxExceptionHandler.findErrorPageLocation ?
It reads error pages from web.xml because that's the standard way how they are used if you didn't use JSF ajax in first place. In other words, the FullAjaxExceptionHandler ensures that the exception handling and error page presentation behaves exactly the same as if when you wouldn't be using ajax by e.g. <h:commandButton> without <f:ajax>, or <p:commandButton ajax="false">, or because enduser has JS disabled.
You can indeed control it by extending the exception handler and overriding findErrorPageLocation method, but then it won't behave anymore the same way as the standard way.
Alternatively, and likely a better way in your specific case, is to just specify a specific error page for ViewExpiredException and grab the initial page by the usual request attribute and let JavaScript redirect to it on page load.
<h:outputScript>
window.location = "#{requestScope['javax.servlet.error.request_uri']}";
</h:outputScript>
As a bonus, it will work exactly the same way as when you wouldn't be using JSF ajax.
I only wonder how this all would improve the user experience. This is likely only more confusing to the enduser; a page which reloads itself without any additional feedback when the session has expired during a postback.

How to implement callback in primefaces ajax request?

we are working on a shopping application. we use jsf and primefaces . we have login form on primefaces dialog. The dialog appears on click of add to cart(dlg.show()) if the user is not logged in or else it adds to cart directly. if user login sucessfully then the item is added to cart successfully.
I refered the following example for login dialog.
https://www.primefaces.org/showcase/ui/overlay/dialog/loginDemo.xhtml
I am not sure how do we have callback in primefaces ajax request. based on the response from the login action i want to call add to cart action.
can anyone suggest better approch for this?
Primefaces provides essentially two options for this:
Primefaces JavaScript API (the hard way), with which you an hook into the Primefacs jsf engine Using the function call Primefaces.ajax.AjaxRequest(cfg). cfg is an array of ajax request parameters that you pass along with the call and can include among other things component ids to ajax process, ids to update,event hooks (oncomplete,onerror etc) and custom request parameters. I'm typing this on a phone so it'll be painful to list an example here but if this is the route you prefer, consult the official Primefaces manual.
<p:remoteCommand/>, the easy way, is implemented as a standard JSF component that gives you all the function and behaviour of a regular JavaScript function and half the typing. With this, you can execute a backing bean function with the behaviour and semantics of a javaScript function

Ajax binding vs Server binding

I have a project which is MVC. In it, I use the telerik grid. I need to choose whether I will user server or ajax binding. What is the difference between these two?
I was told that when I pass data from the controller to the view via model, that is server binding. Is this true? If yes, how else to pass data?
Server binding will refresh the page every time where the Ajax one will be done on the browser with no page reloading. At least that is the difference from the user's prospective.
Ajax binding will need an extra method (the examples on the Telerik website are using " _AjaxBinding()"). That method is called in the grid .ajax() located in the view.
And yes, A normal call is server binding.

Loading data after a JSF page has completed rendering

I'm using Richfaces 4 and Mojarra 2.0.4. So I'll be glad if the answer is specific to Richfaces 4 framework :)
I understand that the JSF components are processed at the server side. Some of my pages are really data centric. So when I load that page (eg: inside a richTab with switchType ajax), it takes a lot of time for my bean to execute the method marked with #PostConstruct to execute and fetch data. And hence the time period between user clicking on the tab's header and actual page rendering is quite large, a really bad user experience. I've implemented lazy loading for the dataTables in the page but that still doesn't optimize my page load time since #PostConstruct is still there.
Is there anyway I can load the page first and THEN fetch the data with ajax and update the page? This loading time is really annoying for the user.Any help appreciated :)
PS: I thought of using the oncomplete and onbeforedomupdate client side events of rich:tab and call ajax functions declared with a4j:function. But it seems that these events are fired before the page is actually switched.
use onload event of <body> tag in html along with <a4j:jsFunction>
If you still feel your page is not rendering completely before the method called then use setTimeout('a4jfunctionName',10) javascript function in onload to complete the rendering first.

Telerik MVC: Loading Grid with ajax request don't work

I have a Telerik MVC Tabstrip.
I have used:
.LoadContentFrom("Grid", "Orders");
"Grid" Action just returns view without model. Then the Ajax request should have been fired to get the data.
It is loading the grid normally but it is not calling the Ajax request to fill the data.
If I am calling the same action normally i.e. without ajax it displays grid and fires Ajax request to load the data.
Perhaps you've missed to register the required JavaScript files for the Grid. You can check the following help topics:
Loading components in tab content
Required JavaScript files
Check the sample available at here.
This one uses MVC 1.0 but can be surely converted to MVC 2.0

Resources