In his blog, Keith poses a question “Are you using JSF or intending to?”. So here goes, these are my reasons for learning JSF.
  1. The answer Keith didn’t want. It’s a standard. I know this is a very boring and possibly not very good reason, but I’m interested in the new Java EE 5 spec and JSF is a part of the new spec.
  2. I don’t know JSF yet. This may sound pretty obvious, but I want to learn JSF because I don’t know it. If I don’t know JSF well enough to develop applications in it, I can’t really say whether it is fit for the types of applications I develop or whether I should be using some other framework like Struts or Wicket or Spring MVC.
  3. I want to learn Seam. Seam is based upon JSF, so its probably a good idea to get a good grounding in the basics before progressing to the next stage.
  4. JSF will supposedly allow me to develop web apps quickly. Whether this is true or not, I’m sure I’ll soon find out!
So, will I ever deploy an application with JSF? I don’t know, I’m still in the early stages of learning. I know there is plenty of information on the internet regarding peoples opinions of various technologies including JSF, but how much of it is FUD and how much is reasoned criticism?
 
As usual, when learning a new technology, the first application to write is a "Hello World" style application. To continue this tradition, I've written a very simple Hello World JSF application. After setting up the Faces servlet as described in my previous post, I've written a simple JSP page called hello.jsp which has the following contents:
<%@page contentType="text/html"%> 
<%@page pageEncoding="UTF-8"%>

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<f:view>
<h:outputText value="Hello World" />
</f:view>
</body>
</html>
This simple JSP page specifies that the 2 JSF taglibs "core" and "html" are to be used and specifies their appropriate prefixes. The HTML is pretty standard up until the <f:view> tag. This tag tells the container we are going to start using JSF components. Inside this tag, the outputText component is used to print out the text "Hello World" to the browser.
This trivial application is started by pointing the browser to http://localhost:8080/LearningJSF/hello.jsf
 
In a previous post, I specified my JSF URL mapping as *.jsf . If however, you try to access the JSP page that contains the JSF code directly (e.g. browse to hello.jsp instead of hello.jsf), then JSF will throw an IllegalStateException. It took me a few minutes to work out what the exception means, but in this case the description is rather obvious.
java.lang.IllegalStateException: No FacesContext is available to process this request. 
This is most likely due to the request being sent to the wrong path.
com.sun.faces.taglib.jsf_core.ViewTag.doStartTag(ViewTag.java:159)
org.apache.jsp.hello_jsp._jspx_meth_f_view_0(hello_jsp.java:106)
org.apache.jsp.hello_jsp._jspService(hello_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
....
 
I've just found a series of articles published on IBM Developer Works called "JSF for non believers" written by Richard Hightower. They seem to be a good tutorial into JSF.
There are 4 articles in the series namely:
 
Declaring the Faces servlet is the first necessary step in developing a JSF application. The following XML snippet (from WEB-INF/web.xml) allows the servlet to be declared and specifies that all requests ending in *.jsf are passed to the Faces servlet which routes them to the corresponding JSP page.
<context-param> 
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
I've noticed that when you create a JSF project in NetBeans, a different servlet mapping is used. NetBeans creates a servlet mapping with a url-pattern of /faces/* as shown below:
<servlet-mapping> 
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
I don't know if there is a standard convention to use here or if one is better than the other?
 
JSF applications need a JSF implementation to run. There seem to be a few of these about, most noticeably the Sun JSF Implementation and the Apache MyFaces JSF implementation.
The Sun implementation supports JSF 1.2 which is the latest version of JSF and part of the Java EE 5 specification. The Apache MyFacesimplementation supports JSF 1.1.
I'm interested in Java EE 5, so I'm going to start learning with the Sun implementation using the GlassFish application server using NetBeans 5.5 as my IDE.
I know NetBeans has a lot of wizards and helpers to allow experienced developers to write applications quicker, but since I'm learning I'm going to try and minimize the use of these wizards. I'm a firm believer of learning a topic first before I start to use IDE generated code. When I'm more proficient in JSF, I imagine the tools provided by IDEs will be invaluable.
 
This blog aims to help categorize my experiences whilst learning Java Server Faces (JSF).
My aim is to blog about the experiences I have whilst learning JSF, both good and bad. I'd be interested to hear from others who are either learning JSF, or who have learned JSF. If you have any sources of information which may be helpful (books, tutorials, web sites etc.), than I'd be interested in hearing about them.
I hope this blog will be both useful to me and to any others learning JSF.