Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Primefaces Cookbook Second Edition

You're reading from   Primefaces Cookbook Second Edition Over 100 practical recipes to learn PrimeFaces 5.x – the most popular JSF component library on the planet

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781784393427
Length 412 pages
Edition 1st Edition
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with PrimeFaces FREE CHAPTER 2. Theming Concepts 3. Enhanced Inputs and Selects 4. Grouping Content with Panels 5. Data Iteration Components 6. Endless Menu Variations 7. Working with Files, Images, and Multimedia 8. Drag Me, Drop Me 9. Creating Charts and Maps 10. Client-side Validation 11. Miscellaneous Advanced Use Cases Index

Partial view submit

One other cool feature of the enhanced AJAX provided by PrimeFaces is the partialSubmit attribute that can be applied to action components and <p:ajax>, where only partially processed components are added to the AJAX requests with their ID's. By default, the JSF and PrimeFaces implementation serializes the whole form to send it via AJAX requests, and eventually, with large views, this will increase the size of the network data traffic that will be posted to the server. To overcome this problem, partial submit can be used to reduce the size of the post data when actions take place on views that have quite a lot of input fields. With this approach, only the ID's of the partially processed fields will be sent to the server.

How to do it…

Partial submit is disabled by default; it can be enabled globally with a context parameter in web.xml, as follows:

<context-param>
  <param-name>primefaces.SUBMIT</param-name>
  <param-value>partial</param-value>
</context-param>

Or, it can be declared with the partialSubmit attribute explicitly on the command action, as follows:

<h:outputLabel for="name" value="Name:" 
  style="font-weight:bold" />
<p:inputText id="name" />
<p:commandButton value="Partial Submit (False)" 
  partialSubmit="false" process="name" />
<p:commandButton value="Partial Submit (True)" 
  partialSubmit="true" process="name" />
<p:inputText /> <p:inputText /> <p:inputText /><br\>
<p:inputText /> <p:inputText /> <p:inputText /><br\>
<p:inputText /> <p:inputText /> <p:inputText /><br\>

How it works…

The visual output of the given code snippet will be as follows. So, here we have two buttons, one with the partialSubmit attribute set to false and another one set to true:

How it works…

When the button with the Partial Submit (False) label is clicked, the AJAX request that will be sent to the server will contain all the ID's of the input text fields that exist on the page. An example output for the AJAX request is extracted from the <p:log> component (a visual console to display internal logs of PrimeFaces) and given here:

Data:javax.faces.partial.ajax=true&javax.faces.source=j_idt19&jav
ax.faces.partial.execute=name&j_idt19=j_idt19&mainForm=mainForm&bo
okTree_selection=0_6&name=mert&j_idt21=&j_idt22=&j_idt23=&j_id
t24=&j_idt25=&j_idt26=&j_idt27=&j_idt28=&j_idt29=&javax.fac
es.ViewState=-6151865609302284540%3A502720797990996178

The ID's that are highlighted belong to the input text fields that exist in the page. If we click on the button with the Partial Submit (True) label, we should get an AJAX request with no chained ID list in the data list:

Data:javax.faces.partial.ajax=true&javax.faces.source=j_idt20&jav
ax.faces.partial.execute=name&j_idt20=j_idt20&name=&javax.faces.Vi
ewState=-6151865609302284540%3A502720797990996178

Note

The partial submit feature does not exist within the core JSF features; it's a feature provided by PrimeFaces.

There's more…

With version 5.2, PrimeFaces introduced partial submit filtering, which allows customization on the AJAX data sent to the server. This comes in handy when you have multiple input fields within a data table, for instance, and try to prevent sending the ID list of those input fields to the server while doing paging, sorting, or row selection. The filter can be defined as a selector and its default value is :input. The example AJAX component in the following code will filter on all the input fields and will not send any data to the server:

<p:ajax event="page" partialSubmit="true"
  partialSubmitFilter=":not(:input)" />

The PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.

The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter1/partialSubmit.jsf.

You have been reading a chapter from
Primefaces Cookbook Second Edition
Published in: May 2015
Publisher:
ISBN-13: 9781784393427
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime