Max Katz heads Developer Relations for, a cloud-based mobile app platform. He loves trying out new and cool REST APIs in mobile apps. Max is the author of two books “Practical RichFaces” (Apress 2008, 2011), and is a frequent speaker at developer conferences. You can find out what Max is up to on his blog: and Twitter: @maxkatz. Max is a DZone MVB and is not an employee of DZone and has posted 82 posts at DZone. You can read more from them at their website. View Full User Profile

JBoss RichFaces with Spring

  • submit to reddit

Getting the Project

I have used JBoss Tools to build the project.  You can of course use any tool of your choice. Below I show steps for how to import the project as a JSF project into JBoss Tools. You can also import the project as an existing Eclipse project. It's up to you. If you need to install JBoss Tools, here is a [link]

1. Download this project
2. Import the project into Eclipse
         1. File/Import/Other
         2. Select JSF Project
         3. Point to web.xml file and follow the wizard steps.

Before we continue, let's make sure that the project was deployed correctly and that we can run it. Start the server by right-clicking Tomcat in the Servers view and selecting Run.

Right-click the project and select Run As\Run On Server. You should get this welcome page.

Assembling the Application

Creating the Model

Let's start by creating the model for an order. In a real application, this object would usually be an entity – an object that is saved and retrieved from a database.

The class is called Order and looks like this (note the package):

package bar.model;

