Chapter 10. Using JavaServer Faces

Starting with version 4.0.3, the JBoss Application Server features built-in JavaServer Faces support. The implementation used is Apache MyFaces; however, the MyFaces extensions are not included with the JBoss distribution at this time. JBoss allows you to install your JSF application without putting lots of extra JSF implementation JAR files in your WEB-INF/lib directory.

To make use of JSF, you need to declare the Faces Servlet and the servlet mapping in the web.xml file:

<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>

In order to initialize the JSF environment, you also need to add the MyFaces listener to web.xml file:

<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

There are several additional context parameters that can be set to control the configuration of JavaServer Faces for a particular application:

<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>
        /WEB-INF/examples-config.xml
    </param-value>
    <description>
        Comma separated list of URIs of (additional) faces config files.
        (e.g. /WEB-INF/my-config.xml)
        See JSF 1.0 PRD2, 10.3.2
    </description>
</context-param>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
    <description>
        State saving method: "client" or "server" (= default)
        See JSF Specification 2.5.2
    </description>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
    <description>
        This parameter tells MyFaces if javascript code should be allowed in the
        rendered HTML output.
        If javascript is allowed, command_link anchors will have javascript code
        that submits the corresponding form.
        If javascript is not allowed, the state saving info and nested parameters
        will be added as url parameters.
        Default: "true"
    </description>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
    <description>
        This parameter tells MyFaces if javascript code should be allowed in the
        rendered HTML output.
        If javascript is allowed, command_link anchors will have javascript code
        that submits the corresponding form.
        If javascript is not allowed, the state saving info and nested parameters
        will be added as url parameters.
        Default: "false"
        
        Setting this param to true should be combined with STATE_SAVING_METHOD "server" for
        best results.
        
        This is an EXPERIMENTAL feature. You also have to enable the detector 
        filter/filter mapping below to get JavaScript detection working.
    </description>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
    <description>
        If true, rendered HTML code will be formatted, so that it is "human readable".
        i.e. additional line separators and whitespace will be written, that do not
        influence the HTML code.
        Default: "true"
    </description>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
    <description>
        If true, a javascript function will be rendered that is able to restore the
        former vertical scroll on every request. Convenient feature if you have pages
        with long lists and you do not want the browser page to always jump to the top
        if you trigger a link or button action that stays on the same page.
        Default: "false"
    </description>
</context-param>

To use another JSF implementation, such as the reference implementation, instead of the bundled MyFaces implementation, simply delete the

jbossweb-tomcat55.sar/jsf-lib

directory. You will need to include the the implementation JAR files in your own

WEB-INF/lib

directory for each web application making use of JSF.