Programmatic control of partial processing in RichFaces 4

Version 2

    JSF 2 introduced built-in support for Ajax, so RichFaces 4 Ajax API provides extensions of JSF Ajax API. PartialViewContext class is an entry point to JSF Ajax. It provides the following functionality:

    • Check and modify set of component IDs that will be executed/rendered
    • Check whether current request is Ajax request

     

    Note: you have to use client ids working with PartialViewContext, short ids won’t work. See this link for information on the differences between short ids and client ids.

     

    Instance of current PartialViewContext can be obtained from FacesContext via getPartialViewContext() method.

     

    RichFaces 4 provides ExtendedPartialViewContext class that extends JSF PartialViewContext with the following functionality:

    • Allows to add client-side event handlers executed before DOM updates and when request completеd
    • Allows to control limiting of rendering to the set of ids defined using render attribute
    • Define contextual client-side data for arbitrary component
    • Understands extended variants of client ids

     

    As this is an extension of standard PartialViewContext, all of its methods are available for usage. To operate with ExtendedPartialViewContext, get its instance using static getInstance(FacesContext) method. AjaxContext class of RichFaces 3 provided similar methods:

     

    RichFaces 3.xRichFaces 4.x
    getCurrentInstance(FacesContext)getInstance(FacesContext)
    getAjaxAreasToProcess()getExecuteIds()
    getAjaxAreasToRender()getRenderIds()
    addRenderedArea(String)/
    removeRenderedArea(String)/

    getAjaxRenderedAreas()
    Not applicable to JSF 2
    Not availablegetOnbeforedomupdate()
    getOncomplete()getOncomplete()
    Not availableappendOnbeforedomupdate(Object)
    setOncomplete(Object)appendOncomplete(Object)
    isAjaxRequest()isAjaxRequest()
    getResponseData()getResponseData()
    getResponseDataMap()getResponseComponentDataMap()
    isLimitToList()/setLimitToList()isLimitRender()/setLimitRender()
    isSelfRender()/setSelfRender()Not available/supported
    addAreasToProcessFromComponent(
    FacesContext, UIComponent)

    addComponentToAjaxRender(UIComponent, String)

    addComponentToAjaxRender(UIComponent)

    addRegionsFromComponent(UIComponent)
    Not available, use UIComponent#findComponent(String) and UIComponent#getClientId(FacesContext) as replacement

     

    The following small example shows programmatical Ajax updates control in action:

     

    package org.richfaces.demo;
     
    import java.util.Formatter;
    import java.util.Random;
     
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.RequestScoped;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
     
    import org.ajax4jsf.javascript.JSFunction;
    import org.richfaces.context.ExtendedPartialViewContext;
     
    @ManagedBean
    @RequestScoped
    public class AjaxControlBean {
     
        @ManagedProperty(value = "#{facesContext}")
        private FacesContext facesContext;
        
        private int randomNumber = new Random().nextInt(36) + 1;
        
        private int componentToUpdateNumber = new Random().nextInt(6) + 1;
     
        public void setFacesContext(FacesContext facesContext) {
            this.facesContext = facesContext;
        }
     
        public int getRandomNumber() {
            return randomNumber;
        }
        
        public void setupCellUpdate() {
            ExtendedPartialViewContext partialViewContext = ExtendedPartialViewContext.getInstance(facesContext);
     
            UIComponent component = UIComponent.getCurrentComponent(facesContext);
     
            UIComponent componentToUpdate = component.findComponent("output_" + componentToUpdateNumber);
            
            //add component for update
            partialViewContext.getRenderIds().add(componentToUpdate.getClientId(facesContext));
            
            //setup event handlers
            partialViewContext.appendOnbeforedomupdate(new JSFunction("alert", "Preparing update of component, standby..."));
            partialViewContext.appendOncomplete(new Formatter().format("alert('Component %1$s has been updated!')", componentToUpdateNumber));
        }
    }
    

     

     

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:a4j="http://richfaces.org/a4j"
              xmlns:c="http://java.sun.com/jsp/jstl/core">
    <f:view>
              <h:head>
                        <style type="text/css">
                                  .box {
                                            border: 1px solid red; 
                                            width: 40px; 
                                            height: 40px; 
                                            text-align: center;
                                  }
                        </style>
              </h:head>
              <h:body>
                        <h:form>
                                  <c:forEach begin="1" end="6" varStatus="status">
                                            <h:panelGroup id="output_#{status.index}" styleClass="box" layout="block">
                                                      #{ajaxControlBean.randomNumber}
                                            </h:panelGroup>
                                  </c:forEach>
      
                                  <a4j:commandButton value="Update one of cells" action="#{ajaxControlBean.setupCellUpdate}" />
                        </h:form>
              </h:body>
    </f:view>
    </html>
    

     

    When user clicks link, bean randomly chooses one of components to update.