Portlet事件也是JSR-286(Portlet 2.0新添加的一種Portlet之間交互的方式),比起Public Render參數的交互方式,它能夠傳遞大對象而不僅侷限於字符串或者字符串數組,並且能夠啓用cache.比起PortletSession,它又支持跨Portlet應用的Portlet交互.java
(我驚歎:果真事件驅動的思想深刻人心而且無處不在啊,已經在無數地方看到了事件驅動的強大能力了)數組
步驟1:先定義一個Event類,它必須實現Serializable接口, 由於有可能要被遠程的Portlet容器裏面的Portlet所接受,因此要在網絡傳輸所以要被序列化,序列化的方式就是用JAXB將其轉爲XML文檔而後在網絡上傳輸。網絡
這個@XmlRootElement表示這個事件將以xml的形式被Portlet容器發佈出去app
- import chapter11.code.listing.domain.Book;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- public class BookAddedEvent implements Serializable
- private static final long serialVersionUID = ...
- private String name;
- private String author;
- private Long isbnNumber;
- private String category;
- public BookAddedEvent() {
- //-- do nothing
- }
- public BookAddedEvent(Book book) {
- this.name = book.getName();
- this.author = book.getAuthor();
- this.category = book.getCategory();
- this.isbnNumber = book.getIsbnNumber();
- }
- ...
- }
而後這個Book添加的事件就會轉爲以下的XML序列化的文檔而且在網絡上傳輸:dom
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <bookAddedEvent>
- <author>Ramnivas Laddad</author>
- <category>Java</category>
- <isbnNumber>1933988053</isbnNumber>
- <name>AspectJ in Action, Second Edition</name>
- </bookAddedEvent>
步驟2:在發送者Portlet的方法中添加這個事件對象:ide
- public class BookCatalogPortlet extends GenericPortlet {
- @ProcessAction(name = "addBookAction")
- public void addBook(...)... {
- ...
- Book book = new Book(category, name, author, Long
- .valueOf(isbnNumber));
- //調用bookService的addBook方法來添加這本書
- bookService.addBook(book);
- //同時,建立一個QualifiedName來表明事件的惟一名稱(有了某個名字空間)
- QName eventName = new QName(
- "http://www.mynamespace.com", "bookAddedEvent"
- );
- //在response對象中,將Event的內容和Event的QName進行綁定
- response.setEvent(eventName, new BookAddedEvent(book);
- ...
- }
步驟3:在portlet.xml的<portlet>中聲明這個portlet所支持的發佈事件,而且在portlet應用級別顯式的定義它所發佈的事件<supported-publishing-event>(事件的qname關聯到具體的事件類類型)this
- <portlet-app...>
- <portlet>
- <portlet-name>bookCatalog</portlet-name>
- <portlet-class>
- chapter11.code.listing.base.BookCatalogPortlet
- </portlet-class>
- ...
- <supported-publishing-event>
- <qname xmlns:n="http://www.mynamespace.com">
- n:bookAddedEvent</qname>
- </supported-publishing-event>
- </portlet>
- <event-definition>
- <qname xmlns:n="http://www.mynamespace.com">
- n:bookAddedEvent</qname>
- <value-type>chapter11.code.listing.base.
- BookAddedEvent</value-type>
- </event-definition>
- ...
- </portlet-app>
步驟4;在接收者Portlet中,用代碼顯示的從事件中獲取所須要的內容而且處理:spa
- public class RecentlyAddedBookPortlet extends GenericPortlet ...
- @ProcessEvent(qname=
- "{http://www.mynamespace.com}bookAddedEvent")
- public void processAddedBookEvent(EventRequest
- request, EventResponse eventResponse) throws...{
- Event event = request.getEvent();
- BookAddedEvent bookAddedEvent =
- (BookAddedEvent)event.getValue();
- eventResponse.setRenderParameter("category",
- bookAddedEvent.getCategory());
- eventResponse.setRenderParameter("name",
- bookAddedEvent.getName());
- eventResponse.setRenderParameter("author",
- bookAddedEvent.getAuthor());
- eventResponse.setRenderParameter("isbnNumber",
- String.valueOf(bookAddedEvent.getIsbnNumber()));
- }
- }
- <portlet-app...>
- <portlet>
- <portlet-name>recentBook</portlet-name>
- <portlet-class>
- chapter11.code.listing.base.RecentlyAddedBookPortlet
- </portlet-class>
- ...
- <supported-processing-event>
- <qname xmlns:n="http://www.mynamespace.com">
- n:bookAddedEvent</qname>
- </supported-processing-event>
- </portlet>
- <event-definition>
- <qname xmlns:n="http://www.mynamespace.com">
- n:bookAddedEvent</qname>
- <value-type>chapter11.code.listing.base.
- BookAddedEvent</value-type>
- </event-definition>
- ...
- </portlet-app>