The framework is implemented as a component library which adds Ajax capability into existing pages, so you don't need to write any JavaScript code or to replace existing components with new Ajax widgets. RichFaces enables page-wide Ajax support instead of the traditional component-wide support. Hence, you can define the event on the page that invokes an Ajax request and the areas of the page that should be synchronized with the JSF Component Tree after the Ajax request changes the data on the server according to the events fired on the client.
Next Figure shows how it works:
RichFaces allows to define (by means of JSF tags) different parts of a JSF page you wish to update with an Ajax request and provide a few options to send Ajax requests to the server. Also JSF page doesn't change from a "regular" JSF page and you don't need to write any JavaScript or XMLHttpRequest objects by hands, everything is done automatically.
Next figure lists several important elements of the RichFaces framework
Ajax Filter. To get all benefits of RichFaces, you should register a Filter in web.xml file of your application. The Filter recognizes multiple request types. The sequence diagram on Figure 3 shows the difference in processing of a "regular" JSF request and an Ajax request.
In the first case the whole JSF tree will be encoded, in the second one option it depends on the "size" of the Ajax region. As you can see, in the second case the filter parses the content of an Ajax response before sending it to the client side.
Have a look at the next picture to understand these two ways:
In both cases, the information about required static or dynamic resources that your application requests is registered in the ResourseBuilder class.
When a request for a resource comes (Figure 4), the RichFaces filter checks the Resource Cache for this resource and if it is there, the resource is sent to the client. Otherwise, the filter searches for the resource among those that are registered by the ResourceBuilder. If the resource is registered, the RichFaces filter will send a request to the ResourceBuilder to create (deliver) the resource.
Next Figure shows the ways of resource request processing.
AJAX Action Components. There are Ajax Action Components: AjaxCommandButton, AjaxCommandLink, AjaxPoll and AjaxSupport and etc. You can use them to send Ajax requests from the client side.
AJAX Containers. AjaxContainer is an interface that describes an area on your JSF page that should be decoded during an Ajax request. AjaxViewRoot and AjaxRegion are implementations of this interface.
JavaScript Engine. RichFaces JavaScript Engine runs on the client-side. It knows how to update different areas on your JSF page based on the information from the Ajax response. Do not use this JavaScript code directly, as it is available automatically.
In order to create RichFaces applications properly, keep the following points in mind:
There are different ways to send Ajax requests from your JSF page. For example you can use <a4j:commandButton>, <a4j:commandLink>, <a4j:poll> or <a4j:support> tags or any other.
All these tags hide the usual JavaScript activities that are required for an XMHttpRequest object building and an Ajax request sending. Also, they allow you to decide which components of your JSF page are to be re-rendered as a result of the Ajax response (you can list the IDs of these components in the "reRender" attribute).
<a4j:commandButton> and <a4j:commandLink> tags are used to send an Ajax request on "onclick" JavaScript event.
<a4j:poll> tag is used to send an Ajax request periodically using a timer.
The <a4j:support> tag allows you to add Ajax functionality to standard JSF components and send Ajax request onto a chosen JavaScript event: "onkeyup", "onmouseover", etc.
Most important attributes of components that provide Ajax request calling features are:
<h:inputText size="50" value="#{bean.text}">
<a4j:support event="onkeyup" RequestDelay="3"/>
</h:inputText>
So every next request from the frequent keyboard events will be delayed on 3 ms to reduce the number of requests.
You may describe a region on the page you wish to send to the server, in this way you can control what part of the JSF View is decoded on the server side when you send an Ajax request.
The easiest way to describe an Ajax region on your JSF page is to do nothing, because the content between the <f:view> and </f:view> tags is considered the default Ajax region.
You may define multiple Ajax regions on the JSF page (they can even be nested) by using the <a4j:region> tag.
If you wish to render the content of an Ajax response outside of the active region then the value of the "renderRegionOnly" attribute should be set to "false" ("false" is default value). Otherwise, your Ajax updates are limited to elements of the active region.
Using IDs in the "reRender" attribute to define "AJAX zones" for update works fine in many cases.
But you can not use this approach if your page contains, e.g. a <f:verbatim> tag and you wish to update its content on an Ajax response.
The problem with the <f:verbatim/> tag as described above is related to the value of the transientFlag of JSF components. If the value of this flag is true, the component must not participate in state saving or restoring of process.
In order to provide a solution to this kind of problems, RichFaces uses the concept of an output panel that is defined by the <a4j:outputPanel> tag. If you put a <f:verbatim> tag inside of the output panel, then the content of the <f:verbatim/> tag and content of other panel's child tags could be updated on Ajax response. There are two ways to control this:
RichFaces allows to redefine standard handlers responsible for processing of different exceptional situations. It helps to define own JavaScript, which is executed when these situations occur.
To execute your own code on the client in case of an error during Ajax request, it's necessary to redefine the standard "A4J.AJAX.onError" method:
A4J.AJAX.onError = function(req,status,message) {
// Custom Developer Code
};
The function defined this way accepts as parameters:
Thus, it's possible to create your own handler that is called on timeouts, inner server errors, and etc.
It's possible to redefine also the "onExpired" framework method that is called on the "Session Expiration" event.
Example:
A4J.AJAX.onExpired = function(loc,expiredMsg){
// Custom Developer Code
};
Here the function receives in params:
If you have a look at a CSS file in an enterprise application, for example, the one you're working on now, you'll see how often the same color is noted in it. Standard CSS has no way to define a particular color abstractly for defining as a panel header color, a background color of an active pop-up menu item, a separator color, etc. To define common interface styles, you have to copy the same values over and over again and the more interface elements you have the more copy-and-paste activity that needs to be performed.
Hence, if you want to change the application palette, you have to change all interrelating values, otherwise your interface can appear a bit clumsy. The chances of such an interface coming about is very high, as CSS editing usually becomes the duty of a general developer who doesn't necessarily have much knowledge of user interface design.
Moreover, if a customer wishes to have an interface look-and-feel that can be adjusted on-the-fly by an end user, your work is multiplied, as you have to deal with several CSS files variants, each of which contains the same values repeated numerous times.
These problems can be solved with the skinnability system built into theRichFaces project and realized fully in RichFaces. Every named skin has some skin-parameters for the definition of a palette and the other parameters of the user interface. By changing just a few parameters, you can alter the appearance of dozens of components in an application in a synchronized fashion without messing up user interface consistency.
The skinnability feature can't completely replace standard CSS and certainly doesn't eliminate its usage. Skinnability is a high-level extension of standard CSS, which can be used together with regular CSS declarations. You can also refer to skin parameters in CSS via JSF Expression Language. You have the complete ability to synchronize the appearance of all the elements in your pages.
RichFaces skinnability is designed for mixed usage with:
Skin parameters defined in the RichFaces framework
Predefined CSS classes for components
User style classes
The color scheme of the component can be applied to its elements using any of three style classes:
A default style class inserted into the framework
This contains style parameters linked to some constants from a skin. It is defined for every component and specifies a default representation level. Thus, an application interface could be modified by changing the values of skin parameters.
A style class of skin extension
This class name is defined for every component element and inserted into the framework to allow defining a class with the same name into its CSS files. Hence, the appearance of all components that use this class is extended.
User style class
It's possible to use one of the styleClass parameters for component elements and define your own class in it. As a result, the appearance of one particular component is changed according to a CSS style parameter specified in the class.
Here is a simple panel component:
Example:
<rich:panel>
...
</rich:panel>
The code generates a panel component on a page, which consists of two elements: a wrapper <div> element and a <div> element for the panel body with the particular style properties. The wrapper <div> element looks like:
Example:
<div class="dr-pnl rich-panel">
...
</div>
dr-pnl is a CSS class specified in the framework via skin parameters:
background-color is defined with generalBackgroundColor
border-color is defined with panelBorderColor
It's possible to change all colors for all panels on all pages by changing these skin parameters.
However, if a <rich-panel> class is specified somewhere on the page, its parameters are also acquired by all panels on this page.
A developer may also change the style properties for a particular panel. The following definition:
Example:
<rich:panel styleClass="customClass">
...
</rich:panel>
could add some style properties from customClass to one particular panel, as a result we get three styles:
Example:
<div class="dr_pnl rich-panel customClass">
...
</div>
RichFaces provides eight predefined skin parameters (skins) at the simplest level of common customization:
To plug one in, it's necessary to specify a skin name in the "org.richfaces.SKIN" context-param.
Here is an example of a table with values for one of the main skins, "blueSky".
Table 5.1. Colors
Parameter name | Default value |
---|---|
headerBackgroundColor | #BED6F8 |
headerGradientColor | #F2F7FF |
headTextColor | #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 |
Table 5.2. Fonts
Parameter name | Default value |
---|---|
headerSizeFont | 11px |
headerFamilyFont | Arial, Verdana, sans-serif |
tabSizeFont | 11px |
tabFamilyFont | Arial, Verdana, sans-serif |
buttonSizeFont | 11px |
buttonFamilyFont | Arial, Verdana, sans-serif |
tableBackgroundColor | #FFFFFF |
tableFooterBackgroundColor | #cccccc |
tableSubfooterBackgroundColor | #f1f1f1 |
tableBorderColor | #C0C0C0 |
Skin "plain" was added from 3.0.2 version. It doesn't have any parameters. It's necessary for embedding RichFaces components into existing projecst which have its own styles.
To get detailed information on particular parameter possibilities, see the chapter where each component has skin parameters described corresponding to its elements.
In order to create your own skin whose constants are used by style classes at the first level, do the following:
Create a file whose name follows the format of a skin file and place it into the ClassPath for the application. (Any skin file follows the naming format, <name.skin.properties>.)
Add a skin definition context-param element to the application's web.xml file:
Example:
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>name</param-value>
</context-param>
In the skins file, specify your own values for skin constants as described in the table.
RichFaces gives an opportunity to incorporate skinnability into UI design. With this framework you can easily use named skin parameters in properties files to control the appearance of the skins that are applied consistently to a whole set of components. You can look at examples of predefined skins at:
http://livedemo.exadel.com/richfaces-demo/ |
You may simply control the look-and-feel of your application by using the skinnability service of the RichFaces framework. With the means of this service you can define the same style for rendering standard JSF components and custom JSF components built with the help of RichFaces.
To find out more on skinnability possibilities, follow these steps:
<render-kit>
<render-kit-id>NEW_SKIN</render-kit-id>
<render-kit-class>
org.ajax4jsf.framework.renderer.ChameleonRenderKitImpl
</render-kit-class>
</render-kit>
<renderer>
<component-family>javax.faces.Command</component-family>
<renderer-type>javax.faces.Link</renderer-type>
<renderer-class>
newskin.HtmlCommandLinkRenderer
</renderer-class>
</renderer>
newskin.skin.properties
.
Extra information on custom renderers creation can be found at:
http://java.sun.com/javaee/javaserverfaces/reference/docs/index.html |