How to Set Up Core JavaServer Faces
Example Code in Eclipse

Russell Bateman
last update: 5 May 2010

Table of Contents

Step by step
Running a new project
Appendix A: Setting up a database...
Appendix B: web.xml
Appendix C: faces-config.xml


In celebration of Cinco de mayo, 2010, I thought I'd throw this little how-to together for anyone who cares. It shows you the steps to setting up Geary's and Horstmann's sample code for use in Eclipse (Galileo). It would also be useful as a guide to making Eclipse projects of most any other sample source code. For example, I happen to own the Second Edition of this book, not the Third which covers JSF 2.0 and is already out. (I just haven't yet decided whether it's going to be relevant to me.)

Download the sample code for Core JavaServer Faces, Second Edition from their web site. I'm assuming you have the book, which is available on Safari and elsewhere, and that this exercise is relevant to you. The examples are pretty dang good, often in excruciating and enlightening detail.

Depending on which exercises you undertake, you might want to keep a thumb in this page since there is an errata link containing corrections to some of the code. Don't scratch your head before visiting this link for sample code that doesn't seem to work.

Some examples simply will not work as is. Many of these are discussed in the FAQ at the web site. In addition to those listed, the database-consuming examples will not work because you don't have a database set up, e.g.: for chapter 5's database project. See Appendix A: Setting up a database for these examples.

Step by step

  1. Download the sample code by clicking on the Source code for all examples link (click on the picture of the book above). Put the zip somewhere plausible to your development process. I put mine at C:\Users\russ\dev\downloads. Unzip it and clean up whatever you like to delete.
  2. Create a new folder, workspace, in the corejsf2-examples subdirectory.
  3. Launch Eclipse and set up the new workspace for JSF and web development. See Setting up Eclipse for web development.
  5. IMPORTANT: Each subdirectory of each chapter directory under corejsf2-examples will become an Eclipse project. Under each of these, there is a subdirectory named web. Eclipse prefers to call this directory WebContent, so you must be careful when creating the project not to leave the default name or you'll end up with a mess to clean up. (More on this in a moment.)
  7. Right-click in the Project Explorer view and choose New, then Dynamic Web Project. You might have to choose Other..., then Web to find this wizard.
  9. Uncheck Use default and then browse to the sample (project) you wish to set up, for example, corejsf-examples/ch1/login-xml, then type the name, ch1.login-xml, into Project name:. You browse first to find the project code, then take note of the name to use and prefix it using the chapter directory name. This is because some sample code projects have the same name under different chapters.
  11. Target runtime should be Apache Tomcat v6.0 and Dynamic web module version should be 25.
  13. Change Configuration from Default Configuration for Apache Tomcat v6.0 to JavaServer Faces v1.2 Project.
  15. Click Next twice.
  17. Change Content directory from WebContent to simply web. This is necessary because Geary/Horstmann named their equivalent subdirectory thus while Eclipse prefers the later name.
  19. If Generate web.xml deployment descriptor isn't checked, click to check it.
  21. Ensure that JSF 1.2 (Apache MyFaces) and JSTL, set up in the step from another document, are checked. The rest of this dialog is probably fine. It should appear approximately thus:

  23. Click Finish. You should now have a new project, ch1.login-xml, with errors that you will now correct.

  24. Correct the package name Eclipse establish by right-clicking on, choosing Refactor -> Rename..., then delete java. from the beginning of the name to leave just com.corejsf. This action will correct a) the red error box on and the warning on faces-config.xml where the package name is wrong and the bean specification inaccurate.
  26. Next, choose menu Project -> Clean... and click OK to rebuild the project.
  28. At this point, you should be able to run your project by right-clicking on the significant JSP file name under the web subdirectory and choosing Run As -> Run on Server (or the debug equivalent).

Running a new project

Here's how to run the newly created project:

  1. (Make the modifications to the package path and others noted above.)
  3. Stop the server that was running the last project you tried.
    1. Open the Eclipse Server tab.
    2. Right-click on the Tomcat server and choose Stop.
    3. Repeat and choose Clean.
    4. Repeat and choose Add and Remove....
    5. Click on old project in Configured: (right-hand panel) and click Remove.
    6. Click on new project in Available: (left-hand panel) and click Add.
    7. Click Finish.
    8. Right-click on the Tomcat server and choose Start. At this point, if your Eclipse console is open, you'll see a great number of lines in red ink appear. This is normal; these Tomcat INFO messages are printed in red.
  5. Right-click on the JSP, usually index.jsp, and choose Run As -> Run on Server.
  7. (If the Run on Server dialog comes up) Click Finish.
  9. If an error occurs, check the errata link given above to see if it's not a bug that's been found and fixed.

Appendix A: Setting up a database for these examples

On page 192 you see a dump of the database contents expected for chapter 5's database example. In this project is a file, web/misc/customers.sql, that can be used to create a table in your favorite database. If you have Eclipse Data Tools Platform plug-ins installed, this is fairly easily set up (see illustration below). Otherwise, you'll have to do it more or less by hand. Obviously, you'll not go to the trouble to do this unless that example interests you particularly.

To see how to do this, following the tutorial commented on at Companion Notes on Setting Up and Using Eclipse Data Tools Platform. Transfer those skills over to ch5.database. If you leave the de.vogella.dtp.example project in your Core JSF workspace, you'll be able to modify the SQL controls illustrated above as well as right-click on customers.sql and choose Execute SQL Files to set up the table. You will also have to modify to change the database URL to (if using Derby): jdbc:derby://localhost/C:/Users/russ/MyDB (or your equivalent path).

Appendix B: web.xml

The file web/WEB-INF/web.xml for most projects should appear thus. I have removed the <description> elements for brevity and enhanced the <welcome-file-list> over what Eclipse generated.

<web-app xmlns:xsi=""
    Faces Servlet
    Faces Servlet

Appendix C: faces-config.xml

The file web/WEB-INF/faces-config.xml for most projects should appear' thus, usually with a different managed-bean name, a different to-view-id name, etc.

<faces-config xmlns=""