Fix your Richfaces AJAX (performance) problems

Richfaces supports AJAX functionality in a lot of it components. However a servlet filter is needed for correct functioning of partial page refreshes.

In this post we’ll show you how to add this filter and at the same time optimize Richfaces performance.

The following Richfaces filter is defined in the web.xml:

<filter>
        <filter-name>richfaces</filter-name>
        <display-name>RichFaces Filter</display-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
        <init-param>
			<param-name>forceparser</param-name>
			<param-value>false</param-value>
		</init-param>
    </filter>

What this filter does, is to ‘tidy’ all HTML HTTP Responses so that they are valid XHTML (thus XML compliant). This is needed as dynamic DOM updates in the browser need correct XML.

If you don’t define this filter, it is possible that you’ll not see your AJAX update being rendered on the screen, although you’ll see the html response coming back in eg. Firebug

Of course, parsing HTML incurs a performance overhead.
This can be minimized by setting the forceparser setting to false. In that case only AJAX responses will be ‘tidied’. In the other case all JSF responses are ‘tidied’. That is because the filter is mapped on the Faces servlet:

<filter-mapping>
        <filter-name>richfaces</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

Richfaces has a few parsers onboard. The default one is based on Tidy, but it is quite slow. The Neko parser is faster and can be used by setting the following context-param’s:
<context-param>
        <param-name>org.ajax4jsf.xmlparser.ORDER</param-name>
        <param-value>NEKO</param-value>
    </context-param>

    <context-param>
        <param-name>org.ajax4jsf.xmlparser.NEKO</param-name>
        <param-value>.*\..*</param-value>
    </context-param>

Here we say we only use the NEKO filter and it should be applied to all URLs (.)
There is more configuration possible, like using NONE for some pages that don’t need HTML correction to further speedup things if needed.

Example can be found at: http://jboss.com/index.html?module=bb&op=viewtopic&t=116231

5 thoughts on “Fix your Richfaces AJAX (performance) problems”

  1. Hi,
    with spring webflow 2.0 the init param ‘forceparam’ should be true, in order to get the partly page refresh working.

    richfaces
    RichFaces Filter
    org.ajax4jsf.Filter

    forceparser
    true

    And the filter should be applied to the MVC Dispatcher Servlet as well.

    I do not use the NEKO parser.

    grtz
    Frederik

  2. We are using RichFaces and are having performance problems and I have a sneaking suspicion that is due to the fact that my Views are not being cached and are being rebuilt on every request.

    What is the easy way to determine if a view is being cached or not cached and determining what the cause of this would be.

    Whatty

  3. hi,

    I apply the configuration you mentioned above. but I got the error below;

    java.lang.NoClassDefFoundError: org/cyberneko/html/filters/ElementRemover

    do you know how can I overcome this problem?

    kind regards,
    Mustafa

  4. Which Richfaces version are you using? The error indicates that the Neko parser is not available in any of your packaged jars. I believe that in the past the Neko filter was included in the richfaces jar itself. I haven’t checked with the latest versions…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.