Forums de discussion

Adding portlet instance during page reload in Liferay.

Vikas K, modifié il y a 13 années.

Adding portlet instance during page reload in Liferay.

New Member Publications: 7 Date d'inscription: 07/02/11 Publications récentes
Hi All,

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%>&amp;clink=g">Google</a> <br>
<a href="<%=pageDisp%>&amp;clink=y">Yahoo</a> <br>
<a href="<%=pageDisp%>&amp;clink=d">Daily Tech</a> <br>
<a href="<%=pageDisp%>&amp;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(com.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 &amp;&amp; (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);
    }
</portlet>


The issue with the above implementation is that the new portlet appears on the screen after the page reload:
Step 1
Step 2
Step 3

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 lifray 6.0.5.

Thanks in advance .....
thumbnail
jelmer kuperus, modifié il y a 13 années.

RE: Adding portlet instance during page reload in Liferay.

Liferay Legend Publications: 1191 Date d'inscription: 10/03/10 Publications récentes
You should add the portlet in the action phase. Because the render phase immediately follows the action phase it should work fine.
thumbnail
Ray Augé, modifié il y a 13 années.

RE: Adding portlet instance during page reload in Liferay.

Liferay Legend Publications: 1197 Date d'inscription: 08/02/05 Publications récentes
Not only is that a good point, but in Liferay the page (Layout) is loaded with it's configuration before any portlets ever invoke their render or action. As such, you'll always feel like you have to do one more refresh. So, when modifying the structure of a page, first make sure to perform your operation in the action phase as suggested. BUT you do that so that you can follow up the action with a redirect (which a portlet can only do in the action phase) back to the page.

The reason you redirect is so that the ServicePreAction of the portal (which already fire even before the action phase) can fire again and get the update page settings.

Make sense?
Vikas K, modifié il y a 13 années.

RE: Adding portlet instance during page reload in Liferay.

New Member Publications: 7 Date d'inscription: 07/02/11 Publications récentes
Hi jelmer and Ray,

Thanks for the input. I have managed to solve the problem with your help. emoticon

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