Copyright ©May 2008
Tutorial: Using NetBeans or Eclipse with JDK 6, Apache Tomcat and JavaServer Faces
Three weeks ago, failing to complete several JSF tutorials for Eclipse launched me off on a tangent to learn about the deeper internals of JSF and then to Maven, whose promise was that it could set me up a blank but working JSF project that I could then just modify.
Here are my emerging notes for a JSF tutorial. What I learned here is relevant to Eclipse use also. In the end, it's more about learning JSF than Eclipse or NetBeans although tools often get in the way. And, at the present time, I'm spending pretty much all my time in Eclipse as I work through a new book, Essential JSF, Facelets & JBoss Seam.Bibliography, Tutorials, Source Code and Other, Related Resources
I bought the book, Core JavaServer Faces by David Geary and Cay Horstmann. It took me a long time to get it going. I tried and failed over and over again in Eclipse. This is what led me, first to Maven, and then to NetBeans.
The authors waffled on the source code details throwing a great deal of confusion into the process. It wasn't until I carefully waded through the different possibilities and chose one that I got it working. Click here for the details.
I also have the book, JavaServer Faces in Action, by Kito Mann which also has a sample in its first chapter. I haven't finished getting that one going; there's one last problem. I do have most of it working and I learned some crucial, if now trivial, things along the way. Click here for the details.
Now I have to try my own application. Click here for the details.
An excellent tutorial I'm going through at present is in the first two chapters of Essential JSF, Facelets & JBoss Seam, by Kent Tong. Those chapters are available to read free of charge here. I, however, purchased the whole book. At this writing, I'm going through it (it's only $19.95 on-line). It seems well-worth the price (if you're as desperate as I). You don't get bogged down in deep explanations, but the instructions seem to work and I've learned more than a couple of things.
Here are some things I've learned that I didn't know and whose absence from my wisdom caused me no end of trouble (probably why I went to NetBeans originally).
<url-pattern> /faces/* </url-pattern>instead of
<url-pattern> *.faces </url-pattern>which you will find in many books. Getting confused between what these older books say and what modern practice is can lead to HTTP errors and frustration. This has a particular effect upon the path typed into the browser address line.
/WEB-INF/faces-config.xml Faces Servlet javax.faces.webapp.FaceServlet /faces/*
Here are some random notes I'll incorporate into something worth reading one day.
- In a JSF application, a page is defined by a JSP file and identified by a view id or the relative path to the page from the root (kept as WebContent in Eclipse).
- Each JSP tag belongs to a certain tag library (including the main ones). These libraries are identified by URL. Before you use a tag, you must introduce a prefix or short-hand for the URL and then use it to qualify the tag name.
- When a warning appears in the text portion under the Web Page Editor looking at a JSP, and you've done everything right, save all files, drop Eclipse and relaunch it. If you've done everything right, the warning will sometimes disappear. If not, look harder. One sign is to try to run it anyway; if it works, it probably isn't your fault and bouncing Eclipse may fix the problem.
When you fix a warning or error (or suspect one is already fixed, or just want to see), choose Project -> Clean and clean the current project. If invalid (unless an Eclipse bug), the warning or error should disappear.
- If you're using an external browser to test with, you may need—at very least—to drop the page you're running before running again after some modifications. This is because Eclipse can't clean up and redeploy. I create a new tab, run and then discard the tab every time to make certain I don't have any of the deployed files (principally under WebContent/WEB-INF/lib) still open.
- Over and over again, I learn that web.xml is king and if there's the smallest typo in this file, “FacesServlet” instead of “Faces Servlet”, etc., the worst possible thing will happen, i.e.: an HTTP 404 error or some such thing.
- If web.xml is king, then faces-config.xml is minister of foreign affaires. Anything amiss in this file and the JSF wiring doesn't work and neither does your application.
- The JSF engine goes through the following phases during a form submission:
If there is any error in the Process Validations phase or the Update Domain Values phase, the JSF engine will jump to the Render Response phase directly. Tong addresses this with pretty good detail in chapter 2, especially his StockQuote example.
I intend to go over the preceeding list and define in some detail each phase.
- Managed beans gain access to each other via the <managed-property>. It is important to ensure that the referring bean has a shorter or same lifetime (scope) than the referred so that resources don't disappear from underneath it.
A domain class is a Java class that models the logic and/or concepts in the domain. The Request class is one such class.
“Domain” means the logic and concepts surrounding the business operations concerned: e.g.: the postage is $10 per kg, some patrons may have discounts, etc.
In contrast, there are other classes that deal with getting input from the user to displaying output to the user. Such classes are called “UI classes”.
In such an application if later we decide to use a new UI technology (e.g.: one from JSF to some new technology), the domain classes can be reused but the UI ones can't. This is still much better than if a class is involved with both domain logic and UI because then as the UI technology needs to change, the code won't be reusable.
- Never let a session-scoped bean refer to any other managed bean because when the session bean is written out to disk, the other bean won't be (since it's not session-scoped) and when read back in, the reference inside the session bean will be nil. See Essential JSF, Facelets & JBoss Seam, page 135 (chapter 4).
- Information can be hidden on forms in a special hidden JSF HTML tag (<h:inputHidden>).
- A facet is a child component that is subjected to special processing by its parent. In the shop site example (Essential JSF, chapter 4), the data rows of the catalog are facets and contain <h:outputText> tags. The summary on page 160 makes some important points about creating tables whose row data can only be determined at run time.