JSF Guide
JSF Guide
0 JSF Guide
iii
iv
Part I. Introduction and Quick Start
Chapter 1.
Introduction
In the past, using a JEE application server meant using the JSF implementation that ships with
it. However, there are subtle differences between JSF implementations. Applications written for
Mojarra don't always run well on MyFaces. There are sometimes backward compatibility issues
between JSF specification levels. An application written for JSF 1.2 won't always run on JSF 2.0.
JBoss AS6 is designed for maximum flexibility in JSF deployments. With JBoss AS6, you can use
the default JSF implementation, use a secondary JSF implementation, or bundle your own JSF
implementation with the WAR. You can have different applications in the same server instance that
use different implementations. Also, you can create your own JSF Configurations that include a
JSF implementation, extra libraries, and configuration parameters. Then assign the configuration
to one or more applications.
In this guide, we'll step through a simple JSF example. Then we will go through all the powerful
deployment options for JSF applications.
3
4
Chapter 2.
Quick Start
In this chapter, we demonstrate the world's simplest JSF "Hello World" application.
• hellojsf.war
• hellojsf.war/WEB-INF
<?xml version="1.0"?>
<web-app>
</web-app>
Note
As shown, you don't necessarily need to declare a FacesServlet or mappings in
web.xml. If you leave this out, JBoss AS6 will add it automatically with default
mappings as demonstrated at the end of this chapter.
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/
web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
5
Chapter 2. Quick Start
The faces-config.xml is only there to signal to JBoss AS that this is a JSF application. There are
many other ways that JBoss AS6 could recognize this as a JSF application. This is explained in
detail in chapter 3.
We use a little JSF2/EL 2.2 trick to avoid the need for a backing bean. We can grab the input
value directly from the request object using a parameterized EL expression.
<f:view>
<h:form id="form1">
<h:outputText value="Enter Your Name:"/>
<h:inputText id="name"/>
<h:commandButton value="Submit" />
</h:form>
<h:outputText rendered="#{not empty request.getParameter('form1:name')}"
value=" Hello #{request.getParameter('form1:name')}"/>
</f:view>
</html>
Start JBoss AS6 and put any of the following URLs into your browswer:
• http://localhost:8080/hellojsf/index.jsf
• http://localhost:8080/hellojsf/index.faces
• http://localhost:8080/hellojsf/faces/index.xhtml
6
Part II. Configuration
Chapter 3.
Note
A JSF Configuration is more than just the implementation jars. It can contain
supporting jars such as parsers and JSF component libraries. It also contains
initialization and configuration settings that are applied to your application when
the JSF Configuration is added to your WAR deployment.
Inside the jsf.deployer/META-INF directory you will find a file called jsf-integration-
deployer-jboss-beans.xml. You can use this file for advanced configuration of the deployer,
which we will describe in some of the sections that follow.
When a web application is deployed, the JSF Deployer determines if it is a JSF application. It
recognizes a web application if any of the following are true:
9
Chapter 3. Deploying Your JSF...
If it adds the FacesServlet, it will also add the following mappings for it:
• /faces/*
• *.jsf
• *.faces
Note
MyFaces and pre-2.0 versions of Mojarra still assume that you will declare a
FacesServlet in your web.xml. So if you are using those versions you can not
depend on auto-adding.
<bean name="JSFImplManagementDeployer">
<!--
* Specify the servlet classes that signal this deployer to add JSF to a WAR.
-->
<property name="facesServlets">
10
Bundling JSF Inside Your WAR
<collection elementClass="java.lang.String">
<value>javax.faces.webapp.FacesServlet</value>
<value>org.foo.MyWrapperFacesServlet</value>
</collection>
</property>
To do that, just specify the WAR_BUNDLES_JSF context param in your web.xml file like this:
<context-param>
<param-name>org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL</param-name>
<param-value>true</param-value>
</context-param>
Note
This context-param was available in earlier versions of JBoss AS. However, it
only worked when bundling MyFaces and when using the default classloader
configuration. Now in AS6 you can use this context-param any time you want to
bundle your own JSF impl.
If you look at the deployers/jsf.deployer directory you will see the JSF configurations that
ship with JBoss AS6. To tell your application to use one of these JSF configurations, add this to
your web.xml:
<context-param>
<param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
11
Chapter 3. Deploying Your JSF...
<param-value>Mojarra-1.2</param-value>
</context-param>
It's also useful for testing the same application against different JSF implementations, library
versions, and configurations. You can create a JSF Configuration and then apply it to your WAR
with a a simple context param.
A JSF Configuration consists of some jars and a special web.xml file. When a JSF Configuration
is added to a WAR by the JSF Deployer, the jars are added to the classpath and the elements
in the web.xml file are activated. To add your own JSF Confguration, just create the directory
structure below. This is usually done in the jsf.deployer directory:
• jsf.deployer/MyJSFConfig/jsf-libs
• jsf.deployer/MyJSFConfig/META-INF
Place your jar files in /jsf-libs and place your web.xml in /META-INF. When your JSF
Configuration is activated for a WAR, all jars in the jsf-libs directory will be added to the classpath.
The elements in your special META-INF/web.xml file will also be added to your WAR. This can
help you configure the JSF implementation and component libraries. However, note that only a
few web.xml elements are allowed in this file. These elements are servlet, servlet-mapping, filter,
filter-mapping, listener, and context-param. All other web.xml elements are currently ignored, but
we may support more in the future.
<property name="jsfConfigurations">
<map keyClass="java.lang.String" valueClass="java.lang.String">
<entry>
<key>Mojarra-1.2</key>
<value>${jboss.server.home.url}deployers/jsf.deployer/Mojarra-1.2</value>
</entry>
<entry>
<key>Mojarra-2.0</key>
12
Activating a New JSF Configuration
<value>${jboss.server.home.url}deployers/jsf.deployer/Mojarra-2.0</value>
</entry>
<entry>
<key>MyJSFConfig</key>
<value>${jboss.server.home.url}deployers/jsf.deployer/MyJSFConfig</value>
</entry>
</map>
</property>
<bean name="JSFUrlIntegrationDeployer-MyJSFConfig">
<property name="JSFImplName">
<value>MyJSFConfig</value>
</property>
<property name="JSFImplManagementDeployer">
<inject bean="JSFImplManagementDeployer"/>
</property>
</bean>
13
14
Chapter 4.
To enable JBossSerialization in Mojarra JSF, set the following context parameter in your web.xml
file.
<context-param>
<param-name>com.sun.faces.serializationProvider</param-name>
<param-value>org.jboss.web.jsf.integration.serialization.JBossSerializationProvider</param-
value>
</context-param>
15
16
Part III. Reference
Chapter 5.
Reference
5.1. JSF Standard Context Params
Table 5.1.
19
Chapter 5. Reference
20
JSF Standard Context Params
21
Chapter 5. Reference
Table 5.2.
22
Mojarra Context Params
23
Chapter 5. Reference
24
Mojarra Context Params
25
Chapter 5. Reference
26
JBoss JSF Context Params
Table 5.3.
27
28