Starting with JSF, JBoss RichFaces, AJAX, and Tomcat

This article will show you how easy it is to setup the JBoss Tools (including RichFaces support) with Eclipse 3.5. I’ll deploy the finished app to Apache’s Tomcat server. I will also make use of the built-in AJAX support in RichFaces as well. Some experience with JSF (JavaServer Faces) would be helpful but not necessary but you should be familiar with the basics of using Eclipse.


If you want to see what all you can do with RichFaces take a look at the live demo here.

To get started you will need Eclipse 3.5 (Galileo) in particular the Eclipse IDE for Java EE Developers. Once you have Eclipse installed head on over to the JBoss Tools website and grab the latest development milestone release of JBoss Tools 3.1 (as of this writing it was 3.1.0.M2). Download that zip file to your computer. You will probably also want Apache Maven installed and configured as well as Apache Tomcat downloaded and extracted somewhere.

Start Eclipse 3.5 now and go to the menu and select: Help -> Install New Software…

When the dialog pops up we are going to first add the BIRT dependency. Press the “Add” button and enter the following:

Adding the BIRT update site

On the next window select at a minimum the bottom check box but you can also check the three check boxes like I did.

Selecting the BIRT check boxes

This took me approx. 20 minutes to install if you choose all three check boxes or about 5 minutes is you just picked the bottom check box. Restart the Eclipse IDE once it’s installed. After it has restarted we are ready to add the JBoss Tools package we downloaded earlier. Go back into the Eclipse menu and select: Help -> Install New Software…

Once again press the “Add” button only this time select the “Archive” button when the dialog comes up and navigate to that downloaded file:

Installing JBoss Tools

Select the check boxes and let it install. This will take approx. 5 minutes. Restart the Eclipse IDE when its done installing.

When Eclipse starts back up change the perspective to “Web Development”.

Setting the perspective

You are now ready to create your first project!

From the Eclipse menu select: File -> New -> JSF Project

Creating the JSF Project

Naming the project

If you haven’t configured Tomcat 6 you will need can do this now.

Setting up Tomcat for the runtime

Selecting Tomcat 6

Locating the Tomcat installation folder

Once that is done hit the “Finish” button.

Your structure should now look like this:

Project structure

Right-click on the “WebContent/WEB-INF” folder and select: New -> JSP File. Name the new file “index” then select “JSFBasePage” from the drop down. When that is done press “Next”. Now select the top two check boxes and hit “Finish”.

Creating the JSP page

Selecting the tag libraries

Very cool so far but you might notice we have a little problem.

Problem!

We can fix that problem easily by grabbing a couple files and putting those into the lib folder. First, go to this address:

https://javaserverfaces.dev.java.net/download.html

Grab the latest 1.2 release (in my case it was 1.2_13 binary) and download it and extract the contents to your hard drive. Now in Eclipse click on the “Package Explorer” tab and right-click on the “WebContent/WEB-INF/lib” folder and select “Import”. Make sure in the dialog to select “General” and then “File System”.

Selecting the import source

Browse to those files you extracted a minute ago and grab the following two files from the “lib” folder:

Selecting the required jar files

The error should go away at this point. Sometimes I’ve had to close the index.jsp file and re-open it though for that to happen.

Now lets add the RichFaces support files. Right-click on the “MyRFProject” and select: JBoss Tools -> Add Custom Capabilities. Then select the following:

Adding RichFaces support

Press “Finish” and your done adding RichFaces support. The JBoss Tool plug-in will handle all the configuration updates required.

Now drag over a JSF Form tag to the index.jsp page between the “view” tags.

Adding a form tag

Your code should look like this:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
	<head>
		<title></title>
	</head>
	<body>
		<f:view>
			<h:form>

			</h:form>
		</f:view>
	</body>
</html>

Now from the JBoss RichFaces Palette drag and drop the “Panel” component in between the “Form” tags like this:

<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
	<head>
		<title></title>
	</head>
	<body>
		<f:view>
			<h:form>
				<rich:panel>

				</rich:panel>
			</h:form>
		</f:view>
	</body>
</html>

Adding the Panel component

Now we will add the rest:

<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
	<head>
		<title></title>
	</head>
	<body>
		<f:view>
			<h:form>
				<rich:panel>
					<h:inputText value="#{ajaxbean.value}" size="30">
						<a4j:commandButton value="Press Me" reRender="ajaxOutput" />
					</h:inputText>
					<br /><br />
					<h:outputText id="ajaxOutput" value="#{ajaxbean.value}" />
				</rich:panel>
			</h:form>
		</f:view>
	</body>
</html>

Lets add a session bean now to hold the “value”. Right-click on the “JavaSource” folder and choose: New -> Class

Modify the settings as follows:

Creating the ajaxbean

Here is the Java code for the bean:

package com.giantflyingsaucer;

public class ajaxbean
{
	public String value;	

	public ajaxbean()
	{
	}

	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		this.value = value;
	}
}

Double click on the “faces-config.xml” and then click on the “Tree” tab at the bottom. Now click on “Managed Beans” and then press the “Add” button and use the settings below:

New managed bean step 1

New managed bean step 2

Save the file if you haven’t done so already.

Right-click on the project and select: Run As -> Run on Server

Starting Tomcat

You should hopefully see a screen like below, make sure the path you use is this:
http://localhost:8080/MyRFProject/index.jsf
Type some text into the textbox and hit the command button, you should see a response (via AJAX).

Final result

12 Responses to “Starting with JSF, JBoss RichFaces, AJAX, and Tomcat”

  1. [...] Starting with JSF, JBoss RichFaces, AJAX, and Tomcat « Giant … Share and [...]

  2. juan says:

    muy interesante, podria mostrar el web.xml ? por que he tenido muchos problemas con el

  3. sampath says:

    hello,i did the same way u did, bt i m nt getting the hoped scrren in my browser,error is coming….

    06:48:27,539 ERROR [MainDeployer] Could not start deployment: file:/C:/java software/jboss-4.2.3.GA/server/default/deploy/proj1.war
    org.jboss.deployment.DeploymentException: URL file:/C:/java software/jboss-4.2.3.GA/server/default/tmp/deploy/tmp2958554349965348282proj1-exp.war/ deployment failed

  4. Memoks says:

    Pure awesomeness !!

  5. martosfre says:

    Good reference….

  6. tom0916 says:

    It’s very helpful~ Thanx

  7. Rahul Tewari says:

    Very helpful. Followed the instructions and it worked with Tomcat 6.0. Thanks

  8. Carlos says:

    Thank you very much. These are a very interesting guidelines for the beginners. Thanks a lot.

  9. aleksandr says:

    How to deploy it on JBoss 5.1?

  10. Chad Lung says:

    @aleksandr,

    Just add JBoss 5.1 to Eclipse as a server to deploy to.

    Chad

  11. Great start-up guide! Thanks for spelling everything out with lots of screenshots. One thing: I had to move the index.jsp page out of WEB-INF into the main WebContent folder to make it visible.

  12. JAPeTo says:

    Thanks . Very helpful.

Leave a Reply