Java:从Web内容Portlet获取文章ID [英] Java : Getting the article id from a web content portlet

查看:90
本文介绍了Java:从Web内容Portlet获取文章ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有两个portlet:

I have two portlets on my web page :

第一个是Web内容portlet,它允许提取文章并将其显示.

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

另一个是我正在使用的portlet(Struts MVC). 我要在第二个Portlet中执行的操作是获取用于在第一个Portlet中显示Web内容的文章ID.

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.

有可能吗?

谢谢!

推荐答案

您可以使用某些Liferay特定的API来完成此任务,尽管这种方法并不完美,但可以使用.

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

您可以使用Liferay API获取页面上当前可用的portlet列表.然后,您可以通过portlet ID来确定哪些portlet的类型为WebContentDisplay.然后,您可以阅读他们的首选项,并显示他们所显示的WebContent文章的ID.

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.

但是请注意,在某些情况下,页面上可能只有一个WebContent Display portlet,或者没有一个.您可以在每个渲染器的页面上阅读portlet列表,也可以创建一个配置页,在其中可以显示一个选择框,供站点管理员选择应从中采用该值的WebContent Display Portlet实例.

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);
    }

}

这篇关于Java:从Web内容Portlet获取文章ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