2011-12-26 33 views
1

我有我的网页上两个portlet:的Java:获取文章ID从网页内容的portlet

第一个是网络内容门户,允许拿起一篇文章,并将其显示。

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

可能吗?

谢谢!

回答

1

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

设置和获取的值(Portlet间通信)的例子 - >Check this

+1

的Web Content Portlet是Liferay内置的portlet ...我如何编辑该porlet?谢谢。 – Carl 2011-12-27 08:18:54

+0

检查com.liferay.portlet.journalcontent.action.WebContentAction/ViewContentAction类。我认为你可以在课堂上获得文章ID,并从这里设置相同的内容。进行更改,然后再次将编辑后的.class文件添加到portal-impl.jar中。 – 2011-12-27 09:39:52

+0

获取liferay源代码,执行更改,并将编辑后的.class文件添加回portal-impl.jar – 2011-12-27 09:40:34

2

你可以使用一些Liferay的特定的API,但这种方法是不完美的做到这一点,但它会工作。

您可以使用Liferay API来获取页面上当前可用的portlet列表。然后,您可以通过Portlet ID找出哪些Portlet是WebContentDisplay类型。然后,您可以阅读他们的偏好设置,并且会显示它们显示的WebContent文章的ID。

但是请注意,可能会出现以下情况:您在页面上拥有多个WebContent Display 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); 
    } 

}