Max Katz is a Senior Systems Engineer and Developer Advocate at Exadel. Max is a well-known speaker appearing at many conferences, webinars, and JUG meetings. Max leads Exadel’s RIA and mobile strategy. Part of this role is working as developer advocate for Tiggr Mobile Apps Builder (gotiggr.com), a cloud-based application for building mobile Web and native apps for any device. In addition, Max leads Exadel’s open source projects (exadel.org) such as Fiji, Flamingo, and JavaFX Plug-in for Eclipse. Max has been involved with RichFaces since its inception, publishing numerous articles, providing consulting and training, and authoring the book “Practical RichFaces” (Apress, 2008). Max also co-authored the DZone RichFaces 3 Refcard and the DZone RichFaces 4 Refcard. You can find Max’s writings about RIA and mobile technologies on his blog, mkblog.exadel.com, and you can find his thoughts about these topics and others on Twitter at @maxkatz. Max holds a Bachelor of Science in Computer Science from the University of California, Davis and an MBA from Golden Gate University. Max is a DZone MVB and is not an employee of DZone and has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

RichFaces and Partial JSF View Processing

07.23.2009
| 39582 views |
  • submit to reddit

RichFaces is a rich component library for JSF. RichFaces doesn't replace standard JSF, so you use RichFaces with either the Mojara JSF (Sun RI) implementation or the MyFaces implementation. RichFaces simply provides ready-to-use Ajax components to enable building Ajax-based applications. Another way to look at it is as just lots of extra JSF components beyond what the standard JSF provides. These components include all the necessary JavaScript coding, so you almost never have to work with JavaScript directly.

Although there are over 100 tags in RichFaces, there are just three core concepts that you need to know. Once you are familiar with these concepts, you can use any RichFaces tags and features. The concepts are: (1) sending an AJAX request, (2) partial JSF view rendering, (3) partial JSF view processing. This article covers concept 3: partial JSF view processing. Being able to process just part of the JSF view is important for creating true AJAX regions on the page, validation and performance. 

The a4j:region tag

The a4j:region tag in RichFaces is probably one of the most misunderstood tags, but it provides one of the most important features in RichFaces. With it, server-side processing can be limited to only certain designated components.

One reason for misunderstanding could be the tag name. Many believe that the region tag limits what is rendered in the JSF component tree (to the browser). However, it’s used for the opposite purpose, to mark areas on the JSF component tree to be processed on the server during the Apply Request Values, Process Validations, and Update Model Values phases. Maybe “processRegion” would have been a better name, but we will just deal with what we have.

Before we continue, I want to thank Nick Belaevski (RichFaces project lead and co-author of RichFaces DZone Refcard, Exadel) and Charley Cowens (Technical Writer, Exadel) for helping edit this article.

An Example

Let’s see how the tag works in an example. Suppose there are five input components on a page. Also suppose that you only need one or two of them to be processed on the server. While we cannot change how the form is submitted (all five input field will be submitted), we can control which components will be processed on the server. Components that are not processed on the server, will not be validated and will not be pushed on to the model (during the Update Model phase).

Suppose there is the following page:

<h:form>
<h:panelGrid>
<h:inputText id="input1" >
<a4j:support event="onblur"/>
</h:inputText>
<h:inputText id="input2" >
<a4j:support event="onblur"/>
</h:inputText>
<h:inputText id="input3" >
<a4j:support event="onblur"/>
</h:inputText>
</h:panelGrid>
</h:form>

By default the entire page is a region. Thus a request sent from any of the input fields will process the entire form.

Suppose we only need to process the input1 component. To do that, we can put the component inside an a4j:region tag:

<h:panelGrid>
<a4j:region>
<h:inputText id="input1">
<a4j:support event="onblur" />
</h:inputText>
</a4j:region>
<h:inputText id="input2">
<a4j:support event="onblur" />
</h:inputText>
<h:inputText id="input3">
<a4j:support event="onblur" />
</h:inputText>
</h:panelGrid

