Chapter 24. 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 Section 23.1, “Example Code” applies here too.
24.1. Example Code
This section cites code from JSF2+RF4 Hello World Portlet from the Quickstarts Collection .
24.1.1. pom.xml
We need to add several RF-specific dependencies to the general JSF ones:
Example 24.1. pom.xml
<dependencies> <!-- The versions, scopes and types of these dependencies are managed in gatein-*-bom. You need to name only groupId and artifactId here. Name only those artifacts you refer to in your code. Look at gatein-*-bom POM file for the complete list of available artifacts. --> <!-- General JSF dependencies --> <dependency> <groupId>org.jboss.spec.javax.faces</groupId> <artifactId>jboss-jsf-api_2.1_spec</artifactId> </dependency> <dependency> <groupId>org.jboss.portletbridge</groupId> <artifactId>portletbridge-api</artifactId> </dependency> <!-- RF-sprecific dependencies --> <dependency> <groupId>org.jboss.portletbridge</groupId> <artifactId>portletbridge-extension-richfaces</artifactId> </dependency> <dependency> <groupId>org.richfaces.ui</groupId> <artifactId>richfaces-components-api</artifactId> </dependency> <dependency> <groupId>org.richfaces.ui</groupId> <artifactId>richfaces-components-ui</artifactId> </dependency> <dependency> <groupId>org.richfaces.core</groupId> <artifactId>richfaces-core-impl</artifactId> </dependency> </dependencies>
24.1.2. JSF Template Files
We use
<rich:*> components in the templates:
Example 24.2. Form with rich: components in main.xhtml
</p> <h:form id="jsf2HelloWorldPortlet"> <h:panelGrid columns="2"> <h:outputLabel value="#{msgs.Greeting}" for="greeting"/> <rich:select id="greeting" value="#{helloBean.greeting}"> <f:selectItems value="#{helloBean.greetings}" /> <f:ajax render="output" event="selectitem"/> </rich:select> <h:outputLabel value="#{msgs.Name}" for="nameInput"/> <h:inputText id="nameInput" value="#{helloBean.name}"> <f:validateLength minimum="1" maximum="50" /> <f:ajax render="output" event="keyup"/> </h:inputText> </h:panelGrid> <p> <h:panelGroup id="output"> <strong><h:outputText value="#{helloBean.greeting} #{helloBean.name}!" rendered="#{not empty helloBean.name}"/></strong> </h:panelGroup> </p> <p> <h:commandButton id="reset" value="#{msgs.Reset}" actionListener="#{helloBean.reset}"> <f:ajax render="@form" /> </h:commandButton> - #{msgs.ResetComment} </p> <p> <h:commandButton id="reload" value="#{msgs.Reload}" /> - #{msgs.ReloadComment} </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.
24.1.3. Java Beans
The
HelloBean presented in the Section 23.1, “Example Code” 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.
Example 24.3. HelloBean.java
/** * Static list of greetings. Contains {@code "Hello"} and {@code "Hi"}. */ private static final List<SelectItem> GREETINGS; static { List<SelectItem> l = new ArrayList<SelectItem>(2); l.add(new SelectItem("Hello")); l.add(new SelectItem("Hi")); GREETINGS = Collections.unmodifiableList(l); } /** * Stores the greeting phrase which will be used to greet the application user. */ private String greeting;
Example 24.4. HelloBean.java
/** * Returns {@link #greeting}. * * @return {@link #greeting} */ public String getGreeting() { return greeting; } /** * Set {@link #greeting}. * * @param greeting */ public void setGreeting(String greeting) { this.greeting = greeting; } /** * Returns {@link #GREETINGS}. * * @return {@link #GREETINGS} */ public List<SelectItem> getGreetings() { return GREETINGS; } /** * Resets {@link #name} to the default value {@code "World"} and {@link #greeting} with the default value {@code "Hello"}. * * @param ae ignored */ public void reset(ActionEvent ae) { this.name = "World"; this.greeting = "Hello"; } }
24.1.4. portlet.xml
There is no substantial change in
portlet.xml against Section 23.1, “Example Code”. Only <description> , <portlet-name> , <display-name> and <title> have been changed.
Example 24.5. 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>
24.1.5. web.xml
We set a few more
init-params in web.xml for RichFaces components to work:
Example 24.6. 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>
24.1.6. Custom CSS
Fully analogous with basic JSF portlets. See Chapter 23, Basic JSF Portlet Development for more information about basic JSF Portlet Development.
24.1.7. Internationalization
Fully analogous with basic JSF portlets. See Chapter 23, Basic JSF Portlet Development for more information about JSF portlet development.
24.2. 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.