Sunday, October 16, 2011

Liferay Search Container

MVC Portlet Development : MVC Framework and AlloyUI

What we have done so far ,

Eclipse Installation

Directory Structure

Service Builder - I

Service Builder - II

and lets continue to the last part . The MVC Framework and Alloy UI


Every Portlet on a portal page , will have a different render and request cycle.

In servlet we either implement a doGet or a doPost method which handle the response and the request in the same cycle. But here in the Portlet specification jsr-286 , the request and rendering or response is handled separately.

In the Portlet cycle , we have two main methods processAction and render.

processAction handles all the requests and render handles the response.
When the portlet is loaded for the first time , render is called to load the default view lets say a form.When you fill the form and send it across processAction will be called to get all the form data.Once the processAction is executed you can specify a jsp page where the next event has to be handlded. By default it will go to the home page.

But in the MVCPorrlet interface , all these things have been abstracted for us. We donot need to implement any of these render or processAction methods.

In portlet.xml we specified view-jsp as n initial parameter.This is nothing but the location of the default page that loads when the portlet is loaded for the first time or whenever no action is called.

In MVC Portlet , there is a simple way of sending a request and rendering the response.

Below is the code of a JSP file and its corresponding MVC portlet.









In Portlet , we cannot create our own links, the way we do it in servlets . We need to create URLs to submit a form or to perform some action.

The above code , maps the to the function



in the BooksPortlet class.

The name in actionURL tag is the name of the function that is implemented in the portlet class. The var is just a variable holding the link.

So while creating a form , we specify it in the following manner .



Just like actionURL , we also have renderURL



It basically renders a jsp or an HTML page.


For rendering any JSP , MVCPortlet framework , has the above syntax.

For rendering after completing an action , we have



Calls to these functions , are followed by execution of the
Public void render(RenderRequest request,RenderResponse response) function internally.


BooksActionUtil.class

As you have seen in the BooksPortlet , we have called two functions ,



This is a standard procedure followed by Liferay , to reduce concentrating all the code in one class itself. Like common , it follows more out of practise and common sense. So here , are the two classes , BooksActionUtil.class and BooksValidator.class



BooksVaildator.class



ThemeDisplay



themeDisplay is an object containing all the information about the page, its layout, about the user whether he is logged in or not , certain fixed urls like create account , sign in etc. from themeDisplay we can get information about the company,group,user the particular page is associated with. It’s a very important object.

Here is the documentation of the ThemeDisplay Object and its functions.

Here is the Screen Shot of the portlet that we have created.


DatePicker

The date picker object Is very simple to implement. Here is the implementation code.



For the date picker , it is necessary to provide the Model and the bean attributes to the input tag.

In the src/META-INF folder , we have a xml file where we can set restrictions on the datatypes
portlet-model-hints.xml




We can also restrict the lenth of certain fields using



For more info on model-hints , take a look at the liferay source code ,
portal-impl/src/META-INF/portal-modelt-hints.xml file.

Model-Hints


NOTE : While using the date field , the name attribute of the input tage should match with the name attribute of the field tag in portlet-model-hint.

ParamUtil and PortalUtil.

ParamUtil is a utility class provided by liferay to extract the parameters from the query strings with a lot of added functionality.
For Example :


Here the getString method is having 3 parameters, request object ,the parameter name , and the default parameter if in case there is an error in the connection or the session is timed out.

ParamUtil Documentation .

PortalUtil is also a utility class having some portal related functions

Liferay Also provides a Validator class , which we can use to validate all predefined java primitives and objects.
Life email address,phonenumber , street address , null check etc.

Validator Documentation


Liferay-Search Container

Liferay provides yet another awesome feature of generating a tabular container , with navigation capabilities.
Its also pretty simple to implement. You just needs the list of Object that needs to be displayed.That object can either be Model class or our Own bean class.




In the tag , we first populate the list . It counts the number of elements.

this tag , is used to display the all the information , But we must make sure to give the correct class name and the primary key

Note : Make sure you specify your "className", "keyProperty" name exactly the way you have written in service.xml, It is case-sensitive , and the "modelVar" is just a variable name, You can name it whatever you want just make sure that name doesnt repeat throughout the entire jsp document.



You might come across a situation where the data in your column is not matching the UI requirement , Like for Example , you store the date in 1970,11,11 and now you want to format this date before you put it up on search-container , how to go about that ?

Simple , Just create a new Class , and just to follow proper naming convention , post-fix the class name with "Bean"
Then create instance variables , of the class same as that in service.xml , you can also give them different names

For Example , for the books portlet ,



then refer modelClass=com.package.name.BooksBean and the keyProperty=bookId or whatever you have chosen in your beanClass.

Also while populating the List donot forget to map each property of Bean to the Actual Class



More About Search-container.Click Here


Also look into Liferay Permission Checker.

No comments:

Post a Comment