Tuesday, 5 June 2012

How to create Simple Spring portlet in liferay


Step 1)create spring portlet called Springdemo
Step 2) put all this jar file into your lib folder under WEB-INF/lib

 Jstl.jar
 org.springframework.asm-3.0.0-RC1.jar
org.springframework.context-3.0.0-RC1.jar
org.springframework.beans-3.0.0-RC1.jar
org.springframework.core-3.0.0-RC1.jar
org.springframework.expression-3.0.0-RC1.jar
org.springframework.web.portlet-3.0.0-RC1.jar
org.springframework.web.servlet-3.0.0-RC1.jar
org.springframework.web-3.0.0-RC1.jar
standared.jar

Step 3) create one .xml file with name of your portlet in WEB-INF folder, put this code in the .xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        ">



<bean id="helloWorldController"                                        
   class="com.liferay.test.HelloWorldController"/>

 <bean id="portletModeHandlerMapping"                                  
  class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
 
  <property name="portletModeMap">
   <map>
    <entry key="view">                                                  
     <ref bean="helloWorldController" />                                
    </entry>                                                            
   </map>
  </property>
</bean>

 <bean id="viewResolver"                                                
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView"/>
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>
</beans>
Step 4)  Edit the portlet.xml file change the <portlet-class> tag
   <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
Step5)Edit the web.xml File put this code into web.xml
<servlet>
  <servlet-name>ViewRendererServlet</servlet-name>
  <servlet-class>
    org.springframework.web.servlet.ViewRendererServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>ViewRendererServlet</servlet-name>
  <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
Step 6) Now Create class under src folder com/liferay/test called HelloWorldController,put this code into this class









import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.Controller;

public class HelloWorldController implements Controller{


 public void handleActionRequest(ActionRequest request,
    ActionResponse response)throws Exception {
 }


 public ModelAndView handleRenderRequest
  (RenderRequest request, RenderResponse response)
 throws Exception {


   Map<String, Object> model =
      new HashMap<String,Object>();
model.put("helloWorldMessage", "This is Hello World Spring Portlet!!!!!!");
return new ModelAndView("helloWorld", model);
}
}

Step 7)Now build the service and deploy the portlet

Create Spring Portlet in Liferay


Step 1)Create spring portlet called NewSpring portlet go to ->File->New->Liferay Project
Step 2)right click on your project, create  Liferay Service Builder


Step 3) write this code into your service builder
  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.liferay.test">
<author>azazdesai</author>
<namespace>New</namespace>

<entity name="Film" local-service="true" remote-service="true">
<!-- primary key -->
<column name="filmId" type="long" primary="true" />
<!-- form fields -->

<column name="filmTitle" type="String" />
<column name="director" type="String" />
<column name="cast" type="String" />

<column name="userId" type="long" />
<column name="groupId" type="long" />
<!-- Order -->

<order>
<order-column name="director" order-by="asc" />
</order>

<!--  finders -->
<finder name="UserId" return-type="Collection">
<finder-column name="userId" />
</finder>

<finder name="GroupId" return-type="Collection">
<finder-column name="groupId" />
</finder>
</entity>
<exceptions>
<exception>InvalidFilm</exception>
</exceptions>
</service-builder>


Step 4)put all this jar file into your lib folder under WEB-INF/lib
  Jstl.jar
 org.springframework.asm-3.0.0-RC1.jar
org.springframework.context-3.0.0-RC1.jar
org.springframework.beans-3.0.0-RC1.jar
org.springframework.core-3.0.0-RC1.jar
org.springframework.expression-3.0.0-RC1.jar
org.springframework.web.portlet-3.0.0-RC1.jar
org.springframework.web.servlet-3.0.0-RC1.jar
org.springframework.web-3.0.0-RC1.jar
standared.jar
Step 5)create one .xml file with name of your portlet in WEB-INF folder, put this code in the .xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        ">
<context:component-scan base-package="com.liferay" />

