[Liferay]Portlet 之間使用 PortletSession 進行通信

Portlet 之間通信有三種方式:

1. PortletSession

2. IPC

3. Cookies


現在有個需要解決的問題,怎麼使用 Portlet A 在滿足一定條件的情況下,穿參數給 Portlet B,通過該參數改變 Portlet B 的狀態。

原本打算使用 IPC, 因爲上述三個方法中就屬 IPC 概念最通俗易懂操作簡單。

但是 IPC 事件的週期首先需要 processAction 再是 processEvent, 在 Resource 狀態下貌似是沒辦法觸發 IPC。

於是使用第一種方法,步驟如下

1. liferay-portlet.xml 添加一個屬性,注意屬性必須按照順序添加:

<portlet>
	...
	<private-session-attributes>false</private-session-attributes>
	...
</portlet>

2. Portlet A 添加:

PortletSession session = request.getPortletSession();

session.setAttribute("LIFERAY_SHARED_directive", "test" ,PortletSession.APPLICATION_SCOPE);

session.setAttribute 方法第一個參數是對應於 Http Session 的 key,唯一的區別就是定義的 key 必須得有前綴,不然無法在其他 Portlet 中獲取。

portal.properties 裏的定義:

session.shared.attributes=COMPANY_,LIFERAY_SHARED_,org.apache.struts.action.LOCALE,PORTLET_RENDER_PARAMETERS_,PUBLIC_RENDER_PARAMETERS_POOL_,USER_

3. Portlet B 使用:

PortletSession portletSession = request.getPortletSession();

String directive = (String)portletSession.getAttribute("LIFERAY_SHARED_directive",PortletSession.APPLICATION_SCOPE);

最後需要注意的一點,這個方式適用於同一個 web application 的兩個不同 war 包。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章