As I happily wrote about new features of JSF 2.0, my coauthor David Geary
kept asking me how to run the examples in Tomcat 6. I kept putting it
off—hunting down all those JAR files and web.xml fragments
is just too much like eating soup with a fork. I finally got around to doing
the research and thought that others might benefit from the (unhappy) results,
if only to realize that this may be the time for switching to GlassFish.
This part is easy and has not changed from previous versions. Download the
reference implementation
and put jsf-api.jar and jsf-impl.jar into the
WEB-INF/lib directory.
The expression language has changed in minor ways. Perhaps the most useful enhancement for JSF programmers is the use of arguments in action methods. For example,
<h:dataTable var="row" ...>
...
<h:column>
<h:commandLink action="#{myBean.doSomething(row)}" .../>
</h:column>
</h:dataTable>
Before JSF 2.0, you had to fuss with sPAL
or a TableModel, so this is definitely an improvement you want to
put to work. Unfortunately, the EL is not a part of JSF, so you need to get the
bits from somewhere else. This
page has links to outdated versions of the API and implementation JARs in
the GlassFish Maven repository. You can divine the correct links for the
current version (2.2) from there. They are
If you aren't excited about using an unversioned snapshot, you can download
and install GlassFish v3 and use modules/javax.servlet.jsp.jar (for the API)
and modules/el-impl.jar. Either way, put the two JARs into the
WEB-INF/lib directory.
You also need to put this into your web.xml:
<context-param> <param-name>com.sun.faces.expressionFactory</param-name> <param-value>com.sun.el.ExpressionFactoryImpl</param-value> </context-param>
CDI gives a more robust mechanism for managing beans and their dependencies
than JSF managed beans. You should probably skip
the @ManagedBean annotation altogether and just get going with
@Named. (The sole purpose of @ManagedBean is to
support an otherwise unmodified Tomcat 6.)
More importantly, CDI gives you conversation scope.
Download the reference
implementation and add the file
artifacts/weld/weld-servlet.jar to WEB-INF/lib.
Put this into your web.xml:
<listener> <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> </listener>
Bean validation lets you attach validators where they belong—to the bean properties that are being validated rather than the pages doing the validation. For example,
public class PaymentBean {
@Size(min=13) private String card;
@Future public Date getDate() { ... }
...
}
This way, you can't have inconsistent validation rules on different pages. JSF 2.0 supports bean validation, but only when an implementation is present.
Download the reference
implementation and add the files
hibernate-validator-4.0.2.GA.jar, and all JARs in the
lib directory to WEB-INF/lib. This is not so
wonderful—you get to include yet another copy of slf4j as well as a
jpa-api-2.0.Beta-20090815.jar, which isn't exactly confidence-inspiring.
This can be done—see these instructions. But it doesn't mean that it should be. You'd have to manually obtain an entity manager and manually handle transactions. There are also restrictions on dynamic weaving of entities, and you would need a pretty deep understanding of JPA to understand the ramifications.
You can, with some pain, use JSF 2 with Tomcat 6. The EL support is critical—you don't want an EL expression to fail mysteriously that works fine elsewhere. CDI and bean validation are optional, but they are better solutions than the equivalent JSF facilities, so it makes good sense to learn and use them.
If you use JPA (which you should seriously consider if your application accesses a database), don't even think of using Tomcat. But even if you don't, ask yourself what you gain from the Tomcat pain. GlassFish v3 is very fast, easy to manage, and, due to its modular nature, you get as much or as little of EE 6 as you want.