Wednesday, March 18, 2015

[Liferay] Get HTTPServletRequest in portlet

Hello Friends,

In case you submitted need HttpServletRequest in your portlet, here is the code snippet.

HttpServletRequest request = PortalUtil.getHttpServletRequest(renderRequest);
HttpServletRequest originalServletRequest= PortalUtil.getOriginalServletRequest(request);

This goes all the way from your custom code to  PortalImpl  and then PortletRequestImpl to get the request.

After getting request object it goes to find originalServletRequest.


This will be useful when you submitted your form or link without a proper portlet request or want to read a parameter from url.

Hope this helps.

Wednesday, March 4, 2015

[Liferay] Get URL using Portlet ID

Hi,

Sometimes, we need to get url based on portlet id. All we have is a portlet, we can easily get portlet id and then find the page where it is added.

To generate a url like this, we need PortletRequest, portletId, plid, and phase of the portlet.

plid is layout id which can be get like this..

long plid = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), true, portletId); 

In case you don't know how to get themeDisplay,

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

Second argument to method is true which shows that this is private layout. In case of public layoue make it false. We already have portletId. 

We now have plid. Phase of the portlet is as per the requirement, assuming it is render phase for us. So once we have all the things. We call PortletURLFactoryUtil and create the url ( PortletURL )- 

PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);

That's it. Hope this helps.
Until next time. 

[Liferay] Model Listener hook in 6.2

If you are looking for help to create a model listener hook in Liferay 6.2 here are the simple steps to walk you through.

Step 1. Create a simple hook in plugins sdk. Go to plugins-sdk > hooks directory and run the command below.

create journal-article "Journal Article hook"