有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

liferay Java:从web内容portlet获取文章id

我的网页上有两个portlet:

第一个是web内容portlet,它允许拾取并显示文章

另一个是我正在使用的portlet(Struts MVC)。 我想在第二个portlet中做的是获取用于在第一个portlet中显示web内容的文章id

可能吗

谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    是的,通过在会话中设置数据,可以在两个不同的Portlet之间共享数据。通过编辑portlet代码(第一个portlet的代码)来设置文章ID,在会话中设置它,并在portlet中检索它

    用于设置和获取值(portlet间通信)示例>Check this

  2. # 2 楼答案

    您可以使用一些特定于Liferay的API来实现这一点,尽管这种方法并不完美,但会奏效

    您可以使用Liferay API获取页面上当前可用的Portlet列表。然后,您可以通过portlet ID确定哪些portlet属于WebContentDisplay类型。然后你可以阅读他们的偏好,然后就会看到他们显示的WebContent文章的ID

    但请注意,在某些情况下,页面上可能有多个WebContent Display portlet,或者没有这些portlet。您可以在每个呈现页面上读取Portlet列表,也可以创建一个配置页面,在该页面上可以显示一个选择框,供站点管理员选择应该从哪个WebContent display Portlet实例中获取该值

    让我向您展示第一个选项的代码,如果您需要第二个选项,我想您将从给定的代码示例(请注意注释)推断如何实现它:

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