public class Order {

private String name;
private String email;
private String drink;
private String comments;

public Order(String name, String email, String drink, String comments) {
super(); = name; = email;
this.drink = drink;
this.comments = comments;

// getter and setters for each property

Don't forget to generate getters and setters for each property. There is nothing interesting about this object.

Creating the Order Service

Next we are going to create a service class that knows how to add a new order. You can place other methods into this service, such as finding and deleting orders.

The OrderService class looks  like this:

package bar.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import bar.model.Order;

public class OrderService {

private List <Order>orders;

public List<Order> getOrders() {
return orders;

public void addOrder (String name, String email, String drink, String comments){
Order order = new Order(name, email, drink, comments);

public void create (){
orders = new ArrayList <Order> ();

A few things to note:

  • This service class will keep the list of all orders.
  • The addOrder method adds a new order to the list.
  • The @PostConstruct annotation marks the create() method to be invoked right after the OrderService class has been created. To be more technically correct, this method is called after all injections have been satisfied. While the class doesn't have dependencies, this method is still invoked and is a good place to initialize any properties.  In our case, we initialize the list object. 

Registering as a Spring Bean

We also need to register the Service bean with Spring. We want Spring to manage this bean. Remember that we will first use XML to configure that. Registration looks like this:

<bean id="service" class="bar.service.OrderService" scope="session" />

id is just the id or the name of this bean.
We are placing this bean in session scope because we are keeping the list of all orders in it.

We used the @PostConstruct annotation to initialized the list property. This is a standard Java 5 annotation which is supported by Spring.
Note: for @PostConstruct, @PreDestory and @Resource annotations to work, the following has to be added to Spring configuration file:


Alternatively , Spring also provides init-method attribute when registering the bean as shown here:

<bean id="orderService" class="bar.service.Service" scope="session"  init-method="create"/>

This attribute points to a custom initialization method to invoke after setting bean properties.

A third option is to implement InitializingBean and DisposableBean interfaces: [].

Next we are going to create two view beans, one for the wizard and one for displaying current orders.

richfaces-spring-wizard.zip11.15 MB
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.)


Gene De Lisa replied on Fri, 2009/07/31 - 9:45am

What exactly are you thinking by posting an 11 MB zip file that contains nothing but library jar files? Are you getting some perverse pleasure by wasting other people's time and bandwidth? Do you know the meaning of the word courtesy? Apparently not.

If you don't want to post your code don't post a zip. It's as simple as that.

Kushan Jayathilake replied on Fri, 2009/10/30 - 7:56am

Hi Max, Great work!..

i have a problem, i have added the validation conditions in managed bean of the JSF page

ie :

@NotEmpty(message = "Country Name must not be empty.")

@Pattern(regex = ".*[^\\s].*", message = "Spaces are not allowed.")

private String countryName;


i have several  <a4j:commandButton> and <a4j:commandLink> in the same page, validation fires each and every time i press a commandLink or Button,   iif that field is empty("countryName" as i specified here)

how can i control the validation to fire for the particular button or a link...?  (i need validation to be fired when only i clicked the save button)

please help me on this...

Max Katz replied on Thu, 2009/11/05 - 8:10pm in response to: Kushan Jayathilake

This article should help:

Kushan Jayathilake replied on Fri, 2009/11/06 - 6:45am

Thx Max, It helped me to solve my problem,

I have another question,

I want to generate dynamic checkboxes depends on a collection, so i have written

<h:dataTable id="functionsGrid" value="# {functionManagedBean.collecOfFunctions}"                                                            var="function"  >
      <h:selectBooleanCheckbox id="#{function.functionName}" value="#{functionManagedBean.checked}"/>
    <h:outputLabel for="" value="outputLabel"></h:outputLabel>


and finally this form contains an <a4j:commandButton> , in the managedBean side i want to get the selected checkboxes....(problem is checkboxes are generating dyamically, so how can assign their values for the different properties in the managedBean..

Thank you very much in advance.. :)

Tee Siong replied on Wed, 2010/04/07 - 5:40am

I have problem where the Next button could't go to next page . I am not using Facelets .But i use jsf . I have configure my faces-config.xml file properly .What is the possible root cause ? Can you share your working example ? email to me . . Thanks

Thomas Strecker replied on Thu, 2010/05/27 - 3:36am in response to: Mohsin Khan

I encountered the same problem, but only after switching to Spring Annotations and Component Scan. The reason is rather simple: the WizardBean now no longer receives the value for the startPage (which was set to "/page1.xhtml" in the spring application context before) and now the renderer ends up in a endless loop.

So if you want to use the annotation based version, manually set the parameter in the bean.

Mohammad Shariq replied on Tue, 2010/06/15 - 2:50pm

Please email me the correct file. I am not able to open or download the source code. The one file from which I downloaded contained only jars and no significant java files or any other code. PLease email me as early as possible at

Mehdi Ben Haj Abbes replied on Mon, 2010/07/19 - 2:18am in response to: stu bilton

Hi, For the NotSerializableException, I tried to implement Serializable but didn’t work and I had a Spring Exception : my backing bean has a bean injected through spring. The solution was to look for the bean through getBean(“beanName”) rather than injecting it.

Fabio Malheiro replied on Tue, 2010/09/28 - 8:26am

This was supposed to be a good tutorial right?

I was trying to implement your example... but I'm a little bit confused. Sometimes you "talk" about a bean orderService, and Service ... but where the fuck is the Service class?

Andres Botero replied on Wed, 2010/11/17 - 12:02pm

When i inyect a dependency into a bean it's null. For Example:

<bean id="wizardBean" class="bar.view.WizardBean" scope="request">
   <property name="orderService" >
    <ref bean="service" />

In that case the orderService is null, colud you tell me why is that?

A S M Russel replied on Fri, 2011/09/30 - 1:10am

After Migrating from XML configuration to Annotation based configurated i am getting the following error.

at com.sun.facelets.el.VariableMapperWrapper.resolveVariable(

the error trace is too long thats why i am pasting it here.

I create the spring3 template project with STS and follow your instruction.

Its working well for xml configuration.But getting error when i am going to annotation based configuration 

Scott Duke replied on Tue, 2011/10/25 - 10:46pm

I came across this example and ran across a few problems and wanted to post the solutions once the project is imported. The project is using Tomcat 6.0 but I was using Tomcat 7.0. There were about 3 places in the project preferences I had to change from Tomcat 6.0 to Tomcat 7.0. Here were 3 of the places. Just look through the preferences. 1. preferences -> Java -> Compiler 2. preferences -> Server -> Runtime Environment 3. preferences -> Java Build Path Because I was using jdk 1.7.0, I had to update the facets on the project at: preferences -> Project Facets and change the Java to 1.7

Max Katz replied on Thu, 2011/10/27 - 4:48pm in response to: Scott Duke

Thanks for posting.

Nagy Andika replied on Fri, 2011/11/04 - 5:13am

Hi! Nice article. This is what I'm looking for but in an upgraded version. I wonder if you tried it with RichFaces4 and Spring3.0. Would it be as seamless as here? Andika

Comment viewing options

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