我有我的網頁上兩個portlet:的Java:獲取文章ID從網頁內容的portlet
第一個是網絡內容門戶,允許拿起一篇文章,並將其顯示。
另一個是我正在使用的portlet(Struts MVC)。 我想在第二個portlet中執行的操作是獲取用於在第一個portlet中顯示Web內容的文章ID。
可能嗎?
謝謝!
我有我的網頁上兩個portlet:的Java:獲取文章ID從網頁內容的portlet
第一個是網絡內容門戶,允許拿起一篇文章,並將其顯示。
另一個是我正在使用的portlet(Struts MVC)。 我想在第二個portlet中執行的操作是獲取用於在第一個portlet中顯示Web內容的文章ID。
可能嗎?
謝謝!
是的,您可以通過在會話中設置它來在兩個不同的portlet之間共享數據。通過編輯portlet代碼(第一個portlet)來設置文章ID,在會話中設置它並在portlet中檢索它。
設置和獲取的值(Portlet間通信)的例子 - >Check this
你可以使用一些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);
}
}
的Web Content Portlet是Liferay內置的portlet ...我如何編輯該porlet?謝謝。 – Carl 2011-12-27 08:18:54
檢查com.liferay.portlet.journalcontent.action.WebContentAction/ViewContentAction類。我認爲你可以在課堂上獲得文章ID,並從這裏設置相同的內容。進行更改,然後再次將編輯後的.class文件添加到portal-impl.jar中。 – 2011-12-27 09:39:52
獲取liferay源代碼,執行更改,並將編輯後的.class文件添加回portal-impl.jar – 2011-12-27 09:40:34