且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过代码添加嵌套的portlet(liferay)

更新时间:2023-12-01 23:42:22

作为完整示例,我假设您想使用另一个portlet操作处理程序将嵌套的portlet添加到当前页面. (如果用于渲染操作,则直到页面的下一个视图都不会看到嵌套的portlet)

for complete example i'll assume that you want to add nested portlet to current page using another portlets action handler. (if used from render action you would not see nested portlet until next view of the page)

将这些方法添加到您的代码中

Add these methods to your code

private static String addPortlet(final long p_userId, final Layout p_layout, final String p_portletId, final String p_columnId, final int p_position, final boolean p_checkPermission)
                throws PortalException, SystemException
{
    if (p_layout.isTypePortlet()) {
        final LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) p_layout.getLayoutType();

        final String portletId = layoutTypePortlet.addPortletId(p_userId, p_portletId, p_columnId, p_position, p_checkPermission);
        if (portletId != null) {
            final String rootPortletId = PortletConstants.getRootPortletId(portletId);
            final String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(p_layout.getPlid(), portletId);
            ResourceLocalServiceUtil.addResources(p_layout.getCompanyId(), p_layout.getGroupId(), 0, rootPortletId, portletPrimaryKey, true, true, true);
            LayoutLocalServiceUtil.updateLayout(p_layout.getGroupId(), p_layout.isPrivateLayout(), p_layout.getLayoutId(), p_layout.getTypeSettings());
        }
        return portletId;
    }

    return null;
}

private static void addNestedPortlet(final PortletRequest p_request) throws PortalException, SystemException {
    final ThemeDisplay themeDisplay = (ThemeDisplay) p_request.getAttribute(WebKeys.THEME_DISPLAY);
    final Layout layout = themeDisplay.getLayout();
    long userId = themeDisplay.getUserId();

    //create nested portlet and add it to "column-1"
    final String nestedPortletId = addPortlet(userId, layout, "118", "column-1", -1, false);

    //this will be used to target nested portlet's columns
    final String nestedColumnPrefix = "_" + nestedPortletId + "__";

    //default page layout (used by nested portlet) has two columns
    //we'll add two portlets (in this example two iframe portlets), one portlet to each column
    addPortlet(userId, layout, "48", nestedColumnPrefix + "column-1", -1, false);
    addPortlet(userId, layout, "48", nestedColumnPrefix + "column-2", -1, false);
}


如果您希望并且可能希望将嵌套的portlet添加到另一个页面或不从portlet添加到另一个页面,则可以查找Layout和User而不是从ThemeDisplay中获取它们.


If you would like, and probably you would, to add nested portlet to another page or not from portlet, you can lookup Layout and User instead of getting them from ThemeDisplay.