JBoss.orgCommunity Documentation

Developer Guide

Developing applications using RichFaces 4 (draft)

Logo

Abstract

Read this book for a comprehensive guide to getting started and working with RichFaces 4. It includes details of the architecture, the framework's use in different applications, and skinning implementations.


The RichFaces framework is a rich component library for JavaServer Faces (JSF). It allows integration of Ajax capabilities into enterprise web application development without needing to use JavaScript.

RichFaces leverages several parts of the JSF2 framework including lifecycle, validation, conversion facilities, and management of static and dynamic resources. The RichFaces framework includes components with built-in Ajax support and a customizable look-and-feel that can be incorporated into JSF applications.

RichFaces provides a number of advantages for enterprise web application development:

Follow the instructions in this chapter to configure the RichFaces framework and get started with application development. RichFaces applications can be developed using JBoss Tools, as described in Section 2.3, “Creating a project with JBoss Tools”; or using Maven, as described in Section 2.4, “Creating a project with Maven”.

If you have existing projects that use a previous version of RichFaces, refer to the RichFaces Migration Guide.

Follow the instructions in this section to set up the RichFaces framework and begin building applications.

  1. Download RichFaces archive

    Download RichFaces from the JBoss RichFaces Downloads area at http://www.jboss.org/richfaces/download.html. The binary files (available in .bin.zip or .bin.tar.gz archives) contain a compiled, ready-to-use version of RichFaces with a set of basic skins.

  2. Unzip archive

    Create a new directory named RichFaces, then unzip the archive containing the binaries there.

Apache Maven is a build automation and project management tool for Java projects. Follow the instructions in this section to create a Maven project for RichFaces.

Maven can be downloaded and installed from Apache's website at http://maven.apache.org/download.html. Version 2.2.1 is recommended.

Once Maven has been installed, no further configuration is required to begin building Maven projects.

A Maven archetype is a template for creating projects. Maven uses an archetype to generate a directory structure and files for a particular project, as well as creating pom.xml files that contain build instructions.

The RichFaces Component Development Kit includes a Maven archetype named richfaces-archetype-simpleapp for generating the basic structure and requirements for a RichFaces application project. Maven can obtain the archetype from the JBoss repository at https://repository.jboss.org/nexus/content/groups/public/. The archetype is also included with the RichFaces source code. Follow the procedure in this section to generate a new Maven-based RichFaces project using the archetype.

  1. Add required repository

    The details for the JBoss repository need to be added to Maven so it can access the archetype. Add a profile in the maven_installation_folder/conf/settings.xml file under the <profiles> element:

    
    <profiles>
        ...
        <profile>
            <id>jboss-public-repository</id>
            <repositories>
                <repository>
                    <id>jboss-public-repository-group</id>
                    <name>JBoss Public Maven Repository Group</name>
                    <url>https://repository.jboss.org/nexus/content/groups/public/</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>jboss-public-repository-group</id>
                    <name>JBoss Public Maven Repository Group</name>
                    <url>https://repository.jboss.org/nexus/content/groups/public/</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    The profile then needs to be activated in the <activeProfiles> element:

    
    <activeProfiles>
        <activeProfile>jboss-public-repository</activeProfile>
    </activeProfiles>
  2. Generate the project from the archetype

    The project can now be generated with the richfaces-archetype-simpleapp archetype. Create a new directory for your project, then run the following Maven command in the directory:

    mvn archetype:generate -DarchetypeGroupId=org.richfaces.archetypes -DarchetypeArtifactId=richfaces-archetype-simpleapp -DarchetypeVersion=4.0.0-SNAPSHOT -DgroupId=org.docs.richfaces -DartifactId=new_project

    The following parameters can be used to customize your project:

    -DgroupId

    Defines the package for the Managed Beans

    -DartifactId

    Defines the name of the project

    The command generates a new RichFaces project with the following structure:

    new_project
    	├── pom.xml
    	└── src
    		└── main
    		    ├── java
    		    │   └── org
    		    │       └── docs
    		    │           └── richfaces
    		    │               └── RichBean.java
    		    └── webapp
    		        ├── index.xhtml
    		        ├── templates
    		        │   └── template.xhtml
    		        └── WEB-INF
    		            ├── faces-config.xml
    		            └── web.xml
  3. Add test dependencies (optional)

    Your root directory of your project contains a project descriptor file, pom.xml. If you wish to include modules for test-driven JSF development, add any dependencies for the tests to the pom.xml file. For full details on how to use the jsf-test project, refer to http://community.jboss.org/wiki/TestDrivenJSFDevelopment.

  4. Build the project

    Build the project from the command line by entering the mvn install command.

    The BUILD SUCCESSFUL message indicates the project has been assembled and is ready to import into an IDE (integrated development environment), such as JBoss Tools.

  5. Import the project into an IDE

    Import the Maven project into your IDE. For Eclipse and JBoss Tools, you can import the project using the M2Eclipse plug-in.

    To install the plug-in, choose HelpInstall New Software from the menu. Type Maven to locate the Maven Integration for Eclipse Update Site entry, then type Maven in the filter to show the available plug-ins. Follow the prompts to install the Maven Integration for Eclipse plug-in.

    With the plug-in installed, open the importing wizard by choosing FileImport from the menu. Select MavenExisting Maven Projects as the import source and choose the pom.xml file for your project.

