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

我的网页上有两个portlet:

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

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

可能吗 ?

谢谢!

您可以使用一些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 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, ie // 56_INSTANCE_NWWDuJPL64xa // 56_INSTANCE_N1m7pQGwcScG // would be IDs of WebContent Display portlet // PortletConstants.getRootPortletId(portletId) will return // portlet ID part without instance ID. Ie 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); } } 

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

有关设置和获取值(内部portlet通信)的示例 – > 选中此项