Check Ajax request

UI Components for JSF
Post Reply
User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

18 Jan 2010, 23:42

Hello,

I need to distinguish between Ajax and not Ajax requests. I didn't check it yet, but the question is whether the following code works with PrimeFaces

Code: Select all

FacesContext fc = FacesContext.getCurrentInstance();
boolean isAjax = fc.getPartialViewContext().isAjaxRequest();
if (isAjax) {
    ....
}
I read that following parameters must be present in an Ajax request (see JSF 2 spec. 14.2.4 "Request Sending Specifics").

Code: Select all

javax.faces.partial.ajax
javax.faces.source
javax.faces.partial.execute 
javax.faces.partial.render 
javax.faces.partial.ajax is e.g. true. However PrimeFaces sends something like.

Code: Select all

ajaxSource	j_id8:j_id9
primefacesAjaxRequest	true
primefacesPartialProcess	j_id8:j_id9
Can we still trust the call fc.getPartialViewContext().isAjaxRequest()?

Thanks.
Oleg.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

19 Jan 2010, 11:41

The problem will also occur if somebody will check ajax request in quite normally servlet (e.g. security filter). He takes JSF 2 specification and looks for specified request header / request parameters to find out whether the current request has been caused by ajax. Such scenario would be imaginable.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

19 Jan 2010, 23:42

No it'll not work, the key is primefacesAjaxRequest parameter, if it exists then it's an ajax initiated by PrimeFaces. I was thinking about extending PartialViewContext isAjaxRequest to realize PrimeFaces as well but not sure. PrimeFaces will have a RequestContext helper utility soon for various use cases, it could easily have RequestContext.isAjaxRequest().

A future enhancement would be configure PrimeFaces to use that standard parameters for JSF 2.

User avatar
Oleg
Expert Member
Posts: 3805
Joined: 02 Oct 2009, 09:41
Location: Germany, Black Forest

20 Jan 2010, 10:21

cagatay.civici wrote:No it'll not work, the key is primefacesAjaxRequest parameter, if it exists then it's an ajax initiated by PrimeFaces. I was thinking about extending PartialViewContext isAjaxRequest to realize PrimeFaces as well but not sure. PrimeFaces will have a RequestContext helper utility soon for various use cases, it could easily have RequestContext.isAjaxRequest().

A future enhancement would be configure PrimeFaces to use that standard parameters for JSF 2.
Yes, we need these standard parameters for JSF 2. How I already mentioned there are Filters (various Servlets) which don't have any knowledge about used JSF libraries. They are uncoupled from JSF specific libraries (don't matter if PrimeFaces / RichFaces / ICEFaces) and want to find out only ajax requests. Alone precondition - request is caused by some JSF application. They can only rely on standards.
PrimeFaces Cookbook (2. edition): http://ova2.github.io/primefaces-cookbook/ Learning Angular UI Development with PrimeNG: https://github.com/ova2/angular-develop ... th-primeng Blog: https://medium.com/@OlegVaraksin

cagatay.civici
Prime
Posts: 18616
Joined: 05 Jan 2009, 00:21
Location: Cybertron
Contact:

21 Jan 2010, 12:36

I totally agree.

Ironhide2012
Posts: 62
Joined: 03 Aug 2012, 21:01

09 Apr 2013, 19:45

optimus.prime wrote:No it'll not work, the key is primefacesAjaxRequest parameter, if it exists then it's an ajax initiated by PrimeFaces. I was thinking about extending PartialViewContext isAjaxRequest to realize PrimeFaces as well but not sure. PrimeFaces will have a RequestContext helper utility soon for various use cases, it could easily have RequestContext.isAjaxRequest().

A future enhancement would be configure PrimeFaces to use that standard parameters for JSF 2.
Hi, I also need to distinguish between ajax and non-ajax calls in a servlet filter. So, are you saying all primefaces ajax calls will have the httpservletrequest parameter of "primefacesAjaxRequest"? Or is it an httpservletrequest attribute? Thanks

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 57 guests