3/04/2011

Getting started with Liferay 6 and ICEfaces 2.0 - Part 2



This tutorial requires that you have read Part 1 of "Getting Started with Liferay 6 and ICEfaces 2.0".


So - time for a step by step "Hello World" tutorial with Liferay 6 and ICEfaces 2.0. I finally had the time to create a simple example. What we basically do is to combine an already existing icefaces 2.0 example of portletfaces with the liferay IDE. So if some of the content of the web.xml looks familiar to you, take a look here.


If you have installed the Liferay IDE, you can easily create a portlet. Give your portlet a fancy name, and now we have to transform it to be an ICEfaces 2.0 portlet.


faces-config


JSF (and therefore ICEfaces) requires a config file called "faces-config.xml". Since JSF 2.0, a lot of the configuration stuff is obsolete and we just have to provide an empty faces-config.xml with the following in it:

 <?xml version="1.0" encoding="UTF-8"?>
<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>


portlet.xml

We need to tell our portlet, that it is now a "bridged" faces portlet, and no longer a standard MVC Portlet. We do so by adding the new portlet class and the new view id parameter:



<portlet-class>org.portletfaces.bridge.GenericFacesPortlet</portlet-class>
<init-param>
<name>javax.portlet.faces.defaultViewId.view</name>
<value>/index.xhtml</value>
                 </init-param>

Remove all other init-param parameters and the old portlet-class.




web.xml


Big, big changes in the web.xml. Basically you can throw away your old one and replace it with these entries:



  <display-name>helloworld-portlet</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>


<context-param>
<param-name>org.icefaces.uniqueResourceURLs</param-name>
<param-value>false</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>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>

<security-constraint>
<web-resource-collection>
<web-resource-name>Facelet View XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>nobody</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>nobody</role-name>
</security-role>

  <jsp-config>
   <taglib>
   <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
   <taglib-location>
   /WEB-INF/tld/liferay-portlet.tld
   </taglib-location>
   </taglib>
  </jsp-config>





libs

You need the following libs in your WEB-INF/lib directory :

  • commons-beanutils
  • commons-collections
  • commons-digester
  • commons-logging
  • icefaces-2.0.1
  • icefaces-compat-2.0.1
  • jboss-el.jar
  • jsf-api-2.1.1.jar
  • jsf-impl-2.1.0.jar
  • portletfaces-bridge-2.0.0.jar

View

Create a Managed Bean that will just return a "hello world" string:


package de.test.bean;


import javax.faces.bean.ManagedBean;


@ManagedBean
public class MyBean {
private String test = "Hello ICEfaces World !";


public String getTest() {
return test;
}


public void setTest(String test) {
this.test = test;
}
}

Next, create a file called index.xhtml in the root of your web application and reference your managed bean from it:

<?xml version="1.0" encoding="UTF-8"?>
<f:view xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ice="http://www.icesoft.com/icefaces/component" >
<h:head />
<h:body styleClass="example-icefaces-portlet">
<ice:outputText value="#{myBean.test}"/>
</h:body>
</f:view>


That should be all - your "Hello World" example with ICEfaces 2.0 and Liferay 6 is complete. 

If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.