An Introduction To JBoss RichFaces
Application
Creating a new project
- Select File/New/JSF Project
- For the project name, enter “richfaces-start”
- For JSF environment, select JSF 1.2, Facelets, RichFaces
- For a template, select one based on the Tomcat version you are using
- Click Finish to create the project
Creating the model class
As we are dealing with users, we are going to create a user model class.
In the JavaSource directory, create an example.model.User Java class and notice the package name:
package example.model;
public class User {
@Override
public String toString() {
return name + " " + email;
}
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
}
The class is very simple. Our application will list one or more of these users. Next, we are going to create a managed bean. The managed bean will basically be a model for the user interface we will be building shortly.
Creating the managed bean
In JavaSource, create an example.beans.UserBean Java class. Again, pay attention to the package name:
package example.beans;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import example.model.User;
public class UserBean {
private List <User>users;
public List <User>getUsers() {
return users;
}
@PostConstruct
public void init () {
users = new ArrayList <User>();
users.add (new User("Joe", "joe@gmail.com"));
users.add (new User("Charley", "charley@ymail.com"));
users.add (new User("John", "john@hotmail.com"));
users.add (new User("Greg", "greg@gmail.com"));
users.add (new User("Prescila", "prescila@aol.com"));
}
}
This class is also simple. A list of five users is created and place inside an ArrayList. The @PostConstruct annotation is useful for initializing properties. It guarantees that the annotated method will only be called once when the bean is created.
To make this bean a managed bean, we need to register it in a JSF configuration file.
- Open WebContent/WEB-INF/face-config.xml
- Switch to the Tree view
- Select Managed Beans and click Add...
- Keep the scope request
- For Class, enter the bean’s full Java class name, example.beans.UserBean
- For Name, enter or keep userBean - Click Finish
We are ready to create the user interface.
Managed bean registration looks
like this:
<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>example.beans.UserBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Brian ray replied on Tue, 2008/08/26 - 2:22pm
Cant Download richfaces-template.zip invalid file.
Need that to try to make out the details that the writer elected to omit. Dont show me images that are not accurate. Dont make me down load plug ins that may or may-nit work as described.
Max Katz replied on Tue, 2008/08/26 - 3:33pm
I just clicked on the template file and was able to download. What kind of message are you getting?
Please tell me what details are missing.
Please provide more details. If you are talking about the colors, I will update the article so you can try the same skin I'm using. Sorry about that.
Brian ray replied on Tue, 2008/08/26 - 4:53pm
in response to:
Brian ray
Once I get the file, Upon trying to unzip it I recieve "Cannot open file: it does not appear to be a valid archive. If you downloaded this file, try downloading the file again." I have tried to get it several times.
The image problem was on page 4 where it showed the table with the "edit" buttons already there. It appears to have been corrected or I was looking at the wrong image. While going through this I was trying to figure out how those buttons got there. But then on the next page it explained how to add them into their own col.
The part I mentioned about down loading was because I had no way to look at your "faces-config.xml" cause you let the plug-in build it for you. This is also the part where I considered it an ommison to not show me your config file. Colors are pretty self explanitory with the skins that wasn't a problem.
Max Katz replied on Tue, 2008/08/26 - 5:38pm
in response to:
Brian ray
I don't know why you get that error message. Slightly different setup steps, but you can try using a template in this posting: http://mkblog.exadel.com/?p=119
I submitted all other changes (image, managed bean configuration, skin setup). The changes should be up shortly.
Brian ray replied on Wed, 2008/08/27 - 9:13am
in response to:
Max Katz
End-of-central-directory signature not found. Either this file is not a Zip file, or it constitutes one disk of a multi-part Zip file.
"
I will try again from a different machine to see if it is just black magik that thwarts my efforts.
Max Katz replied on Wed, 2008/08/27 - 3:48pm
in response to:
Brian ray
Gary Marsh replied on Fri, 2008/08/29 - 4:15pm
Hi;
I also tried to download and unzip your articles richfaces-template.zip file and was unable to open it. It turns out to the content is binary an un-decipherable. Your alternate zip file is good.
Regards,
glmarsh
Angelo Quaglia replied on Sat, 2008/08/30 - 5:04am
Small typo on page 3:
To make this bean a managed bean, we need to register it in a JSF configuration file.
- Keep the scope request
- For Class, enter the bean’s full Java class name, sample.beans.UserBean -> example.beans.UserBean
Angelo Quaglia replied on Sat, 2008/08/30 - 12:41pm
When clicking on Save I get:
Apache Tomcat/6.0.18 - Error report
HTTP Status 500 -type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: /user.xhtml @69,30 actionListener="#{userBean.save}": java.lang.NullPointerException javax.faces.webapp.FacesServlet.service(FacesServlet.java:277) org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:147) org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:276) org.ajax4jsf.Filter.doFilter(Filter.java:175)root cause
javax.faces.el.EvaluationException: /user.xhtml @69,30 actionListener="#{userBean.save}": java.lang.NullPointerException com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73) javax.faces.component.UICommand.broadcast(UICommand.java:380) org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55) org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:316) org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:291) org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:248) org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:461) com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:265) org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:147) org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:276) org.ajax4jsf.Filter.doFilter(Filter.java:175)root cause
Angelo Quaglia replied on Sat, 2008/08/30 - 12:43pm
I had to initialize rowsToUpdate in the init() method to make it work.
@PostConstruct
public void init () {
users = new ArrayList <User>();
users.add (new User("Joe", "joe@gmail.com"));
users.add (new User("Charley", "charley@ymail.com"));
users.add (new User("John", "john@hotmail.com"));
users.add (new User("Greg", "greg@gmail.com"));
users.add (new User("Prescila", "prescila@aol.com"));
rowsToUpdate = new HashSet();
}
Did I make some mistake in the markup of the page?
Max Katz replied on Mon, 2008/09/01 - 12:22pm
in response to:
Angelo Quaglia
Small typo on page 3:
To make this bean a managed bean, we need to register it in a JSF configuration file.
- Keep the scope request
- For Class, enter the bean’s full Java class name, sample.beans.UserBean -> example.beans.UserBean
[/quote]
Thanks, I'll update the code.
Max Katz replied on Mon, 2008/09/01 - 12:25pm
in response to:
Angelo Quaglia
Brian ray replied on Fri, 2008/09/12 - 9:15am
Max,
I forgot to mention earlier. Thanks for putting up the tutorial. It's easy for me to complain about the things that are wrong. But in doing so I must also show appreceation for your efforts. So Thanks!!
Angelo Quaglia replied on Fri, 2008/09/12 - 10:48am
Oh, yes! I agree.
Many thanks for this interesting and to-the-point tutorial.
Alan kubrick replied on Sat, 2008/09/20 - 12:22am
hi Max,
I bought the pdf version of your book "Practical Richfaces", before seing this example. I found it cause I was looking for a zip file mentioned in the book : rcbook-templates.zip ; after having been told by apress that the file is not available on their site, thank to google, I found it here.
This tutorial is very clear and helpful and I am using it as a start to do examples in the book. I am unable to make the the example about the worldwonders work: no way of seeing the dataOrderedList update after a change of the order of two element in the orderingList, and a submit. Is there anything missing in the submit button?
Is there any place where I can download the source code associated with the book? It would be great if it is a war file, with all examples mentioned in the book. Thanks
John Smith replied on Thu, 2008/09/25 - 1:22pm
I followed these steps exactly, with the exception of using jboss-portal bundle 2.6.6. However, when I deploy the application I get an exception:
13:52:02,584 ERROR [[/example]] Exception sending context initialized event to listener instance of class org.jboss.web.jsf.integration.config.JBossJSFConfigureListener
java.lang.ClassCastException: com.sun.faces.config.WebConfiguration
at com.sun.faces.config.WebConfiguration.getInstance(WebConfiguration.java:154)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:144)
I have tried reinstalling everything, an making sure I have the libs, and a few config options, but no luck.
Any ideas?
Thanks.
Max Katz replied on Thu, 2008/09/25 - 7:59pm
JBoss AS already ships with JSF runtime, at the same time, my template also has JSF runtime as I was running the application in Tomcat. The exception indicates a classloading conflict with existing libraries.
There is a detailed article on using RichFaces in a portal:
http://www.infoq.com/articles/jsf-ajax-seam-portlets-pt-1
http://www.infoq.com/articles/jsf-ajax-seam-portlets-pt-2
http://www.infoq.com/articles/jsf-ajax-seam-portlets-pt-3
Hope this helps.
Laurent Lasalle replied on Tue, 2008/09/30 - 11:41am
in response to:
Max Katz
Hi ! don't see the answer for the same question :
I bought your book's in the ebook preview format.
Where to find the rcbook-templates.zip ?? (and others stuff you have around that book ?!)
Thanks
Laurent Lasalle replied on Tue, 2008/09/30 - 11:56am
in response to:
Laurent Lasalle
Sorry for not reading and looking more closely before posting here : I think we can just use this richfaces-template.zip as a remplacement for the infamous rcbook-templates.zip.
Well thanks Max and when you will got some time do put all things regarding this book on a repository somewhere so book readers will know where to search looking !
Cheers
Max Katz replied on Tue, 2008/09/30 - 12:01pm
Laurent Lasalle replied on Wed, 2008/10/01 - 5:33am
in response to:
Max Katz
max just one question please :
Do you got something introducing how to generate and use components from a java source code.
I see much Richfaces component got some Java manipulation section but no clue on how to use that ?
I've been looking around and nothing clear was found.
Do you got some tips or useful links ? Why I'm looking for this is for case where part of the UI will be mostly driven by what is stored in the database.
depending on this and this and that build me my UI like this.
Not always easily done because you don't have the infos before you must do it on runtime !!
In that case knowing how to build richfaces component from a java class will be pretty nice.
Thanks in advance if you got some hints.
Max Katz replied on Wed, 2008/10/01 - 7:01am
Frank Brown replied on Thu, 2008/10/16 - 2:21pm
Any thoughts on this?
Thanks
Frank
Max Katz replied on Fri, 2008/10/17 - 8:51am
Did you add the RichFaces template to JBoss Tools? If not, the steps are under Project Templates section.
Alan kubrick replied on Tue, 2008/10/21 - 9:35am
Hi Max,
I am experiencing the same problem :
Environment :
- richfaces 3.2.2
- facelets
- Mojara JSF (Sun RI)
Context :
- an orderinglist in a modal panel
- a4j:commandButton, oncomplete of which the modal panel is opened
As a result :
- the modalpanel is opened and the orderinglist displayed inside it
- BUT I am unable to select a row of the list by mouseClicking on it (there is no javascript error)
- even though using the keyboard to navigate from one row to another works well.
- when I close the modalPanel, and click on the same a4j:commandButton the SECOND TIME, everything work fine:
I can select a row by mouseclick as it should.
Observations :
- when I replace the modalPanel by simple richpanel, no such behaviour happens
- No javascript error is showned
- this behaviour is the same for internet explorer or firefox.
Any idea? Is there a way of, say, "activating" an orderingList?
Thanks a lot
Alan kubrick replied on Tue, 2008/10/21 - 9:38am
in response to:
Max Katz
Sorry, I mean, I am experiencing the following problem :
Unability to select-click on a row of an orderinglist inside a modalPanel.
Max Katz replied on Tue, 2008/10/21 - 10:35am
Hi Alan,
I need to look at the source code (JSF page, bean) as this is outside the scope of the article. It's very difficult to say what could be the problem. Send me an email, I willl take a look at it.
Max
Christian Lehmann replied on Sun, 2008/10/26 - 11:06am
Hello,
I used your example with success thanks. I was disapointed though, when I tried to use it with additionnal richfaces features like sortable columns.
When I modified the userlist datatable with rich:column instead of h:column, I could no get the table to be re-rendered after a modal dialog save. How could I achieve this obvious result ?
<rich:column sortBy="#{user.name}" sortOrder="ASCENDING"><f:facet name="header">
<h:outputText value="User Name"/>
</f:facet>
<h:outputText id="name" value="#{user.name}"/>
</rich:column>
<rich:column sortBy="#{user.email}">
<f:facet name="header">
<h:outputText value="User Email"/>
</f:facet>
<h:outputText id="email" value="#{user.email}" />
</rich:column>
Max Katz replied on Mon, 2008/10/27 - 10:12am
Try this:
<rich:column sortBy="#{user.name}" sortBy="#{user.name}">
<f:facet name="header">
<h:outputText value="User Name"/>
</f:facet>
<h:outputText id="name" value="#{user.name}"/>
</rich:column>
Christian Lehmann replied on Mon, 2008/10/27 - 12:27pm
in response to:
Max Katz
<rich:column sortBy="#{user.name}" sortBy="#{user.name}">
[/quote]
Thanks for your answer, but there is probably a mistake in the above line as the attribute "sortBy" is already specified for element "rich:column".