Programmatically Add Portlet (程序控制添加portlet)

来源:互联网 发布:出境游数据 编辑:程序博客网 时间:2024/05/17 23:18

一 问题描述

 本文讨论的是如何程序地添加portlet,实现的效果类似点击 Add 出现portlet。

二 准备

 1.获得portletId,我知道有两种方式获得portletId。

         1).直接去数据库中portlet表中直接查。PortletId命名的结构为portletname_WAR_ProjectName。

         2).在要弹出的portlet上点击Option->look and feel->advance styling 显示的portlet ID

            为 #p_p_id_testportlet_WAR_ProgrammaticallyAddPortletportlet_实际的portlet ID为testportlet_WAR_ProgrammaticallyAddPortletportlet

 

三 解决方法

  解决方法的实现主要是使用LayoutTypePortlet类的addPortletId方法和LayoutLocalServiceUtil的updateLayout方法,,程序地添加portlet的代码可以写在jsp页面上,也可以写在java文件里。

(一)写在jsp页面中

代码如下

<%@ page import="com.liferay.portal.model.LayoutConstants"%>

<%@ page import="com.liferay.portal.service.LayoutLocalServiceUtil"%>

<theme:defineObjects />

<%

layoutTypePortlet.addPortletId(user.getUserId(),"testportlet_WAR_ProgrammaticallyAddPortletportlet","column-2",1,false);

LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());

%>

代码解释

 <theme:defineObjects />这个标签提供几个常用的变量。其中就有我们需要的layoutTypePortlet,具体的在liferay in action 书中的101页有详细的介绍。(在书中标签为<liferay-theme:defineObjects />,在我的环境中不识别,我在Snippets下选择 Taglib inports 的liferay Theme Taglib Import)

剩下的两个函数的原型为:

public String addPortletId(

            long userId, StringportletId, String columnId,int columnPos,

            boolean checkPermission)

        throws PortalException,SystemException;

publicstaticcom.liferay.portal.model.LayoutupdateLayout(long groupId,

        boolean privateLayout,long layoutId,java.lang.String typeSettings)

(二)写在java文件中

1.在jsp 页面添加一个actionURL和一个超链接、

<portlet:actionURL var="addportlet" >

</portlet:actionURL>

<aui:a href="<%=addportlet %>">addPortlet</aui:a>

2.portlet所对应的类文件里添加

    @Override

    publicvoidprocessAction(ActionRequest actionRequest,

            ActionResponseactionResponse)throwsIOException, PortletException {

        //TODO Auto-generated method stub

           try {

            addPortlet("testportlet_WAR_ProgrammaticallyAddPortletportlet",actionRequest,actionResponse);

        }catch(PortalException e) {

            //TODO Auto-generated catch block

            e.printStackTrace();

        }catch(SystemException e) {

            //TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

publicvoidaddPortlet(String portletId,ActionRequest actionRequest,

            ActionResponseactionResponse)throwsIOException, PortletException, PortalException, SystemException {

        //TODO Auto-generated method stub

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

         

         Layout layout=themeDisplay.getLayout();

         LayoutTypePortletlayoutTypePortlet=(LayoutTypePortlet) layout.getLayoutType();

         layoutTypePortlet.addPortletId(themeDisplay.getUserId(),portletId,"column-2",1,false);

         LayoutLocalServiceUtil.updateLayout(layout.getGroupId(),layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());

    }



原创粉丝点击