In this case, if input1 triggers the submit, only input1 will be processed on the server even though the whole form will still be submitted.

It’s also possible to nest regions:

<h:panelGrid>
<a4j:region>
<a4j:region>
<h:inputText id="input1">
<a4j:support event="onblur" />
</h:inputText>
</a4j:region>
<h:inputText id="input2">
<a4j:support event="onblur" />
</h:inputText>
</a4j:region>
<h:inputText id="input3">
<a4j:support event="onblur" />
</h:inputText>
</h:panelGrid>

In this example, if a request is fired by component input1 (Input components don’t actually fire events. The event is fired by a4j:support), then only the immediate region will be processed on the server. If component input2 fires the event, then both, input1 and input2 will be processed. If component input3 fires the event, as it belongs to the default region that wraps the entire page, all three inputs will be processed.

When using a4j:region with an action component, only the action and listeners registered on this component will be invoked on the server:

<a4j:region>
<a4j:commandButton action="#{bean.save}" value="Click Me"/>
</a4j:region>
Using renderRegionOnly attribute with a4j:region

The a4j:region tag comes with an important attribute called renderRegionOnly which can be set to true or false (false by default). This attribute limits any re-rendering to the current region only. Let’s take this code snippet as an example:

<h:panelGrid columns="2">
<h:outputText value="Name:" />
<h:panelGroup>
<h:inputText id="name" value="#{bean.name}" required="true"
requiredMessage="Name is required"
validatorMessage="Must be 3 characters or longer">
<a4j:support event="onblur" />
<f:validateLength minimum="3" />
</h:inputText>
<rich:message for="name" />
</h:panelGroup>
<h:outputText value="Age:" />
<h:panelGroup>
<h:inputText id="age" value="#{bean.age}" size="4" required="true"
requiredMessage="Age is required"
validatorMessage="Invalid age">
<a4j:support event="onblur" />
<f:validateLongRange minimum="0" />
</h:inputText>
<rich:message for="age" />
</h:panelGroup>
</h:panelGrid>

When you tab out or click out (onblur event) from the first input field without entering anything or entering invalid input (as shown), the entire form is submitted and both fields are validated. Both fields are processed and validated because by default the whole page is a region. That’s not exactly the behavior that we want.

regiontag1

We don’t want to validate fields for which the user hasn’t had a chance to enter anything. Placing each input and message tag inside a region will restrict processing to the region from which the request was sent.

<h:panelGroup>
<a4j:region>
<h:inputText id="name" value="#{bean.name}" required="true"
requiredMessage="Name is required"
validatorMessage="Must be 3 characters or longer">
<a4j:support event="onblur" />
<f:validateLength minimum="3" />
</h:inputText>
<rich:message for="name" />
</a4j:region>
</h:panelGroup>

<h:outputText value="Age:" />
<h:panelGroup>
<a4j:region>
<h:inputText id="age" value="#{bean.age}" size="4" required="true"
requiredMessage="Age is required"
validatorMessage="Invalid age">
<a4j:support event="onblur" />
<f:validateLongRange minimum="0" />
</h:inputText>
<rich:message for="age" />
</a4j:region>
</h:panelGroup>

While we solved the problem of only processing the field which fired the event, we now run into another problem.

  1. Place the mouse cursor inside the name field
  2. Without entering anything (or entering less than 3 characters), leave the field and place the mouse in the age field
  3. An error is displayed next to the name field as expected (only this field is processed)
    regiontag2
  4. Enter a negative number in the age field and leave the field
  5. An error is displayed next to the age field (as expected). However, the error message next to the name field gets cleared (not expected)
    regiontag3

What happened? First, JSF error messages are request-scoped. Secondly, rich:message works just like h:message, but the component is always rendered, whether there is an error or not.

