and continuing 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.

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

Java

1

publicvoidaddBook(ActionRequest request,ActionResponse response){

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 .

XHTML

1

<aui:form action="<%=addBookURL.toString() %>"method="post"name="fm">

Just like actionURL , we also have renderURL .

XHTML

1

2

3

4

<liferay-portlet:renderURL var=”successURL”>

<portlet:param name=”jspPage”value=”success.jsp” />

<liferay-portlet:renderURL>

<a href=”<%=successURL.toString()%>”>Success</a>

It basically renders a jsp or an HTML page.

XHTML

1

portlet:paramname=”jspPage”value=”success.jsp” />

For rendering any JSP , MVCPortlet framework , has the above syntax.
For rendering after completing an action , we have

Java

1

response.setRenderParameter(“jspPage”,”jspLocation”);

Calls to these functions , are followed by execution of the

Java

1

publicvoidrender(RenderRequest request,RenderResponse response)

function internally.

BooksActionUtil.class

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

Java

1

2

Books book=BooksActionUtil.getBookFromRequest(request);

booleanbookValid=BooksValidator.validateBook(book,errors);

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

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.

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

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<%

Calendar dop=CalendarFactoryUtil.getCalendar();

dop.setTime(book.getDateOfPurchase());

%>

label="Date of Purchase"name="dateOfPurchase"value="<%= dop %>"/>

//And here is how you would retrieve the Date object,

nt dopMonth=ParamUtil.getInteger(request,"dateOfPurchaseMonth");

intdopDay=ParamUtil.getInteger(request,"dateOfPurchaseDay");

intdopYear=ParamUtil.getInteger(request,"dateOfPurchaseYear");

if(Validator.isDate(dopMonth,dopDay,dopYear)){

try{

book.setDateOfPurchase(PortalUtil.getDate(dopMonth,dopDay,

dopYear,newPortalException()));

}catch(PortalException ex){

/* Setting to Null Will enable us to catch it in the Validator */

book.setDateOfPurchase(null);

}

}else{

book.setDateOfPurchase(null);

}

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 n xml file where we can set restrictions on the datatypes
portlet-model-hints.xml

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?xml version="1.0"encoding="UTF-8"?>

<model-hints>

<model name="com.books.database.model.Books">

<field name="bookId"type="long" />

<field name="title"type="String" />

<field name="author"type="String" />

<field name="dateOfPurchase"type="Date">

<hint name="year-range-delta">90</hint>

<hint name="year-range-future">true</hint>

<hint name="show-time">false</hint>

</field>

<field name="pages"type="int" />

<field name="userId"type="long" />

<field name="companyId"type="long" />

<field name="groupId"type="long" />

</model>

</model-hints>

We can also restrict the lenth of certain fields using

XHTML

1

<hint name=”max-length”>4000</hint>

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 :

Java

1

Stringtitle=ParamUtil.getString(request,"title","Title Not Availible");

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.

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

Note : Make sure that the property matches the exact property name given in service.xml

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 ,

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

packagecom.books.util;

importjava.util.*;

publicclassBooksBean{

privatelongbookId;

privateStringtitle;

privateStringauthor;

privateDate dop;

privateintpages;

privatelonguserId;

privatelongcompanyId;

privatelonggroupId;

publiclonggetBookId(){

returnbookId;

}

publicvoidsetBookId(longbookId){

this.bookId=bookId;

}

publicStringgetTitle(){

returntitle;

}

publicvoidsetTitle(Stringtitle){

this.title=title;

}

publicStringgetAuthor(){

returnauthor;

}

publicvoidsetAuthor(Stringauthor){

this.author=author;

}

publicDate getDop(){

//Write your Own Simple Date Formatter Code Here

returndop;

}

publicvoidsetDop(Date dop){

this.dop=dop;

}

publicintgetPages(){

returnpages;

}

publicvoidsetPages(intpages){

this.pages=pages;

}

publiclonggetUserId(){

returnuserId;

}

publicvoidsetUserId(longuserId){

this.userId=userId;

}

publiclonggetCompanyId(){

returncompanyId;

}

publicvoidsetCompanyId(longcompanyId){

this.companyId=companyId;

}

publiclonggetGroupId(){

returngroupId;

}

publicvoidsetGroupId(longgroupId){

this.groupId=groupId;

}

}

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

Checking permission in liferay is just a matter of calling a function .

permissionChecker.hasPermission(groupId, name,bookId,"DELETE") ;

This function , is given the groupId , the name of the class , the primary key of the class and the type of Action supported.
We have defined all this in the default.xml file under the resource-actions folder.
The above permissionChecker , simply passes on these parameters and checks it against the permissions given to the particular user . If the user has permission it will return true , else false.
We have used the permission checker to toggle the Delete functionality for different users . Any user having role for which delete functionality has been granted permission . will see the delete icon , else wont.
Here is a screen shot displaying the permissionChecker in action , with 2 images of the portlet on before log-in and one after log-in.

hi There I’ve followed all of your instruction but all I got is error when deploying…and after I’m download your book-portlet it’s still error the same error

03:11:47,536 ERROR [HotDeployUtil:112] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering portlets for Books-portletcom.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering portlets for Books-portlet at com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener.throwHotDeployException(BaseHotDeployListener.java:45) at com.liferay.portal.deploy.hot.PortletHotDeployListener.invokeDeploy(PortletHotDeployListener.java:104) at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._doFireDeployEvent(HotDeployUtil.java:109) at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._fireDeployEvent(HotDeployUtil.java:182) at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil.java:38) at com.liferay.portal.kernel.servlet.PortletContextListener.doPortalInit(PortletContextListener.java:99) at com.liferay.portal.kernel.util.BasePortalLifecycle.portalInit(BasePortalLifecycle.java:42) at com.liferay.portal.kernel.util.PortalLifecycleUtil.register(PortalLifecycleUtil.java:52) at com.liferay.portal.kernel.util.BasePortalLifecycle.registerPortalLifecycle(BasePortalLifecycle.java:50) at com.liferay.portal.kernel.servlet.PortletContextListener.contextInitialized(PortletContextListener.java:55) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467) at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1244) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1342) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)