Your project is now ready to use. Once components and functionality have been added, you can run the application on a server and access it through a web browser at the address http://localhost:8080/jsf-app/.

Documentation in development

Some concepts covered in this chapter may refer to the previous version of Richfaces, version 3.3.3. This chapter is scheduled for review to ensure all information is up to date.

The RichFaces framework is a component library which enhances JSF Ajax capabilities, such that you don't need to write any extra JavaScript code or replace any existing components with new Ajax widgets. RichFaces also enables page-wide Ajax support instead of the traditional component-wide support. Events can be defined on pages that invoke Ajax requests. After an Ajax request, the areas of a page that are synchronized with the JSF Component Tree can themselves change data on the server according to events fired on the client.

Figure 3.1, “Request processing flow” illustrates how requests are processed in the RichFaces framework.

Figure 3.1. Request processing flow


Using JSF tags, RichFaces allows different parts of a JSF page to be updated with an Ajax request. JSF pages using RichFaces do not change from "regular" JSF pages, and additional JavaScript code is not required.

RichFaces has been developed with an open architecture to be compatible with a wide variety of environments.

The important elements of the RichFaces framework are as follows:

Read this section for details on each element.

The source code for the RichFaces framework can be downloaded and compiled manually rather than using the pre-compiled binaries.

  1. Download source code

  2. Unzip archive

    Create a new directory named RichFaces, then unzip the archive containing the source code there.

  3. Configure Maven with JBoss developer settings

    To compile the RichFaces source code, Maven requires access to JBoss development repositories. Edit the maven_installation_folder/conf/settings.xml file and add the following settings:

    Server definitions
    
    <servers>
        <server>
            <id>jboss-developer-repository-group</id>
            <username>username</username>
            <password>password</password>
        </server>
        <server>
            <id>jboss-screenshots-repository</id>
            <username>username</username>
            <password>password</password>
        </server>
        <server>
            <id>jboss-releases-repository</id>
            <username>username</username>
            <password>password</password>
        </server>
        ...
    </servers>

    Change the username and password in your server definitions to your JBoss.org username and password.

    Mirror definitions
    
    <mirrors>
        <mirror>
            <id>jboss-developer-repository-group</id>
            <mirrorOf>*,!jboss-deprecated</mirrorOf>
            <name>JBoss.org Developer Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/groups/developer/</url>
        </mirror>
        ...
    </mirrors>
    Profile definitions
    
    <profiles>
        <!-- Redefine the Maven central repository to enable snapshots.  The url will be replaced by the mirror -->
        <profile>
            <id>jboss-nexus</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <name>Central repository proxy</name>
                    <url>replaced by mirror settings</url>
                    <layout>default</layout>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>replaced by mirror settings</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        ...
    </profiles>
    Activate profile
    
    <activeProfiles>
        <activeProfile>jboss-nexus</activeProfile>
        ...
    </activeProfiles>
  4. Compile using Maven

    In the root directory of the unzipped source code, enter the following command to compile and build the RichFaces framework:

    mvn clean install

    Add any of the following options after the command to customize the build:

    -P release,docs

    Maven additionally builds the documentation and release artifacts.

    -D skipTests=true

    Maven skips the unit and functional tests, which completes the build faster.

    -D checkstyle.skip=true

    Maven skips the scans for checking style, which completes the build faster.

    -D skip-source

    Maven does not produce any source jar files from the build.

    -D skip-enforce

    Maven does not check for the correct JDK version, Maven version, and SNAPSHOT plug-in, and forces the build.

    -D jsf-profile=implementation

    Maven tests builds and tests against the specified JSF implementation. The implementation value can be any of the following:

    • jsf_ri: The latest release of JSF RI (Mojarra). This is the default implemenation.

    • jsf_ri_javax: The latest release of javax.faces.

    • jsf_ri_snapshot: The latest snapshot version of JSF RI (Mojarra).

    • myfaces: The latest release of MyFaces.

    • myfaces: The latest snapshot version of MyFaces.

  5. Access compiled binaries

    The compiled libraries are located in the following sub-directories of the root RichFaces directory:

    • ui/assembly/target/dist/richfaces-ui-version-SNAPSHOT.jar

    • framework/api/target/richfaces-api-version-SNAPSHOT.jar

    • framework/impl/target/richfaces-impl-version-SNAPSHOT.jar

    If Maven built the framework using the -P release option, the distribution package containing all three compiled libraries and reference documentation is located at ui/assembly/target/dist/richfaces-ui-version-SNAPSHOT-bin.zip.