So, how are these two related? When the age field fired an event, only the age field was processed. As the name field wasn’t processed, no error message was queued and the old one is now gone as this is a new request. Because we used rich:message , an empty string value (or empty error) replaced the original error message in the browser.

To help us solve this, we are going to use the renderRegionOnly attribute. For both a4j:region tags, we are setting renderRegionOnly=”true”:

...
<h:panelGroup>
<a4j:region renderRegionOnly="true">
<h:inputText id="name" value="#{bean.name}" required="true"
requiredMessage="Name is required"
validatorMessage="Must be 3 characters or longer">
<a4j:support event="onblur" />
<f:validateLength minimum="3" />
</h:inputText>
<rich:message for="name" />
</a4j:region>
</h:panelGroup>
...
...
<h:panelGroup>
<a4j:region renderRegionOnly="true">
<h:inputText id="age" value="#{bean.age}"
size="4" required="true"
requiredMessage="Age is required"
validatorMessage="Invalid age">
<a4j:support event="onblur" />
<f:validateLongRange minimum="0" />
</h:inputText>
<rich:message for="age" />
</a4j:region>
</h:panelGroup>

This means that any re-rendering is limited to the region from which the request was fired. When we leave the age field without entering anything, the name error is not cleared as we just limited updating to the current region (the region in which age field is).

The desired result is achieved: both error messages are displayed:
regiontag4

One thing to keep in mind, setting renderRegionOnly=”true” doesn’t define what to re-render, it only limits the updates to the current region. What to re-render is determined by rich:message

Using renderRegionOnly allows the creation of a true AJAX region on a page – meaning all input for processing on the server and all output for re-rendering is limited to this particular region.

Using ajaxSingle attribute

A closely related feature to the region is ajaxSingle attribute. The attribute can be applied to just one component while a4j:region can contain any number of components.

<a4j:region>
<a4j:commandButton action="#{bean.save}" value="Click Me"/>
</a4j:region>

Is the same as:

<a4j:commandButton action="#{bean.save}" value="Click Me" 
ajaxSingle="true"/>

And this:

<a4j:region>
<h:inputText id="age" value="#{bean.city}">
<a4j:support event="onblur" />
</h:inputText>
</a4j:region>

Is the same as this:

<h:inputText id="age" value="#{bean.city}">
<a4j:support event="onblur" ajaxSingle="true"/>
</h:inputText>
Using process attribute

Another important attribute is process. It’s available on all RichFaces components that fire an AJAX request and is used in conjunction with ajaxSingle. When ajaxSingle is set to true, we know that only this component will be processed. A situation might arise where you still need to process some specific other components on the page in addition. The process attribute points to those components or containers.

<h:inputText>
<a4j:support event="onblur" ajaxSingle="true" process="mobile"/>
</h:inputText>
...
<h:inputText id="mobile"/>

process can also point to an EL expression or container component id in which case all components inside the container will be processed:

<h:inputText>
<a4j:support event="onblur" ajaxSingle="true"
process="infoPanel"/>
</h:inputText>
<h:panelGrid id="infoPanel">
<h:inputText />
<h:dataTable>
...
</h:dataTable>
</h:panelGrid>
Summary

Partial tree processing is one of the key concepts in RichFaces, so make sure you understand it. Another core concept is partial tree rendering and more specifically rendering content not previously rendered. You can read about it here.

One thing to keep in mind is that regions only work in the context of an AJAX request. Regions don’t work with a non-AJAX request.

RichFaces training and support

Exadel offers custom on-site RichFaces training. Get your entire team up to speed in just 1-2 days (info, outline). Exadel also offers RichFaces support.

Max Katz is a senior system engineer at Exadel. He is the author of “Practical
RichFaces” (Apress) and co-author of RichFaces DZone RefCard.  He has written numerous articles, provided training, and presented at many conferences and webinars about RichFaces and RIA technologies. Max blogs about RichFaces, JavaFX and RIA technologies at http://mkblog.exadel.com

Published at DZone with permission of Max Katz, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Luis Landgrave replied on Thu, 2009/07/23 - 10:33am