hi there now I get This Error 05:53:40,014 ERROR [PortletRequestDispatcherImpl:107] org.apache.jasper.JasperException: /jsps/view.jsp(1,1) Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLVorg.apache.jasper.JasperException: /jsps/view.jsp(1,1) Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standard.tlv.JstlCoreTLV at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40) at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407) at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)

Hi , I am using search container to search the values its working , but now my need is that .in my first column text i want to link another page ,like when someone click on that text the new popup window will open in editable form …can you please help me for this …

hi i have a problem when i click in the button add book :com.liferay.portal.ResourceActionsException: There are no actions associated with the resource com.Books.database.model.Books at com.liferay.portal.service.impl.ResourceLocalServiceImpl.validate(ResourceLocalServiceImpl.java:927)

can anybody please help me to create a liferay portlet in which we can use/call the code of spring mvc using hibernate annotaions (means we are able to call directly @request mapping url of controller)with any good example/tutorial.

com.liferay.portal.NoSuchResourceActionException: com.books.database.model.Books#DELETE at com.liferay.portal.service.impl.ResourceActionLocalServiceImpl.getResourceAction(ResourceActionLocalServiceImpl.java:80) at sun.reflect.GeneratedMethodAccessor200.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

Please help me to jolt out the problem. It will be greatly appriciated.

So, what do you think ?

Who Am I ?

Rohit Salecha is a Technology enthusiast with over 6+ years of experience in Information Security and Allied fields
including Vulnerability Assessment, Penetration Testing, Risk Assessment, Technology Evaluation, Information Security
Project Management, Solution Designing and Architecture, Third Party Vendor Audits and Vendor Management.
You can reach out to him using any of the channels below in relation to any of the above topics.
Happy To Help !