Book Image

Mastering JavaServer Faces 2.2

By : Anghel Leonard
Book Image

Mastering JavaServer Faces 2.2

By: Anghel Leonard

Overview of this book

Table of Contents (20 chapters)
Mastering JavaServer Faces 2.2
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
The JSF Life Cycle
Index

Is it a non-AJAX request?


JSF can answer this question by inspecting request headers or checking the PartialViewContext.isAjaxRequest method. The request headers that provide information about the request type are Faces-Request and X-Requested-With. For an AJAX request, the Faces-Request header will have the value partial/ajax, while the X-Requested-With request type will have the value XMLHttpRequest (in JSF 2.2, X-Requested-With doesn't seem to work; however, for the sake of completeness, you can test them again). In the following screenshot, you can see the headers of a typical JSF 2.2 AJAX request:

In a managed bean, you can determine the type of the request, as shown in the following code:

public void requestTypeAction() {

  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();
  Map<String, String> headers = externalContext.getRequestHeaderMap();
  logger.info(headers.toString());

  //determination...