You can now add the compiled libraries to your project as described in Step 2 of Section 2.3, “Creating a project with JBoss Tools”.

Documentation in development

Some concepts covered in this chapter may refer to the previous version of Richfaces, version 3.3.3. This chapter is scheduled for review to ensure all information is up to date.

Read this chapter for the basic concepts of using RichFaces in conjunction with Ajax and JavaServer Faces.

Many of the tags in the a4j and rich tag libraries are capable of sending Ajax requests from a JavaServer Faces (JSF) page. RichFaces tags hide the usual JavaScript activities that are required for an XMHTTPRequest object building and an Ajax request sending. Additionally the tags can determine which components of a JSF page are to be re-rendered as a result of the Ajax response; refer to Section 4.2.1, “Partial page updates” for details.

  • The <a4j:commandButton> and <a4j:commandLink> tags are used to send an Ajax request on the click JavaScript event.

  • The <a4j:poll> tag is used to send an Ajax request periodically using a timer.

  • The <a4j:ajax> tag allows you to add Ajax functionality to standard JSF components and send Ajax request on a chosen JavaScript event, such as keyup or mouseover, for example.

  • Most components in the rich tag library have built-in Ajax support. Refer to the RichFaces Component Reference for details on the use of each component.

Documentation in development

Some concepts covered in this chapter may refer to the previous version of Richfaces, version 3.3.3. This chapter is scheduled for review to ensure all information is up to date.

Read this chapter for details on some of the advanced features and configuration possibilities for the RichFaces framework.

Scripts and styles are normally loaded into a RichFaces application on demand. The default loading strategy can be altered to suit certain applications by specifying the strategy in the web.xml file.

RichFaces allows standard handlers to be defined for processing different application exceptions. Custom JavaScript can be executed when these exceptions occur.

To define handlers for application exceptions, add the following code to your web.xml file:


<context-param>
    <param-name>org.ajax4jsf.handleViewExpiredOnClient</param-name>
    <param-value>true</param-value>
</context-param>

Documentation in development

Some concepts covered in this chapter may refer to the previous version of Richfaces, version 3.3.3. This chapter is scheduled for review to ensure all information is up to date.

Read this chapter for a guide to skinning and theming RichFaces applications, including how to implement themes, and details on customizing and extending skins.

RichFaces skins are designed to use a combination of styling elements. Themes for components can be applied using any of the following style classes:


