开发者

Adding portlet instance during page reload in Liferay

开发者 https://www.devze.com 2023-02-09 04:58 出处:网络
I am trying to add a new IFrame portlet in liferay on click of a link. I have achieved it with some issues.

I am trying to add a new IFrame portlet in liferay on click of a link. I have achieved it with some issues.

First I created a portlet using liferay sdk and customized the default view.jsp:

<portlet:renderURL var="pageDisp">
    <portlet:param name="jspPage" value="/view.jsp" />
</portlet:renderURL>
<a href="<%=pageDisp%>&clink=g">Google</a> <br />
<a href="<%=pageDisp%>&clink=y">Yahoo</a> <br />
<a href="<%=pageDisp%>&clink=d">Daily Tech</a> <br />
<a href="<%=pageDisp%>&clink=w">Wired</a> <br />

Changed portlet.xml from

<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

to

<portlet-class>test.LinksPortlet</portlet-class>

I have implemented the following code in LinksPortlet:

public void render(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
        log("LinksPortlet:render");
        try {
            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(c开发者_Python百科om.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY);
            LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
            List<Portlet> portletList = layoutTypePortlet.getPortlets();
            for(Portlet p : portletList) {
                log(p.getPortletId());
                log(p);
                log(p.getDisplayName());
                log(p.getInitParams());
            }
            String c = request.getParameter("clink");
            log(c);
            if(c != null && (c.equals("g") || c.equals("y") || c.equals("d") || c.equals("w"))) {
                String portletId = layoutTypePortlet.addPortletId(Long.parseLong(request.getRemoteUser()), PortletKeys.IFRAME, "column-2", -1);
                log("portletId: "+portletId);
                long companyId = themeDisplay.getCompanyId();
                long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
                int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
                Layout layout = themeDisplay.getLayout();
                PortletPreferences prefs = PortletPreferencesLocalServiceUtil.getPreferences(companyId, ownerId, ownerType, layout.getPlid(), portletId);
                Locale locale = new Locale("en", "US");
                prefs.setValue("portlet-setup-use-custom-title", "true");

                if(c.equals("g")) {
                    prefs.setValue("src","http://www.google.com");
                    prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Google");
                }
                else if(c.equals("y")) {
                    prefs.setValue("src","http://www.yahoo.com");
                    prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Yahoo");
                }
                else if(c.equals("d")) {
                    prefs.setValue("src", "http://www.dailytech.com");
                    prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Daily Tech");
                }
                else if(c.equals("w")) {
                    prefs.setValue("src", "http://www.wired.com");
                    prefs.setValue("portlet-setup-title-" + LocaleUtil.toLanguageId(locale), "Wired");
                }

                PortletPreferencesLocalServiceUtil.updatePreferences(ownerId, ownerType, layout.getPlid(), portletId, prefs);

                portletList = layoutTypePortlet.getPortlets();
                for(Portlet p : portletList) {
                    if(p.getPortletId().equals(portletId)) {
                        break;
                    }
                }

                LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
            }
        }
        catch(Exception e) {
            log("LinksPortlet:doView:Exception");
            e.printStackTrace();
        }

        super.render(request, response);
    }

The issue with the above implementation is that the new portlet appears on the screen after the page reload: http://www.go-ahead.in/books/1.JPG http://www.go-ahead.in/books/2.JPG http://www.go-ahead.in/books/3.JPG

The reason for the issue is that the new portlet is added after the layout render starts. Is it possible to customize or add a hook for performing the action I am performing during page / layout load. I came across the source LayoutAction.java of liferay where portlets are added. Is there any way to add a post hook to add the portlet without modifying the liferay's source code?

I am using Liferay 6.0.5.

Thanks in advance.


Found the solution to the problem.

I have moved the code from render method to processAction and creating the link using <portlet:actionURL> tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消