简体   繁体   中英

Java : Getting the article id from a web content portlet

I have two portlets on my web page :

The first one is a web content portlet that allows picking up an article and displays it.

The other one is the portlet I'm working on (Struts MVC). What I want to do in the second portlet is to get the article id used to display the web content in the first portlet.

Is it possible ?

Thank you!

You can do it using some Liferay specific APIs, though the approach is not perfect, but it'll work.

You can use Liferay APIs to get list of portlets currently available on page. Then you can figure out by portlet IDs which portlets are of type WebContentDisplay. Then you can read their preferences and there will be the ID of WebContent Article they display.

Note however that there can be cases when you have more then one WebContent Display portlet on page, or have none of them. You can either read list of portlets on the page on each render, or you can make a config page where you can display a select box for site admin to choose what WebContent Display Portlet instance should the value be taken from.

Let me show you the code for the first option, and second option if you'll need it I suppose you will deduce how to implement it from given code sample (mind the comments):

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.PortletConstants;
import com.liferay.portal.model.PortletPreferences;
import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;
import java.util.List;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * Portlet implementation class WCDPrefReaderPortlet
 */
public class WCDPrefReaderPortlet extends MVCPortlet {

    public void doView(RenderRequest request, RenderResponse response)
            throws IOException, PortletException {
        // Obtain Liferay's ThemeDisplay object (typical operation in Liferay)
        ThemeDisplay themeDisplay = (ThemeDisplay) request
                .getAttribute(WebKeys.THEME_DISPLAY);

        // Get ID of current page
        long plid = themeDisplay.getPlid();
        try {
            // Obtain portlets on current page as list of
            // com.liferay.portal.model.PortletPreferences
            List<PortletPreferences> pagePortlets = PortletPreferencesLocalServiceUtil
                    .getPortletPreferencesByPlid(plid);
            for (PortletPreferences portlet : pagePortlets) {
                // Portlet ID
                String portletId = portlet.getPortletId();
                // WebContent Display portlet has ID 56. Also it's instanceable,
                // so we expect instance ID to be present, i.e.
                // 56_INSTANCE_NWWDuJPL64xa
                // 56_INSTANCE_N1m7pQGwcScG
                // would be IDs of WebContent Display portlet

                // PortletConstants.getRootPortletId(portletId) will return
                // portlet ID part without instance ID. I.e. we expect just "56"
                if ("56".equals(PortletConstants.getRootPortletId(portletId))) {
                    // If we would have portlet ID stored, we could load it's
                    // preferences using this code:
                    // PortletPreferencesLocalServiceUtil.getPortletPreferences(plid,
                    // portletId);
                    // Not needed for now, since we already have the
                    // corresponding
                    // com.liferay.portal.model.PortletPreferences object

                    // Here we get portlet preferences as XML -
                    // Liferay stores them that way
                    String prefsAsXml = portlet.getPreferences();

                    // Parsing XML and creating Portlet API PortletPreferences
                    // object
                    javax.portlet.PortletPreferences prefs = PortletPreferencesFactoryUtil
                            .fromDefaultXML(prefsAsXml);

                    // Read preference named "articleId" - WebContent Display
                    // Portlet uses this preference to store articleId
                    String articleId = prefs.getValue("articleId", null);

                    // Do something with the articleId value
                    System.out.println(articleId);
                }
            }
        } catch (SystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        super.doView(request, response);
    }

}

Yes, you can share data between two different portlets by setting it in session. Set the article ID by editing the portlet code (of 1st portlet), set it in the session and retrieve it in you portlet.

For setting and getting values (Inter portlet communication) example--> Check this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM