Getting Started with Primefaces Portlet in liferay.

JSF(Java Server Faces) is a java based web application framework which is component based user interface specification to develop web application.The Jsf provides different components for Standard faces ,alloy faces ,primefaces, icefaces   and richfaces.

In  Liferay we can build an application using these faces which requires very few  dependencies.

Here we will see how to create a simple library  application which stores the booktitle and author name ,in liferay using primefaces.

NameSpace  xmlns:p= http://primefaces.org/ provides  all the  primefaces components.

Step-1.Create a new liferay project  as shown below

This provides the primefaces jar file that gets included in the classpath and some of the liferay dependencies  also gets included.

liferay-faces-util-3.1.0-rc2.jar

liferay-faces-portal-3.1.0-rc2.jar

liferay-faces-bridge-impl-3.1.0-rc2.jar

liferay-faces-bridge-api-3.1.0-rc2.jar

jsf-impl-2.1.3-b02.jar

jsf-api-2.1.3-b02.jar

 

Step-2

Now we need to modify the faces-config.xml file.

<?xml version=“1.0”?>

<faces-config  version=“2.0”

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

<lifecycle>

<phase-listener>com.liferay.faces.bridge.lifecycle.BridgeDebugPhaseListener</phase-listener>

</lifecycle>

</faces-config>

 

To

 

<lifecycle>

<phase-listener>com.liferay.faces.util.lifecycle.DebugPhaseListener</phase-listener>

</lifecycle>

 

Step-3 Now we are all ready to create our application.

The default view.xhtml page is

<?xml version=“1.0”?>

<f:view

xmlns=“http://www.w3.org/1999/xhtml”

xmlns:c=“http://java.sun.com/jsp/jstl/core”

xmlns:f=“http://java.sun.com/jsf/core”

xmlns:h=“http://java.sun.com/jsf/html”

xmlns:p=“http://primefaces.org/ui”

xmlns:ui=“http://java.sun.com/jsf/facelets”>

<h:head />

<h:body>

<h:outputText value=“#{i18n[‘Library_Jsf-hello-world’]}” />

</h:body>

</f:view>

Note: {i18n} is the constant from the properties files.

Step-4 We will create a form to store the booktitle and the author name.

The booktitle and author name is stored in the database.The code is as below.

<h:body>

<h:form>

<p:outputLabel for=“bookTitle” value=“BookTitle:”  /><br />

<p:inputText value=“#{libraryBean.booktitle}” id=“bookTitle” >

<br />

<p:outputLabel for=“author” value=“Author:” /><br />

<p:inputText value=“#{libraryBean.author}” id=“author” />

<br /><br />

<p:commandButton action=“#{libraryBean.addBook}” value=“Add Book” />

</h:form>

</h:body>

</f:view>

The  value attributes in the inputText column  sets the booktitle and author name entered by the user into the attributes booktitle and author declared in the LibraryBean.The managed bean class appears as below.

@ManagedBean(name=”libraryBean”)

public class LibraryBean {

private String booktitle;

private String author;

public String getBooktitle() {

return booktitle;

}

public void setBooktitle(String booktitle) {

this.booktitle = booktitle;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

 

public void addBook(){

System.out.println(“inside add>>”+booktitle+”>>>”+author);

LibraryJsf book  = new LibraryJsfImpl();

long bookId = 0L;

try {

bookId = CounterLocalServiceUtil.increment(LibraryJsf.class.getName());

book.setBookId(bookId);

book.setBooktitle(booktitle);

book.setAuthor(author);

LibraryJsfLocalServiceUtil.addLibraryJsf(book);

} catch (SystemException e) {

e.printStackTrace();

}

}

}

Leave a Reply