Friday 10 February 2012

Liferay spring portlet

Hello,
    Last days I was working with spring portlets and want to share some basic knowledge related them.
    To make spring portlet you can create basic MVC Liferay portlet by using Liferay IDE or plugin's ant script and make few steps.



1) Under web.xml add necessary spring configs:

       
       
           contextConfigLocation
           /WEB-INF/application-context.xml
       
       
           log4jConfigLocation
           /WEB-INF/classes/log4j.properties
       
       
           org.springframework.web.context.ContextLoaderListener
       
       
           dispatcher
           org.springframework.web.servlet.DispatcherServlet
           2
       
       
           ViewRendererServlet
           org.springframework.web.servlet.ViewRendererServlet
       
       
           dispatcher
           *.htm
       
       
           ViewRendererServlet
           /WEB-INF/servlet/view
       
2) Uder portlet.xml change portlet class to:
 
   org.springframework.web.portlet.DispatcherPortlet
and replace init-param block to:
 
   
    contextConfigLocation
    /WEB-INF/spring-sceleton-portlet.xml
   
3) Add spring-sceleton-portlet.xml to docroot/WEB-INF directory:
   
    

      

      
        
          
          
          
        
      

     
     
      
       
        com.paul.spring.sceleton.portlet.messages
       
      
     
    
4) Add application-context.xml to docroot/WEB-INF directory:
    
    

     
     
      
      
      
     
    
5) Add dispatcher-servlet.xml to docroot/WEB-INF directory:
   



    
    

6) After it add controller
 package com.paul.spring.sceleton.portlet.controller;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.portlet.bind.annotation.RenderMapping;

 /**
  * View controller.
  *
  * @author Paul
  */
 @Controller("viewController")
 @RequestMapping(value = "view")
 public class ViewController {


   /**
    * Render.
    *
    * @return the string
    */
   @RenderMapping
   public String render() {
     return "view";
   }
 }
and page:
 <%@page contentType="text/html" isELIgnored="false"%>
 <%@page pageEncoding="UTF-8"%>

 <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

 
You can check spring sceleton project in attachment.

More info related spring portlets you can find here.

UPD. Spring portlet with actions and friendly urls

BR,
Paul

3 comments:

  1. Hi, have you managed to get friendly urls working with spring portlet?

    ReplyDelete
    Replies
    1. Yes, check this post java-liferay.blogspot.com/2012/04/spring-portlet-and-friendly-url.html

      Delete
  2. https://www.youtube.com/watch?v=8kvfjRHpBQo

    ReplyDelete