JBoss Community Archive (Read Only)

GateIn Portal 3.8

JSF Portlet Development with RichFaces

As we have already noted, RichFaces (RF) is just a component library for JavaServer Faces (JSF). Therefore, everything said in the Basic JSF Portlet Development chapter applies here too.

Example Code

This section cites code from JSF2+RF4 Hello World Portlet from the Quickstarts Collection.

pom.xml

We need to add several RF-specific dependencies to the general JSF ones:

pom.xml
068.     <dependencies>
069.         <!-- 
070.             The versions, scopes and types of these dependencies are managed in gatein-*-bom.
071.             You need to name only groupId and artifactId here.
072.             Name only those artifacts you refer to in your code.
073.             Look at gatein-*-bom POM file for the complete list of available artifacts.
074.         -->
075.         <!-- General JSF dependencies -->
076.         <dependency>
077.             <groupId>org.jboss.spec.javax.faces</groupId>
078.             <artifactId>jboss-jsf-api_2.1_spec</artifactId>
079.         </dependency>
080.         <dependency>
081.             <groupId>org.jboss.portletbridge</groupId>
082.             <artifactId>portletbridge-api</artifactId>
083.         </dependency>
084. 
085.         <!-- RF-sprecific dependencies -->
086.         <dependency>
087.             <groupId>org.jboss.portletbridge</groupId>
088.             <artifactId>portletbridge-extension-richfaces</artifactId>
089.         </dependency>
090.         <dependency>
091.             <groupId>org.richfaces.ui</groupId>
092.             <artifactId>richfaces-components-api</artifactId>
093.         </dependency>
094.         <dependency>
095.             <groupId>org.richfaces.ui</groupId>
096.             <artifactId>richfaces-components-ui</artifactId>
097.         </dependency>
098.         <dependency>
099.             <groupId>org.richfaces.core</groupId>
100.             <artifactId>richfaces-core-impl</artifactId>
101.         </dependency>
102.     </dependencies>

JSF Template Files

We use <rich:*> components in the templates:

Form with rich: components in main.xhtml
43.         </p>
44.         <h:form id="jsf2HelloWorldPortlet">
45.             <h:panelGrid columns="2">
46.                 <h:outputLabel value="#{msgs.Greeting}" for="greeting"/>
47.                 <rich:select id="greeting" value="#{helloBean.greeting}">
48.                     <f:selectItems value="#{helloBean.greetings}" />
49.                     <f:ajax render="output" event="selectitem"/>
50.                 </rich:select>
51.                 
52.                 <h:outputLabel value="#{msgs.Name}" for="nameInput"/>
53.                 <h:inputText id="nameInput" value="#{helloBean.name}">
54.                     <f:validateLength minimum="1" maximum="50" />
55.                     <f:ajax render="output" event="keyup"/>
56.                 </h:inputText>
57.             </h:panelGrid>
58.             <p>
59.                 <h:panelGroup id="output">
60.                     <strong><h:outputText value="#{helloBean.greeting} #{helloBean.name}!" rendered="#{not empty helloBean.name}"/></strong>
61.                 </h:panelGroup>
62.             </p>
63.             <p>
64.                 <h:commandButton id="reset" value="#{msgs.Reset}" actionListener="#{helloBean.reset}">
65.                     <f:ajax render="@form" />
66.                 </h:commandButton> - #{msgs.ResetComment}
67.             </p>
68.             <p>
69.                 <h:commandButton id="reload" value="#{msgs.Reload}" /> - #{msgs.ReloadComment}
70.             </p>

The complete source code of the above template can be found in src/main/webapp/pages/main.xhtml of JSF2+RF4 Hello World Portlet quickstart.

Java Beans

The HelloBean presented in the Basic JSF Portlet Development chapter was extended firstly to provide a list of greeting phrases selectable in the drop-down box on the main.xhtml page and secondly to be able to store the greeting phrase selected in the drop-down box.

