简体   繁体   English

Java:从Web内容Portlet获取文章ID

[英]Java : Getting the article id from a web content portlet

I have two portlets on my web page : 我的网页上有两个portlet:

The first one is a web content portlet that allows picking up an article and displays it. 第一个是Web内容Portlet,它允许提取文章并显示它。

The other one is the portlet I'm working on (Struts MVC). 另一个是我正在使用的portlet(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. 我想在第二个portlet中做的是获取用于在第一个portlet中显示web内容的文章id。

Is it possible ? 可能吗 ?

Thank you! 谢谢!

You can do it using some Liferay specific APIs, though the approach is not perfect, but it'll work. 您可以使用一些Liferay特定的API来完成它,虽然这种方法并不完美,但它会起作用。

You can use Liferay APIs to get list of portlets currently available on page. 您可以使用Liferay API获取当前可用的portlet列表。 Then you can figure out by portlet IDs which portlets are of type WebContentDisplay. 然后,您可以通过portlet ID找出哪些portlet属于WebContentDisplay类型。 Then you can read their preferences and there will be the ID of WebContent Article they display. 然后,您可以阅读他们的首选项,并将显示他们显示的WebContent文章的ID。

Note however that there can be cases when you have more then one WebContent Display portlet on page, or have none of them. 但是请注意,在某些情况下,页面上可能会有一个以上的WebContent Display portlet,或者一个都不包含。 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. 您可以在每个渲染器的页面上阅读portlet列表,也可以创建配置页,在其中可以显示选择框,供站点管理员选择应从中采用该值的WebContent Display Portlet实例。

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. 是的,您可以通过在会话中进行设置来在两个不同的Portlet之间共享数据。 Set the article ID by editing the portlet code (of 1st portlet), set it in the session and retrieve it in you portlet. 通过编辑(第一个Portlet的)Portlet代码来设置文章ID,在会话中进行设置并在您的Portlet中检索它。

For setting and getting values (Inter portlet communication) example--> Check this 有关设置和获取值(内部portlet通信)示例 - > 检查此项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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