Normally we add a page, change layout adding a portlet on a particular page by using drag and drop or by clicking our mouse.Today we will add a page,Change Layout from 2 to 3 and than finally add a portlet on newly created page Programmatically.
1)LayoutLocalServiceUtil
2)LayoutTypePortlet
Step 1 :- Create Liferay project and Generic Portlet in it.
Explanation:-
Explanation:-
Explanation:-> Here first we again get the Layout(ie page) Object on which we have to add the portlet by providing friendlyUrl and other values.
For doing this we basically focus on two classes:-
1)LayoutLocalServiceUtil
- Because page means layout in liferay thats why we use LayoutLocalServiceUtil
- For using method of LayoutLocalServiceUtil we need userID , groupID so we also need ThemeDisplay Object.
2)LayoutTypePortlet
Object of this Class is used to programmatically add or remove a portlets from a page.This object can be get in jsp by <liferay-theme:defineObjects />
Ok Lets discuss step by Step
Step 1 :- Create Liferay project and Generic Portlet in it.
Create a liferay project then create a generic portlet in it . Copy this code inside your view.jsp
view.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL var="addPage" name="addPageAction"></portlet:actionURL> | |
<portlet:actionURL var="updatePageLayout" name="updatePageLayout"></portlet:actionURL> | |
<portlet:actionURL var="addPortlet" name="addPortlet"></portlet:actionURL> | |
<a href="<%=addPage %>">Adding Page</a><br> | |
<a href="<%=updatePageLayout %>">Update Page Layout</a><br> | |
<a href="<%=addPortlet %>">Add Loan Calculator</a> |
Explanation:-
Here we create just 3 action URL that hit 3 different methods on click.
Step 2 :- Adding a Page.
On Clicking Adding Page addPage method is called which is responsible for adding a page in Liferay.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ProcessAction(name="addPageAction") | |
public void addPageAction(ActionRequest request, ActionResponse response)throws PortletException{ | |
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY) | |
long userId = themeDisplay.getUserId(); | |
long groupId = themeDisplay.getScopeGroupId(); | |
boolean privateLayout = false;// private page or public page | |
long parentLayoutId = 0; | |
String name = "Dynamic"; | |
String title = "Inside SEO HTML Title"; | |
String description = "Inside SEO Description"; | |
String type = LayoutConstants.TYPE_PORTLET; | |
boolean hidden = false; | |
String friendlyURL = "/dynamic"; | |
ServiceContext serviceContext = new ServiceContext(); | |
try | |
{ | |
LayoutLocalServiceUtil.addLayout(userId, groupId, privateLayout, parentLayoutId,name,title,description, type, hidde,friendRL,serviceContext); | |
} | |
catch (PortalException e) | |
{ | |
e.printStackTrace(); | |
} | |
} |
Here we use addLayout method of LayoutLocalServiceUtil Class which take parameters as:-
a)UserId:- Current User Id .
b)GroupId:- Group id from which user is belong.
c)Private Layout:- The page added as a public(false) page or private(true) page.
d)name:- Name of the page appears in menu.
e)title:-That is show on tabs of browser.
f)description:- description about the page.
g)type:- type of page .ex- Panel ,embedded etc.
i)hidden:- page is shown in menu bar or not.
j)friendlyUrl:- the friendly Url for the page.
After Clicking the link just Reload the page will appear in menu bar.
Step 3 :-Change Page Layout
By default the page is added with 2 column .If you want to change from 2 column layout to 3 column layout click on update page Layout link.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ProcessAction(name="updatePageLayout") | |
public void updatePageLayout(ActionRequest request, ActionResponse response)throws PortletException{ | |
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); | |
long groupId = themeDisplay.getScopeGroupId(); | |
String friendlyURL = "/dynamic"; | |
boolean privateLayout = false; | |
long userId = themeDisplay.getUserId(); | |
try { | |
Layout layout =LayoutLocalServiceUtil.getFriendlyURLLayout(groupId, privateLayout, friendlyURL); | |
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType(); | |
layoutTypePortlet.setLayoutTemplateId(userId, "3_columns"); | |
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.getPrivateLayout(),layout.getLayoutId(), layout.getTypeSettings()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
>Here first we get the Layout(ie page) Object for which we have to change the layout by providing friendlyUrl and other values.
>Then we get the LayoutTypePortlet Object and then use setLayoutTempelateId method.
>Other values rather than "3_columns" can be found (\ROOT\layouttpl\custom)
Step 4:- Adding Portlet on Page
Now we have a page with 3 Column layout.Now we add Loan Calculator portlet on the page. Just Click the link Add Loan Calculator this will call the addPortlet method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ProcessAction(name="addPortlet") | |
public void addPortlet(ActionRequest request, ActionResponse response)throws PortletException{ | |
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); | |
long groupId = themeDisplay.getScopeGroupId(); | |
String friendlyURL = "/dynamic"; | |
boolean privateLayout = false; | |
long userId = themeDisplay.getUserId(); | |
try { | |
Layout layout =LayoutLocalServiceUtil.getFriendlyURLLayout(groupId, privateLayout, friendlyURL); | |
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType(); | |
layoutTypePortlet.addPortletId(userId, "61", "column-1", -1); | |
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.getPrivateLayout() ,layout.getLayoutId() , layout.getTypeSettings()); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} |
>Then we use the method addPortletId which take 4 parameters as:-
a)UserId:- Current User Id .
b)PortletId:- id of the portlet which is to be added on page.
c)ColoumnId:-In which column you want to add your portlet. Ex- column-1 hence portlet added at LHS if column-1 then portlet added at centre and if column-3 portlet added at RHS.
d)Column Position:- Consider if there is already any other portlet in particular column.
0>=:-Our portlet is added at the top of existing portlet.
-1 :-Our portlet is added at the bottom of existing portlet.
>Finally we update the layout
you can download source code from Here
Hope this will help....
you can download source code from Here
Hope this will help....
Related Post:-