Max Katz heads Developer Relations for Appery.io, 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: mkblog.exadel.com and Twitter: @maxkatz. Max is a DZone MVB and is not an employee of DZone and has posted 83 posts at DZone. You can read more from them at their website. View Full User Profile

JBoss RichFaces with Spring

02.16.2009
| 188542 views |
  • submit to reddit
View Beans
Let's start with BarBean class:
package bar.view;

import java.util.List;
import bar.service.Service;

public class BarBean {

private OrderService orderService;

@SuppressWarnings("unchecked")
public List getOrders () {
return orderService.getOrders();
}
public int getRowCount () {
return orderService.getOrders().size();
}
public Service getOrderService() {
return orderService;
}
public void setOrderService(Service orderService) {
this.orderService = orderService;
}
}


getOrders method calls the service class and returns all the orders.

getRowCount will return the number of rows. It's used not to render rich:dataTable component if the order is empty.

The other question is how do we get an instance of orderService into this bean. First, let's register this bean in Spring configuration file:

<bean id="barBean" class="bar.view.BarBean" scope="request"/>


We still have to deal with userService and how it gets initialized inside the bean. This is were Spring DI   comes into play. We want Spring to inject userService into the barBean, when barBean is being created. To accomplish that, we modify the configuration to look like this:

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


service is the name under which we registered the Service class.

We are ready to look at WizardBean class. This class is the back end for the wizard.

package bar.view;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.hibernate.validator.Email;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.Pattern;
import bar.service.Service;
import bar.utils.JSFUtils;

public class WizardBean implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@NotEmpty(message = "Name must not be empty")
@Pattern(regex = ".*[^\\s].*", message = "This string contain only spaces")
private String name;

@NotEmpty
@Email(message = "Invalid email format")
private String email;

private String drink;
private String comments;
private String drinkCategorySelected;

private Service orderService;

private String startPage;

private List<SelectItem> drinkCategory;
private List<SelectItem> drinkList;

// init all drinks
private static final String [] CATEGORY = {"Brandy", "Rum", "Tequila", "Whiskey", "Wine", "Beer"};
private static final String [] BRANDY = {"XO", "VSOP", "VS"};
private static final String [] RUM = {"Medium", "Full-bodied", "Aromatic"};
private static final String [] TEQUILA = {"Reposado", "Añejo", "Blanco"};
private static final String [] WHISKEY = {"Malt", "Grain", "Single Malt", };
private static final String [] WINE = {"Red", "White", "Pink"};
private static final String [] BEER = {"Ales", "Lager", "Specialty Beer", };

public void changeDrink(ValueChangeEvent event) {
String newValue = (String) event.getNewValue();

if (newValue.equals("Brandy")) {drinkList = JSFUtils.createList(BRANDY); drink=BRANDY[0];}
else if (newValue.equals("Rum")) {drinkList = JSFUtils.createList(RUM); drink=RUM[0];}
else if (newValue.equals("Tequila")) {drinkList = JSFUtils.createList(TEQUILA);drink=TEQUILA[0];}
else if (newValue.equals("Whiskey")) {drinkList = JSFUtils.createList(WHISKEY);drink=WHISKEY[0];}
else if (newValue.equals("Wine")) {drinkList = JSFUtils.createList(WINE);drink=WINE[0];}
else if (newValue.equals("Beer")) {drinkList = JSFUtils.createList(BEER);drink=BEER[0];}
}

@PostConstruct
public void create() {
drinkCategorySelected = CATEGORY[0];
drinkCategory = JSFUtils.createList(CATEGORY);
drinkList = JSFUtils.createList(BRANDY);
drink = BRANDY[0];
}
public void save() {
orderService.addOrder(name, email, drink, comments);
this.startPage = "/page1.xhtml";

FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
.remove("wizardBean");
}

}



Don't forget to generate setters and getters for each property. I have omitted them in order to save space.

It looks like a lot more happening in this class, but in reality not that much. First, there are propeties such as email, name, drink and comments which correspond to different wizard screens. If you notice, they also correspond to Order model class.

email and name properties are annotated with Hibernate Validations annotations. We will come back to them when we build the pages.

The large section preceded by // init all drinks comment is how drinks list is created. It's not the most pretty way, but it will do for this example.

There is also JSF changeDrink value change listener. It will be invoked each time a new drink category is selected and will load the drinks associated with that category.

The save method saves the new order. The last line in save method removes the bean from request scope. We will come back to it shortly.

In this class as in BarBean, we have a reference to orderService. No where in the class we instantiate the property. You probably guessed, but we are going to use Spring's DI to inject the service into this bean.  Let's register this bean in Spring configuratin file.

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