The user can change skins at runtime if a managed bean is used to access the skin.

Example 6.2. Skin changing example

This example renders a list of radio buttons from which the user can choose their desired skin. The chosen skin is then applied to the panel bar items.


<h:form>
    <div style="display: block; float: left">
        <h:selectOneRadio value="#{skinBean.skin}" border="0" layout="pageDirection" title="Changing skin" style="font-size: 8; font-family: comic" onchange="submit()">
            <f:selectItem itemLabel="plain" itemValue="plain" />
            <f:selectItem itemLabel="emeraldTown" itemValue="emeraldTown" />
            <f:selectItem itemLabel="blueSky" itemValue="blueSky" />
            <f:selectItem itemLabel="wine" itemValue="wine" />
            <f:selectItem itemLabel="japanCherry" itemValue="japanCherry" />
            <f:selectItem itemLabel="ruby" itemValue="ruby" />
            <f:selectItem itemLabel="classic" itemValue="classic" />
            <f:selectItem itemLabel="laguna" itemValue="laguna" />
            <f:selectItem itemLabel="deepMarine" itemValue="deepMarine" />
            <f:selectItem itemLabel="blueSky Modified" itemValue="blueSkyModify" />
        </h:selectOneRadio>
    </div>
    <div style="display: block; float: left">
        <rich:panelBar height="100" width="200">
            <rich:panelBarItem label="Item 1" style="font-family: monospace; font-size: 12;">
                Changing skin in runtime
            </rich:panelBarItem>
    
            <rich:panelBarItem label="Item 2" style="font-family: monospace; font-size: 12;">
                This is a result of the modification "blueSky" skin
            </rich:panelBarItem>
        </rich:panelBar>
    </div>
</h:form>

Figure 6.1. Skin changing example



  1. Create the skin file

    The name of the skin file should follow the format new_skin_name.skin.properties and is placed in either the META-INF/skins/ directory or the classpath directory of your project.

  2. Define skin constants

    Add skin parameter constants and values to the file. Example 6.3, “blueSky.skin.properties file” shows how the skin parameters listed in Table 6.1, “Parameter settings for the blueSky skin” are included in the skin file.

    Example 6.3. blueSky.skin.properties file

    The blueSky.skin.properties file lists all the skin parameter constants for the skin. It can be extracted from the /META-INF/skins directory in the richfaces-impl-version.jar package.

    #Colors
    headerBackgroundColor=#BED6F8
    headerGradientColor=#F2F7FF
    headerTextColor=#000000
    headerWeightFont=bold
    
    generalBackgroundColor=#FFFFFF
    generalTextColor=#000000
    generalSizeFont=11px
    generalFamilyFont=Arial, Verdana, sans-serif 
    
    controlTextColor=#000000
    controlBackgroundColor=#ffffff
    additionalBackgroundColor=#ECF4FE
    
    shadowBackgroundColor=#000000
    shadowOpacity=1
    
    panelBorderColor=#BED6F8
    subBorderColor=#ffffff
    
    tabBackgroundColor=#C6DEFF
    tabDisabledTextColor=#8DB7F3
    
    trimColor=#D6E6FB
    
    tipBackgroundColor=#FAE6B0 
    tipBorderColor=#E5973E 
    
    selectControlColor=#E79A00
    
    generalLinkColor=#0078D0
    hoverLinkColor=#0090FF
    visitedLinkColor=#0090FF
    
    # Fonts
    headerSizeFont=11px
    headerFamilyFont=Arial, Verdana, sans-serif
    
    tabSizeFont=11
    tabFamilyFont=Arial, Verdana, sans-serif
    
    buttonSizeFont=11
    buttonFamilyFont=Arial, Verdana, sans-serif
    
    tableBackgroundColor=#FFFFFF
    tableFooterBackgroundColor=#cccccc
    tableSubfooterBackgroundColor=#f1f1f1
    tableBorderColor=#C0C0C0
    tableBorderWidth=1px
    
    #Calendar colors
    calendarWeekBackgroundColor=#F5F5F5
    
    calendarHolidaysBackgroundColor=#FFEBDA
    calendarHolidaysTextColor=#FF7800
    
    calendarCurrentBackgroundColor=#FF7800
    calendarCurrentTextColor=#FFEBDA
    
    calendarSpecBackgroundColor=#E4F5E2
    calendarSpecTextColor=#000000
    
    warningColor=#FFE6E6
    warningBackgroundColor=#FF0000
    
    editorBackgroundColor=#F1F1F1
    editBackgroundColor=#FEFFDA
    
    #Gradients
    gradientType=plain
    

    Alternatively, instead of redefining an entire new skin, your skin can use an existing skin as a base on which to build new parameters. Specify a base skin by using the baseSkin parameter in the skin file, as shown in Example 6.4, “Using a base skin”.


  3. Reference skin definition

    Add a skin definition <context-param> to the web.xml settings file of your application:

    <context-param>
        <param-name>org.richfaces.SKIN</param-name>
        <param-value>new_skin_name</param-value>
    </context-param>

