在Portlet2.0,你可以使用服務端事件的技術來使用一個事件監聽來在portlets 之間共享數據。當使用IPC 表單的時候,portlet容器的行爲類似代理和轉發事件及轉載數據給portlets.這種方法需要轉載數據payload必須實現java.io.Serializable接口,因爲它也許會把數據發送到處在另外一個WAR下的portlet,而WAR也許運行在一個不同的類轉載器裏,另外Portlet2.0標準還需要事件在portlet.xml裏進行聲明。
下面的文本片段,定義了一個IPC事件,該事件在當一個客戶在一個訂單portlet裏被編輯的時候被觸發。訂單portlets被當作事件發佈者來註冊。而customersPortlet則處於另一面,作爲該事件的監聽來註冊。當一個客戶被編輯在訂單portlet的時候,則會發佈一個事件,而customersPortlet則會被通知到,並對該事件進行處理。
Web-inf/portlet.xml片段:
<portlet> <portlet-name>customersPortlet</portlet-name> <supported-processing-event> <qname xmlns:x="http://liferay.com/events">x:ipc.customerEdited</qname> </supported-processing-event> </portlet> <portlet> <portlet-name>bookingsPortlet</portlet-name> <supported-publishing-event> <qname xmlns:x="http://liferay.com/events">x:ipc.customerEdited</qname> </supported-publishing-event> </portlet>
....
<event-definition> <qname xmlns:x="http://liferay.com/events">x:ipc.customerEdited</qname> <value-type>com.liferay.faces.example.dto.Customer</value-type> </event-definition> |
你也可以對一個事件類型,實現自己的BridgeEventHandler,並在portlet.xml裏進行註冊。如果該處理器在portlet.xml被註冊JSR329標準裏要求該處理器就必須被激活,以讓它處理所需要處理的任意事件。
當客戶信息(例如名字)在訂單portlet裏被修改,則ipc.customerEdited事件則被髮送到Customers portlet通過CustomerEditedEventHandler類來處理:
...
import javax.faces.context.FacesContext; import javax.portlet.Event; import javax.portlet.faces.BridgeEventHandler; import javax.portlet.faces.event.EventNavigationResult;
...
public class CustomerEditedEventHandler implements BridgeEventHandler {
....
public EventNavigationResult handleEvent(FacesContext facesContext, Event event) { EventNavigationResult eventNavigationResult = null; String eventQName = event.getQName().toString();
if (eventQName.equals("{http://liferay.com/events}ipc.customerEdited")) { ... }
return eventNavigationResult; }
.... } |
而這裏的CustomerEditedEventHandler則需要在portlet.xml裏被註冊爲橋事件處理器:
<init-param> <name>javax.portlet.faces.bridgeEventHandler</name> <value>com.liferay.faces.example.event.CustomerEditedEventHandler</value> </init-param> |