Tuesday, 8 May 2012

How to Create JSF Portlet in liferay


Step 1) Open Eclipse, Click on new liferay project
Step 2)Create Project Name, click on finish button
Step 3)Now create One class Under src floder,right click on src->go to new->Other click on class
Step 4) enter the package name and class name called UserBean  then click finish
Step 5)Uner UserBean Class  Add this code, which I mention below
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

      public String name;
      public String country;
      public String lastname;
     
     

//Create two method outcome and outcome1
     
      public String outcome(){

            FacesContext fc = FacesContext.getCurrentInstance();
           
            this.country = getCountryParam(fc);
           
           
            return "result";
      }
      public String outcome1()
      {
        FacesContext fc1 = FacesContext.getCurrentInstance();
           
            this.lastname = getlastnameParam(fc1);
           
           
            return "result";
           
      }

      //get value from "f:param"
      public String getCountryParam(FacesContext fc){

            Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
            return params.get("country");

      }
      public String getlastnameParam(FacesContext fc1)
      {
            Map<String,String> params = fc1.getExternalContext().getRequestParameterMap();
            return params.get("lastname");
           
      }
   
     
//getter and setter methods
     
      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
      }

      public String getCountry() {
            return country;
      }

      public void setCountry(String country) {
            this.country = country;
      }
      public String getlastname() {
            return lastname;
      }

      public void setlastname(String lastname) {
            this.lastname = lastname;
      }

     
     

}


Step 6) Now go to PortletViewMode.xhtml under docroot/xhtml  add this code under this file




 
<?xml version="1.0"?>

<html
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:icecore="http://www.icefaces.org/icefaces/core"
      xmlns:liferay-ui="http://portletfaces.org/liferayfaces/liferay-ui"
      xmlns:liferay-util="http://portletfaces.org/liferayfaces/liferay-util"
      xmlns:ui="http://java.sun.com/jsf/facelets"
> 
      <h:body>

    <h1>JSF 2 param example</h1>

      <h:form id="form">

      Enter your Name :
      <h:inputText size="10" value="#{user.name}" /><br/><br/>
      Enter your LastName:
      <h:inputText value="#{user.lastname}"/>
      <br /><br />

      <h:commandButton id="submitButton"
            value="Submit - US" action="#{user.outcome}">

           
            <f:param name="address" value="#{user.lastname}" />
            <f:param name="country" value="United States" />

      </h:commandButton>


      </h:form>

    </h:body>
</html>

Step 7) Right click on xhtml folder,-> go to new ->file ->create new file name under xhtml folder with .xhtml extension, add this code into this file
<?xml version="1.0"?>

<html
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:icecore="http://www.icefaces.org/icefaces/core"
      xmlns:liferay-ui="http://portletfaces.org/liferayfaces/liferay-ui"
      xmlns:liferay-util="http://portletfaces.org/liferayfaces/liferay-util"
      xmlns:ui="http://java.sun.com/jsf/facelets"
> 
     

    <h:body>

    <h1>JSF 2 param example</h1>

     <h3>
     <h:outputFormat value="Hello,{0}{1}. You are from {2}.">
       <f:param value="#{user.name}" />
       <f:param value="#{user.lastname}"/>
       <f:param value="#{user.country}" />
       
     </h:outputFormat>
     </h3>

    </h:body>
</html>


Step 8) Now you have change the portal.xml file
           change the <name> tag under <init-param>tag 
        change the <name> tag
             <name>javax.portlet.faces.defaultViewId.view</name>

Step 9)Now deploy your Project and start your server,enter your name and last name click on submit button,
Step 10)You will see the output like this