HelloBean.java
039.     /**
040.      * Static list of greetings. Contains {@code "Hello"} and {@code "Hi"}.
041.      */
042.     private static final List<SelectItem> GREETINGS;
043. 
044.     static {
045.         List<SelectItem> l = new ArrayList<SelectItem>(2);
046.         l.add(new SelectItem("Hello"));
047.         l.add(new SelectItem("Hi"));
048.         GREETINGS = Collections.unmodifiableList(l);
049.     }
050. 
051.     /**
052.      * Stores the greeting phrase which will be used to greet the application user.
053.      */
054.     private String greeting;
HelloBean.java
088.     /**
089.      * Returns {@link #greeting}.
090.      * 
091.      * @return {@link #greeting}
092.      */
093.     public String getGreeting() {
094.         return greeting;
095.     }
096. 
097.     /**
098.      * Set {@link #greeting}.
099.      * 
100.      * @param greeting
101.      */
102.     public void setGreeting(String greeting) {
103.         this.greeting = greeting;
104.     }
105. 
106.     /**
107.      * Returns {@link #GREETINGS}.
108.      * 
109.      * @return {@link #GREETINGS}
110.      */
111.     public List<SelectItem> getGreetings() {
112.         return GREETINGS;
113.     }
114. 
115. 
116.     /**
117.      * Resets {@link #name} to the default value {@code "World"} and {@link #greeting} with the default value {@code "Hello"}.
118.      * 
119.      * @param ae ignored
120.      */
121.     public void reset(ActionEvent ae) {
122.         this.name = "World";
123.         this.greeting = "Hello";
124.     }
125. 
126. }

portlet.xml

There is no substantial change in portlet.xml against the Basic JSF Portlet Development chapter. Only <description>, <portlet-name>, <display-name> and <title> have been changed.

portlet.xml
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
   http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
    <portlet>
        <description>A simple portlet usinf JSF2 and RF4.</description>
        <portlet-name>jsf2Rf4HelloWorldPortlet</portlet-name>
        <display-name>JSF2+RF4 Hello World Portlet</display-name>
        <portlet-class>javax.portlet.faces.GenericFacesPortlet</portlet-class>
        <init-param>
            <name>javax.portlet.faces.defaultViewId.view</name>
            <value>/pages/main.xhtml</value>
        </init-param>
        <init-param>
            <name>javax.portlet.faces.defaultViewId.edit</name>
            <value>/pages/edit.xhtml</value>
        </init-param>
        <init-param>
            <name>javax.portlet.faces.defaultViewId.help</name>
            <value>/pages/help.xhtml</value>
        </init-param>
        <init-param>
            <name>javax.portlet.faces.preserveActionParams</name>
            <value>true</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>VIEW</portlet-mode>
            <portlet-mode>EDIT</portlet-mode>
            <portlet-mode>HELP</portlet-mode>
        </supports>
        <portlet-info>
            <title>JSF2+RF4 Hello World Portlet</title>
        </portlet-info>
        <container-runtime-option>
            <name>org.gatein.pc.remotable</name>
            <value>true</value>
        </container-runtime-option>
    </portlet>
</portlet-app>

web.xml

We set a few more init-params in web.xml for RichFaces components to work:

web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <display-name>jsf2-rf4-hello-world-portlet</display-name>
    <context-param>
        <description>See https://docs.jboss.org/author/display/PBR/Installing+Portlet+Bridge#InstallingPortletBridge-DisableautomaticinclusionofPortletBridge</description>
        <param-name>org.gatein.portletbridge.WAR_BUNDLES_PORTLETBRIDGE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <description>See https://docs.jboss.org/author/display/PBR/Render+Policy</description>
        <param-name>javax.portlet.faces.RENDER_POLICY</param-name>
        <param-value>ALWAYS_DELEGATE</param-value>
    </context-param>
    
    <!-- The following params are documented here: http://myfaces.apache.org/core21/myfaces-impl/webconfig.html -->
    <context-param>
        <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
        <param-value>*.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <!-- Change to Production to compress js files, etc. -->
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <description>http://docs.jboss.org/richfaces/latest_4_X/Developer_Guide/en-US/html/chap-Developer_Guide-Skinning_and_theming.html</description>
        <param-name>org.richfaces.skin</param-name>
        <param-value>#{skinBean.skin}</param-value>
    </context-param>
    <context-param>
        <description>See http://docs.jboss.org/richfaces/latest_4_X/Developer_Guide/en-US/html/chap-Developer_Guide-Advanced_features.html</description>
        <param-name>org.richfaces.resourceOptimization.enabled</param-name>
        <param-value>true</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>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>xcss</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping>

</web-app>

Custom CSS

Fully analogous with plain JSF portlets.

Internationalization

Fully analogous with plain JSF portlets.

Further Steps

After having done all the above, it is time to build and deploy the portlet, import it and add it to a page so that you can test its functionality.

See also

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 13:19:34 UTC, last content change 2013-05-30 13:25:20 UTC.