Standard HTML controls and components used alongside RichFaces and JSF components can also be themed to create a cohesive user interface. The following HTML elements accept skinning:

Skinning for standard HTML controls can be included in one of two ways:

There are two levels of skinning for HTML controls, depending on whether the browser viewing the application includes rich visual styling capabilities, and whether the browser supports features of CSS2 and CSS3.

Browser lists may need to be updated.

Extended skinning

  • Microsoft Internet Explorer 7 in standards-compliant mode

  • Mozilla Firefox

If the browser type cannot be identified, the extended level is used. Set the level explicitly by adding the following context parameter to the web.xml configuration file, and specifying the <param-value> element as either basic or extended:


<context-param>
    <param-name>org.richfaces.CONTROL_SKINNING_LEVEL</param-name>
    <param-value>basic</param-value>
</context-param>

RichFaces uses XCSS (XML-formatted CSS) files to add extra functionality to the skinning process. XCSS files can contain all the styling information for each RichFaces component in the library.

XCSS files contain mappings between CSS properties and skin parameters. The name attribute of the <u:selector> element is the name of the CSS selector. Each <u:style> element defines a CSS property with the name attribute as its name. Using the skin attribute specifies a skin parameter from the current skin file, while using the value attribute enters the literal value in the CSS file. An example of this is shown in Example 6.5, “XCSS style mappings”.


Style properties can be modified using XML-based XCSS code, or using embedded standard CSS code, as shown in Example 6.6, “Using XCSS code or standard CSS code”


Plug-n-skin is an alternate method to create, customize, and add a skin. The skin can be based on an existing RichFaces skin, and can include support for skinning standard HTML controls.

  1. Create a template

    Use the Maven build and deployment tool to create the skin template by using the following command:

    mvn archetype:create -DarchetypeGroupId=org.richfaces.cdk -DarchetypeArtifactId=maven-archetype-plug-n-skin -DarchetypeVersion=RF-VERSION -DartifactId=ARTIFACT-ID -DgroupId=GROUP-ID -Dversion=VERSION

    Use the following parameters for the command:

  2. Add the skin to the CDK

    Change to the newly-created directory. Ensure it contains the pom.xml project file, then enter the following command to create a new skin and add it to the CDK (Component Development Kit):

    mvn cdk:add-skin -Dname=SKIN-NAME -Dpackage=SKIN-PACKAGE

    Use the following parameters for the command:

    Use the following optional keys for advanced features:

    The command creates the following files:

  3. Edit XCSS files

    Edit the XCSS files contained in the src/main/resources/META-INF/skins/ directory. Refer to Section 6.8, “Defining skins for individual components” for instructions on how to edit XCSS files.

  4. Build the skin

    After editing the XCSS files, build the skin by running the following command in the root directory of your skin project (the directory that contains the pom.xml file).

    mvn clean install
  5. Add the skin to the project configuration

    Add the following context parameter to your project's web.xml configuration file to use the new skin in your application:

    <context-param>
        <param-name>org.ajax4jsf.SKIN</param-name>
        <param-value>SKIN-NAME</param-value>
    </context-param>