Total Pageviews

Showing posts with label portlets. Show all posts
Showing posts with label portlets. Show all posts

Sunday, April 1, 2012

Using Velocity Templates in Portlets


In the very beginning when I was introduced to Velocity Templates, it was very confusing. But with the passing time, I came to know the importance and now I love to work with them. 
We've used them very often in themes, in fact its an important part of theme.
So story seems quite straight, now lets get a twist. 

What If we want to use them in portlet ???
And in answer, you'll ask me a question why the hell you want do it, if we have JSPs to serve the purpose ?

hmmm !!! You are right... Yea, we have JSPs and they work very well, no doubt... But the thing I'm talking about is related with heavy data rendering and the advantage is - we can process them in action class itself instead of sending them to JSP in response for further processing  for display. We get faster processing, as full rendered HTML is sent to JSP in response. So, in that condition we just need to put that response for output in JSP. Ambigious, huh... trust me, it will be very transparent after you finish this post...

Lets start, do create a portlet, depoy it and add on a page to see default output. Now complex climax of the story begins.
By default, the portlet we create is MVC portlet and use default MVCPortlet class. Now do the following:
1. At First create a custom class and it should inherit com.liferay.util.bridges.mvc.MVCPortlet class.
2. We have to override the doView method where we will write our code for Velocity Engine initialization. This is main task. Lets do it. 
In addition i've written one more function, just for modularization, in which I'm merging velocity template, as under:
3. Now you are done with java. Create a folders inside docroot for keeping templates and JSPs with relevent names. I've created with name VelocityTemplate and HTML respectively. Now create and template in side the VelocityTemplate folder and write some HTML stuff and create your JSP with help of following:
4. Now lets start the writing the climex of the story. We have to edit the portlet.xml and liferay-plugin-package.properties file. We have to change the <portlet-class>, and <init-params> for getting template's and JSP's path, like following:
liferay-plugin-package.properties
And trust me, its done... Just try it one or get my attaced war file for referance. Cheers !!!

Saturday, August 27, 2011

A story of "Portlet" and "Servlet"

There are so many things which are very clear and understood but if someone ask, it become difficult to explain them in deep. The topic I am going to write today, is something like that. Hope, you'll enjoy reading it, as I am going to write this time full on text, no practical implementation.


The contents of servlets provide generally takes up the whole page in a browser (if you are not framing them), and portlets content are wrapped in a window and we can place multiple portlets on same page with one another and each can provide different content and functionality. A portlet behaves like a full fledged application. The portal can provide some house keeping functionality and secured single point of entries to all of the portlets on a page. 


lets see first some differences among portlets and servlets
  1. Servlets can provide complete web pages, whereas portlets only provide fragments. These fragments are then aggregated to form a complete web page by the portal.
  2. Portlets aren't allowed to generated HTML code that contains tags such as base, body, frame, frameset, head, html, or title. The iframe tag can be used with caution.
  3. The user cannot access a portlet directly using a URL in the way that a servlet is accessed. Instead, the URL points to the page containing all of the portlets on one page.
  4. Communication between the web client and the portlets is performed through the portal.
  5. Portlets can be provided with buttons or controls to manipulate the portlet's window states or portlet modes. 
  6. Multiple instances of a single portlet can be placed onto the same page.
  7. Portlets support persistent configuration and customization.
  8. Portlets also support user profile information.
  9. Portlets support two scopes within the session; application scope and portlet scope .
Here are some similarities
  1. Servlets and portlets are web based components that utilize Java for their implementation.
  2. Portlets are managed by a portlet container similar to a servlet container.
  3. Both of these components generate content, which can be static or dynamic.
  4. Both portlets and servlets have a lifecycle that is controlled by the container.
  5. The client/server model is used for both servlets and portlets.
  6. The packaging and deployment are essentially the same.
  7. The manner in which the classes are loaded and the class loaders that perform the work are also the same.
  8. Lifecycle management is similar.
  9. The Request and Response semantics are also similar.
Advantage of Servlets over portlets-
  1. Portlet aren't allowed to set the character set encoding of the response.
  2. Portlet also aren't allowed to set the HTTP headers on the response.
  3. Portlet cannot manipulate the URL of the client request to the portal.
Advantage of Portlets over Servlets-

  1. Portlet Modes :  Each portlet has a current mode, which indicates the function the portlet is performing.
  2. All JSR 168 compliant portals should support the View, Edit and Help modes.
  3. Window States :Window states indicate the amount of portal page space that will be assigned to a portlet.
  4. Portlet Preferences :Portlets can be configured to provide a custom view or behavior for different users.