Kito Mann: JavaServer Faces in Action

Here is my chapter 1 sample source code and what shows up after launch from NetBeans 6.1. Paths given here are those of NetBeans contrival. In Eclipse, Web Pages is Web Content and Source Packages is src.

Here are the sources:

Web Pages/hello.jsp
Web Pages/goodbye.jsp
 
Source Packages/com.jia.hello/hellobean.java
 
Web Pages/WEB-INF/faces-config.xml
Web Pages/WEB-INF/web.xml

You're asking yourself why I give such long, drawn-out examples and explanations: it's because this all took me an extraordinarily long time to figure out. It's complicated and I have to have a place to come back to refresh my memory, retrace my steps, over and over again, until it sticks.

For example, comparing the source code and behavior of JavaServer Faces in Action to the code in Core JavaServer Faces, I now understand the distinction in the web.xml file between

	<url-pattern>  /faces/*  </url-pattern>

and

	<url-pattern>  *.faces  </url-pattern>

To see the distinction yourself, follow what I've done here and keep an eye on the address line. This distinction, one of two or three suggested by Geary and Horstmann in their first chapter, is what confused me because it wasn't clear which to use (both are possible) and what address line to type in.

Moreover, Eclipse and NetBeans decide on this themselves and, depending on what you've chosen to put in web.xml, they're going to choose wrong. As you see here, I have to fix NetBean's idea of what the address should be (instead of http://localhost:8084/JSFAction/). How to tell these environments what to put on the address line?

Okay, this is fairly trivial, even straightforward once you solve it, but why if you put /faces/* in web.xml does NetBeans turn around and launch Firefox with just the root name and a slash? I've told it that no address will work if it doesn't contain /faces/. But, I'm getting ahead of myself—share the pain with me here as I launch unsuccessfully:

The application was launched with the following command line:

	http://localhost:8084/JSFAction/

...and the result appeared at:

	(nowhere: the result never appeared by reason of HTTP Status 500)

My analysis is that the error occurs on line 7 of hello.jsp:

	org.apache.jasper.JasperException: An exception occurred processing JSP page
	/hello.jsp at line 7

	4: <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
	5: <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
	6: 
	7: <f:view>
	8: <html>
	9:   <head>

The “root” cause is explained as...

	java.lang.RuntimeException: Cannot find FacesContext

This is a frequent error when I play around with JSF and it makes little sense because this code is copied right out of the book. Something called FacesContext is always missing, but what is it and where is it in my code?

Now, if I change the way the application is launched to:

	http://localhost:8084/JSFAction/faces/hello.jsp

I get the display I want to see:

Clicking redisplay gives me the expected error since the text (number) field contains 0 and 0 is not between 1 and 500:

Redisplaying with 45 gives the following page:

Clicking Goodbye gives me an HTTP Status 404 error:

Ostensibly because, as shown in the address line above, the wrong JSP was used. This has got to be a coding error in the example because if I fill in goodbye.jsp by hand, I get what I think is supposed to happen:

This is what I have left to fix before pronouncing this tutorial well and truly gathered.