startPage property is initialized to the first page in the wizard.

orderService is injected with service bean.

One last class is the JSFUtils:

package bar.utils;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

public class JSFUtils {

public static List <SelectItem> createList (String [] data){
ArrayList <SelectItem>list = new ArrayList <SelectItem>();
for (String item : data){
list.add (new SelectItem (item, item));
}
return list;
}
}



This only method in this class creates a list of SelectItem's. We are ready to create the views.
 

AttachmentSize
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.)

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

Thanks taod!

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

I would like to download the code, too.  I've done everything you said to do and can't get it to deploy to JBoss and was wondering if you had some configuration you didn't tell us about.

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

A very good introduction. Can someone provide the password for downloading the project template?

Juan Gonzales replied on Wed, 2009/04/01 - 7:03pm

A very good ...could you please send it by e-mail to me  password

 

 thanks

 

juanjogoa24@gmail.com 

 

 

 

.  

 

 

Max Katz replied on Sat, 2009/04/11 - 11:47am

Link to download project template has been updated.

Davut Uysal replied on Fri, 2009/04/17 - 9:48am

Thanks Max. It was great article!

shantanu banerjee replied on Sat, 2009/04/25 - 1:36am

Hi Max, I am going through your article as we speak. You writing is always so to-the-point. I like it very much. However the zip file is not working. Can you check if you get the opportunity? Shantanu

Donovan Makund replied on Sun, 2009/04/26 - 3:11pm in response to: Max Katz

The zip file cannot be extracted once downloaded.

shantanu banerjee replied on Thu, 2009/04/30 - 1:57am

As Donovan has pointed out, the zip file can not be extracted once downloaded. Can anyone please send the proper template zip to shanroam@yahoo.com. I would be immensely greatful.

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:

  1. Select File/Import/General/Existing Project into Workspase
  2. Click Next
  3. Check 'Select arhcive file' option and browse to downloaded file
  4. Follow the wizards steps

shantanu banerjee replied on Thu, 2009/05/07 - 8:08am

I somehow cannot download the file from the link that Max provided in the last post. Any help would be greatly appreciated.

Max Katz replied on Tue, 2009/05/19 - 6:05pm in response to: shantanu banerjee

I'm not sure why the link is not working. Try this one: http://www.filedropper.com/richfaces-spring

Donovan Makund replied on Sat, 2009/06/06 - 11:48am

Hello Max, I was able to implement a prototype using Richfaces 3.3.1, Spring 2.5.5, JSF 1.2 with Spring managing all the beans. I am now trying to add Facelets 1.1.14 into the mix for template features. Have you tried this combination before?

Max Katz replied on Fri, 2009/06/12 - 11:18am in response to: Donovan Makund

If you look in my template, I used Facelets. So yes, you can (and should) use Facelets.

Mohsin Khan replied on Tue, 2009/06/16 - 2:30pm

Hi i am not able to run the application.Getting following exception javax.faces.FacesException: UIInclude component j_id10 could't include page with path at org.ajax4jsf.taglib.html.facelets.IncludeHandler.applyNextHandler(IncludeHandler.java:83) at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169) at com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314) at com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169) at com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47) at com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java Please help me Mohsin

Martin Haas replied on Mon, 2009/06/22 - 5:50am

None of the links above is working. Another source where I can download the project? The linked project is corrupt and all other links in the comments are broken.

Max Katz replied on Mon, 2009/06/22 - 3:26pm in response to: Mohsin Khan

Is this the full stack trace?

Max Katz replied on Mon, 2009/06/22 - 3:29pm in response to: Martin Haas

Let's try one more time: http://drop.io/pnbhzls

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

java.io.NotSerializableException: bar.service.OrderService
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1113)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1467)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1439)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1382)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1111)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:325)
at java.util.HashMap.writeObject(HashMap.java:1290)
at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:973)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1382)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1111)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1343)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1107)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1343)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1107)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1343)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1107)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1343)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1107)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1343)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1107)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:325)
at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:269)
at javax.faces.render.ResponseStateManager.writeState(ResponseStateManager.java:166)
at org.ajax4jsf.application.AjaxStateManager.writeState(AjaxStateManager.java:294)
at org.ajax4jsf.application.AjaxStateManager.writeState(AjaxStateManager.java:256)
at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:606)
at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1103)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1038)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:145)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:834)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:682)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:566)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:486)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3440)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:815)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1461)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:118)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:267)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:195)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:743)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:873)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)

 

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

Not sure why you get this exception, the server is trying to save/restore the class from somewhere. Try adding implements java.io.Serializable to the class.

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

Comment viewing options

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