Since Exadel's Ajax4JSF version 1.0 I've always fallback to a4j:form for every AJAX call to avoid a4j:support and a4j:region tags, but now I understand the use of these even better. Excellent tutorial.

Oleg Varaksin replied on Wed, 2009/07/29 - 5:42am

Hi Max,

Thanks a lot for your articale. I have one question. How can I archieve the processing and rendering of some specific other components on the page with a4j:region where the ajax request started from? I mean, ajaxSingle can be used with attribute process to reference some other components, but it's valid for just one component. I want to have the same inside of a4j:region with the attribute renderRegionOnly=true. I want to be able to process and render this region (where the ajax request started from) AND process and render some other specified regions.

Is it possible? How does it work?

Best regards.

Oleg.

Max Katz replied on Wed, 2009/07/29 - 2:46pm in response to: Oleg Varaksin

Don't believe it's possible. If you use ajaxSingle="true", the parent component cannot be inside a region.

Oleg Varaksin replied on Thu, 2009/07/30 - 4:13am in response to: Max Katz

Okey, understood. I can only place an a4j:commandButton (or something like) within region and point to some other elements which will be re-rendered. Right? That's what I need because processing makes just sense in the region where the ajax request came from (at least in most scenarios).

Thanks.

Oleg.

 

Max Katz replied on Thu, 2009/07/30 - 11:09am in response to: Oleg Varaksin

Yes, any control that fires an AJAX request and re-render any components on the page.

Shailesh Paranjpe replied on Sat, 2009/08/01 - 10:43am

A Very precise article. Thanks. In case of onblur, is it possible to call a static method of a "utility bean" with the current contents of inputText. I am working on reusable components and the case in point requires me to set the value of the inputText as a transformed value of whatever was typed in by the user. Nomatter which application I use my control, as long as the developer sets the value to the underlying model, the packaged component with the helper and a4j support will do the needful. So the inputText value is someproperty in a backing bean. via oblur and in conjunction with a4j:support, I would like to call a helper method to validate and convert the user input value and set it to the backing bean value Regards -- Shailesh

Max Katz replied on Mon, 2009/08/03 - 11:13am in response to: Shailesh Paranjpe

You can do this:

 <a4j:support event="onblur" action="#{bean.someAction}"/>

Sushant Saini replied on Mon, 2009/08/31 - 2:17pm

Hi Max,

Thanks for this wonderful article. I have started using Richfaces and I must say I am pretty Impressed but I don't think Richfaces 3.3.1 is 508 compliant. Since the current project is for government, 508 is must for us. I created a simple page with menubars and panel tab but could focus on any components using the tab key from keyboard. According to 508 all the elements on page should be accessible using keyboard alone but I cannot achieve this on my page.

 I am doing something wrong here or is it Richfaces?

Max Katz replied on Tue, 2009/09/08 - 12:27pm in response to: Sushant Saini

I would ask on RichFaces forum if all component render 508 compliant HTML. It's also possible you have to set some attributes on the components.

Jan Munioz replied on Mon, 2009/09/21 - 11:02pm

Hi Max, my question is follow.

I have a page ...looks like this.

<code>

 ..taglibs..

<f:subview id="admin">
    <a4j:keepAlive beanName="AutorSeccionBean" />

    <a4j:form id="frmAdmin">
        <rich:messages globalOnly="true" warnClass="richmsgwarn"
            infoClass="richmsginfo" errorClass="richmsgerror" />

        <h:panelGrid styleClass="standard" columns="1"
            columnClasses="rightAlign">
            <h:panelGroup>
                
                <a4j:commandLink oncomplete="#{rich:component('nuevaLeccion')}.show()"
                    action="#{AutorSeccionBean.setNewLeccion}" id="create">
                    <h:outputText value="Import Leccion" />
                    <a4j:actionparam assignTo="#{AutorSeccionBean.tipolecc}" value="0"/>
                </a4j:commandLink>
            </h:panelGroup>
        </h:panelGrid>

        <t:div id="div">
            <t:div rendered="#{! empty AutorSeccionBean.secciones}">
                <rich:dataTable id="secsTable" value="#{AutorSeccionBean.secciones}"
                    var="secs" >
                    