<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="myPropertyEditorRegistrar" />
</list>
</property>
</bean>
</property>
</bean>






<bean id="myPropertyEditorRegistrar" class="com.liferay.test.MyPropertyEditorRegistrar"/>

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language-ext</value>
</list>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Step 6) Edit the portlet.xml file change the <portlet-class> tag
   <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>

Step7)Edit the web.xml File put this code into web.xml
  <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>


Step8)Now Create class under src folder com/liferay called FilmProfileController,put this code into this class
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.test.model.Film;
import com.liferay.test.model.impl.FilmImpl;
import com.liferay.test.service.FilmLocalServiceUtil;



@Controller("filmController")
@RequestMapping(value = "VIEW")
public class FilmProfileController {


@RenderMapping
public String showFilms(RenderResponse response) {
System.out.println("inside show films......");
return "home";
}



    @ModelAttribute
    public Film newRequest(@RequestParam(required=false) Long filmId) {
    try{
Film film = new FilmImpl();
if(filmId!=null){
film = FilmLocalServiceUtil.getFilm(filmId);
}
        return film;
    }catch(Exception e){return new FilmImpl();}
    }


@RenderMapping(params = "myaction=addFilmForm")
public String showAddFilmForm(RenderResponse response) {
return "addFilmForm";
}

@RenderMapping(params="myaction=editFilmForm")
public String showEditBookForm() {
return "editFilmForm";
}

@ActionMapping(params = "myaction=deleteFilmForm")
public void deleteFilmForm(@ModelAttribute Film film,ActionResponse response) {
System.out.println("Inside delete film ...............");
try {
FilmLocalServiceUtil.deleteFilmProfile(film.getFilmId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@ActionMapping(params = "myaction=addFilm")
public void addFilm(@ModelAttribute Film film,
BindingResult bindingResult, ActionResponse response, SessionStatus sessionStatus) {
System.out.println("Inside add film ...............");
try {
FilmLocalServiceUtil.create(film);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setRenderParameter("myaction", "books");
sessionStatus.setComplete();
}
}

Step 9)create another class called  LongNumberEditor under src com/liferay/test,put this code into this class file
import java.beans.PropertyEditorSupport;

public class LongNumberEditor extends PropertyEditorSupport
{
@Override
public String getAsText() {
String returnVal = "";
if(getValue() instanceof Long) {
returnVal = String.valueOf((Long)getValue());
}
if(getValue() != null && getValue() instanceof String[]) {
returnVal = ((String[]) getValue())[0];
}
return returnVal;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Long.valueOf(text));
}
}

Step 10)  create another class called  MyPropertyEditorRegistrar under src com/liferay/test,put this code into this class file
import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar
{
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Long.class, new LongNumberEditor());
}

}
Step 11)Edit the FilmLocalServiceImpl.class file put this code into the Impl file
public Film create(long userId,long groupId,String director, String cast) throws SystemException {

long filmId = counterLocalService.increment();

Film film = filmPersistence.create(filmId);

film.setUserId(userId);
film.setGroupId(groupId);
film.setDirector(director);
film.setCast(cast);
//film.setYear(year);

//film.setRating(rating);

// film.setLanguage(language);
//film.setComments(comments);
//film.setViews(views);


filmPersistence.update(film, false);

return film;
}

public Film create(Film film) throws SystemException {

long filmId = counterLocalService.increment();

//Film film = filmPersistence.create(filmId);
film.setFilmId(filmId);

filmPersistence.update(film, false);
return film;
}




public List<Film> getFilmData(long userId)throws Exception
{
return filmPersistence.findByUserId(userId);
}

public List<Film> getCompanyFilm(long groupId)throws Exception
{
return filmPersistence.findByGroupId(groupId);
}


public int getCompanyFilmCount(long groupId)throws Exception
{
return filmPersistence.countByGroupId(groupId);
}

public void deleteFilmProfile(long filmId)throws Exception
{

filmLocalService.deleteFilm(filmId);
}
}

Step 12)Now build the service and deploy the portlet








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