JBoss RichFaces with Spring
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();
this.name = name;
this.email = 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);
orders.add(order);
}
@PostConstruct
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:
<context:annotation-config/>
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: [http://static.springframework.org/spring/docs/2.5.x/reference/beans.html].
Next we are going to create two view beans, one for the wizard and one for displaying current orders.
| Attachment | Size |
|---|---|
| richfaces-spring-wizard.zip | 11.15 MB |
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Aleksandar Vidakovic replied on Mon, 2009/02/16 - 6:04pm
Salut Max,
interesting article. I've been experimenting with Richfaces and Spring for a while and would be extremely interested in the SpringWebflow integration...
Cheers,
Aleks
Frederic Chopard replied on Tue, 2009/02/17 - 9:57am
Hello Max,
I'm trying to build my richbar application but when I click on the next button of the first wizard page, nothing happens. So I'd like to check your project but I can't find any link in your article. Where can I download your project files ?
Thanks !
Wis
Nitin Bharti replied on Tue, 2009/02/17 - 10:19am
in response to:
Frederic Chopard
Hi Wis,
I believe you can download the project template from here -- http://drop.io/usoalrd
I will add this to the main article as well. Sorry about that.
Thanks,
Nitin
Gene De Lisa replied on Wed, 2009/02/25 - 9:08am
"In the “real” world, you would probably use Maven 2, but, to keep things simple and concentrate on RichFaces and Spring, we are going to use a ready-made project."
maven projects are "ready-made" and they don't have the disadvantages of the approach you have taken.
Other than that this is nothing more a mediocre do this do that tutorial.
Omar Palomino S... replied on Sun, 2009/03/08 - 10:28pm
in response to:
Aleksandar Vidakovic
Hi Max and Aleksandar,
I'm also interesting in Spring Webflow integration [navigation rules are cumbersome :s]. I have a couple of implementations with both RichFaces, Spring and Jpa, and it's fun and easy to develop this way. Webflow would be a nice addition to this project acthitecture.
Tao Dong replied on Fri, 2009/03/13 - 2:34pm
Excellent Article. In case someone want to try it through Maven. Hope the following dependecy setting will save you some time.
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>3.3.0.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-api</artifactId>
<version>3.3.0.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
<version>3.3.0.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.richfaces.samples</groupId>
<artifactId>laguna</artifactId>
<version>3.3.0.GA</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<!-- JBoss RichFaces Repository -->
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
<id>repository.jboss.com</id>
<name>Jboss Repository for Maven</name>
<url>
http://repository.jboss.com/maven2/
</url>
<layout>default</layout>
</repository>
Max Katz replied on Mon, 2009/03/16 - 10:53am
in response to:
Tao Dong
ben salem replied on Tue, 2009/03/17 - 6:44pm
First, thanks for the great work :)
but
<cite> (Note - The Project Template for this article can be downloaded from http://drop.io/usoalrd) </cite>
what is the pass for download ?
thanks in advance!
Anderson Santos replied on Wed, 2009/03/18 - 9:28am
Hi Buddy,
This article is just great, I have been looking for a steb by step like this one, for at least a month.
I just couldn't download the template for this project, could you please send it by e-mail to me, that link where you say to click for download is broken and the one in the first page os this article, I just couldn't use...
thanks,
Anderson
ander.dev@gmail.com
Jean Chastain replied on Wed, 2009/03/18 - 4:27pm
sam set replied on Sat, 2009/03/21 - 12:01pm
in response to:
Jean Chastain
hi, it's been quite some time i am trying to make richfaces 3.3 work with spring 2.0 webflow. Can somebody tell me how to download the source code. I visited http://drop.io/usoalrd, it's asking for password. What am i suppose to enter there ?
or, can you send me through email ?
Appreciate your help!
Aseem Monga replied on Tue, 2009/03/31 - 7:59am
Juan Gonzales replied on Wed, 2009/04/01 - 7:03pm
thanks
juanjogoa24@gmail.com
.
Max Katz replied on Sat, 2009/04/11 - 11:47am
Davut Uysal replied on Fri, 2009/04/17 - 9:48am
shantanu banerjee replied on Sat, 2009/04/25 - 1:36am
Donovan Makund replied on Sun, 2009/04/26 - 3:11pm
in response to:
Max Katz
shantanu banerjee replied on Thu, 2009/04/30 - 1:57am
Max Katz replied on Tue, 2009/05/05 - 1:45pm
Let's try this file. Here is what you need to do:
Download the template.
In Eclipse:
shantanu banerjee replied on Thu, 2009/05/07 - 8:08am
Max Katz replied on Tue, 2009/05/19 - 6:05pm
in response to:
shantanu banerjee
Donovan Makund replied on Sat, 2009/06/06 - 11:48am
Max Katz replied on Fri, 2009/06/12 - 11:18am
in response to:
Donovan Makund
Mohsin Khan replied on Tue, 2009/06/16 - 2:30pm
Martin Haas replied on Mon, 2009/06/22 - 5:50am
Max Katz replied on Mon, 2009/06/22 - 3:26pm
in response to:
Mohsin Khan
Max Katz replied on Mon, 2009/06/22 - 3:29pm
in response to:
Martin Haas
stu bilton replied on Thu, 2009/07/16 - 10:26am
Hi Max,
Firstly thanks for this article it's good to have a straigthforward example of using these frameworks together.
I do have a problem running this on a was 6.1 instance. (However I don't think this is the issue here). I've placed the code into an existing webb app project to prove the spring framework can run in an existing richfaces / facelets implementation
My stack trace on requesting http://localhost:9080/MyProjectName/start.jsf is as follows
Do you have any idea why I'm getting a NotSerializeableException thrown when trying to instantiate this bean? (presumeably through Spring DI ?(I'm a newbie when it comes to jsf and spring))
Thanks
Stuart
Max Katz replied on Thu, 2009/07/16 - 10:47am
stu bilton replied on Thu, 2009/07/16 - 11:00am
in response to:
Max Katz
Thanks Max.
I thought that might (and did) do the trick. As a newbie just wondered if the stack trace might shed any light on why the exception was throwing.
Keep the articles coming . Thanks again
Stuart