... columns ...                        

                </rich:dataTable>
            </t:div>
            <t:div rendered="#{empty AutorSeccionBean.secciones}">
                <h:outputText value="En este momento no hay lecciones disponibles."
                    styleClass="nodata" />
            </t:div>
        </t:div>
    </a4j:form>
    
    <rich:modalPanel id="nuevaLeccion" autosized="true" width="450" overlapEmbedObjects="true">
        <f:facet name="header">
            <h:outputText value="Crear Lección" />
        </f:facet>
        <f:facet name="controls">
            <h:panelGroup>
                <h:graphicImage value="images/close.png" id="hidelink1"
                    styleClass="hidelink" />
                <rich:componentControl for="nuevaLeccion" attachTo="hidelink1"
                    operation="hide" event="onclick" />
            </h:panelGroup>
        </f:facet>
        <h:form>
            <h:panelGrid columns="1">
                <a4j:outputPanel ajaxRendered="true">
                    <h:panelGrid columns="2" styleClass="tabFormulario"
                        columnClasses="colEnuncia, colDatos70">
                        <h:outputText value="#{msgs.txtNombre} *" />
                        <h:panelGroup>
                            <h:inputText value="#{AutorSeccionBean.nombre}" id="nombre"
                                size="40" required="true" label="#{msgs.txtNombre}"/>
                            <rich:message styleClass="errorMessage" for="nombre" />
                        </h:panelGroup>
                        <h:outputText value="#{msgs.txtPagInicio} *" />
                        <h:panelGroup>
                            <h:inputText value="#{AutorSeccionBean.webLaunch}" id="webLaunch"
                                size="20" required="true" label="#{msgs.txtPagInicio}"/>
                            <rich:message styleClass="errorMessage" for="webLaunch" />
                        </h:panelGroup>
                    </h:panelGrid>
                </a4j:outputPanel>
                <a4j:commandButton value="#{msgs.txtGuardar}"
                    action="#{AutorSeccionBean.saveAction}"  id="save"  reRender="div, uploadPanel"
                    oncomplete="if (#{facesContext.maximumSeverity == 'Info'}||#{facesContext.maximumSeverity==null}) #{rich:component('nuevaLeccion')}.hide();">
                    <a4j:actionparam assignTo="#{AutorSeccionBean.evaluacion}" value="0"/>
                    <a4j:actionparam assignTo="#{AutorSeccionBean.tipoAicc}" value="sco"/>
                </a4j:commandButton>
                <a4j:commandButton value="#{msgs.btnCancel}" oncomplete="#{rich:component('nuevaLeccion')}.hide();return false;"
                    immediate="true"/>
            </h:panelGrid>
        </h:form>
    </rich:modalPanel>

... others modalpanel ....

</f:subview>

</code>

Excuse me my english.

1. Its a modalpanel for create a new Entity. The entity hace two fields, well... many fields, but two on user form. Both fields are required.

Steps.

a. Click on commandLink with id create. This call the action setNewLeccion, this set the strings nombre and webLaunch on AutorSeccionBean to empty strings.

b. Open the modalDialog. 

c.  Click on commandButton with id save on open modalDialog. Then validation fired. Fill one field, click again on commandButton, and validation for one field persist. Value Required for any of two fields. 

d. Press Cancel Button. Hide the ModalPanel.

e. Click on commandLink with id create. Action setNewLeccion is called, but field have the last entry on inputText. 

I need clear values for inputText.

This CRUD page. When i try edit on same modalPanel or in another, the result is same, the required field, saves the text after i press the cancel button.

Thanks in advanced.

Regards.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.