liferay
ポートレット間の通信
サーチ…
前書き
備考
パブリックレンダーパラメータの使用
このアプローチはJSR 286で導入されました。
JSR 168では、ポートレットのprocessActionで設定されたレンダーパラメータは、そのポートレットでのみ使用可能でした。パブリックレンダリングパラメータ機能では、一方のポートレットのprocessActionで設定されたレンダリングパラメータは、他のポートレットのレンダリングでも使用できます。これをサポートするすべてのポートレットに対して、
ポートレットタグがportlet.xml終了する直前に<supported-public-render-parameter>タグを追加します。
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-public-render-parameter>{param-name}</supported-public-render-parameter>
</portlet>
<portlet-app>タグが終了する直前に<public-render-parameter>タグを追加する
<public-render-parameter>
<identifier>{param-name}</identifier>
<qname xmlns:x="localhost">x:{param-name}</qname>
</public-render-parameter>
</portlet-app>
processActionメソッドでは、応答にparam値を設定する必要があります
res.setRenderParameter({param-name},{param-value});
関連するポートレットのアクション・フェーズを実行した後、同じまたは異なるアプリケーションの一部であるかどうかに関係なく、ページ上のすべてのサポート・ポートレットのパラメータをレンダリング・フェーズで使用できるようにする必要があります(戦争)。
ポートレット・セッションの使用
これは、JSR 168以降、そこにあった1つのアプローチです。ポートレットセッションを使用して属性を共有することができます。ポートレットセッションには、さまざまな種類のスコープがあります。
- ポートレットの有効範囲(ポートレット内でのみ使用可能な属性)
- アプリケーションスコープ(アプリケーション全体で利用できる属性[war])
この方法を使用するには、ポートレット・リクエストでポートレット・セッションをすぐに使用できるため、ポートレット構成では何も入力する必要はありません。
PortletSession session = renderRequest.getPortletSession();
session.setAttribute("attribute-name","attribute-value", PortletSession.APPLICATION_SCOPE);
または
PortletSession session = renderRequest.getPortletSession();
session.setAttribute("attribute-name","attribute-value", PortletSession.PORTLET_SCOPE);
属性は、それぞれのスコープからのみ取得できます。ポートレットスコープで設定された属性の場合と同様に、
PortletSession session = renderRequest.getPortletSession();
String attributeValue = (String) session.getAttribute("attribute-name", PortletSession.PORTLET_SCOPE);
このアプローチの主な制限は、アプリケーション範囲外の他のポートレット間での共有の欠如です。これを克服するために、 liferay-portlet.xml <private-session-attributes >を追加するための、
<private-session-attributes>false</private-session-attributes>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>{portlet-name}</css-class-wrapper>
</portlet>
属性が設定され、取得されるすべてのポートレットで使用されます。
イベント機能の使用
イベント・メカニズムは、カスタム・オブジェクトを他のポートレットに渡す追加機能を備えた、パブリック・レンダリング・パラメータの拡張バージョンですが、イベント・フェーズのオーバーヘッドを伴います。
これを達成するために、このメカニズムは
- パブリッシャー・ポートレット
- プロセッサー(コンシューマー)ポートレット。両方が異なるポートレット・アプリケーションの一部である場合があります。
で開始する、
portlet.xmlパブリッシャ・ポートレットに<supported-publishing-event>タグを追加しますportlet.xml
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-publishing-event>
<qname xmlns:x="http:sun.com/events">x:Employee</qname>
</supported-publishing-event>
</portlet>
portlet.xmlプロセッサーポートレットに<supported-processing-event>タグを追加しますportlet.xml
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-processing-event>
<qname xmlns:x="http:sun.com/events">x:Employee</qname>
</supported-processing-event>
</portlet>
両方のポートレットに<event-definition>タグを追加し、 portlet.xml event-nameとtypeを定義しportlet.xml
<event-definition>
<qname xmlns:x="http:sun.com/events">x:Employee</qname>
<value-type>com.sun.portal.portlet.users.Employee</value-type>
</event-definition>
</portlet-app>
次に、イベントタイプのクラスを作成する必要があります(カスタムタイプの場合)
public class Employee implements Serializable {
public Employee() {
}
private String name;
private int userId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int id)
{
this.userId = id;
}
}
今、パブリッシャー・ポートレットでは、イベントをアクション・フェーズで公開する必要があります
QName qname = new QName("http:sun.com/events" , "Employee");
Employee emp = new Employee();
emp.setName("Rahul");
emp.setUserId(4567);
res.setEvent(qname, emp);
イベントを公開した後、イベントフェーズで発行者のポートレットで処理する必要があります。
イベントフェーズはJSR 286で導入され、該当する場合はポートレットのレンダリングフェーズの前に実行されます
@ProcessEvent(qname = "{http:sun.com/events}Employee")
public void processEvent(EventRequest request, EventResponse response) {
Event event = request.getEvent();
if(event.getName().equals("Employee")){
Employee payload = (Employee)event.getValue();
response.setRenderParameter("EmpName",
payload.getName());
}
}
レンダリング要求を介してレンダリングパラメータから検索することができる。