Saturday, June 19, 2010

JSF inter portlet communication

Let me start with Inter portlet communication feature of portlets. Firstly, I will give you a brief idea about Inter portlet communication. This is not a new terminology. As web-developers who are using servlets, JSPs and portlets for development purpose. If they want to pass some parameters from one resource to another resource what they will do? In servlets, they can use RequestDispatcher to achieve this functionality and for portlets itself, they can use Inter portlet communication feature.

In portlets, Inter portlet communication can implement by following ways. 

1) Using portlet session in Application scope.
2) Using IBM's property broker services (portlet wiring).
3) Eventing using JSR -286 specifications.
4) Public render parameter using JSR-286 specification.

In this post, I will describe Inter portlet communication using portlet session (first point) and JSF eventing feature. You can see detail description of JSF portlet framework flow in one of our posts.
   I am taking an example of two portlets. First portlet is calculator portlet which contains two numbers and four submit buttons (for ADD, SUBTRACT, MULTIPLY and DIVIDE operations) and second portlet shows the result according to the operation performed by first portlet. You can see the whole picture of Inter portlet communication in the picture below,


















Now, you need to add portlet configuration for both portlets in portlet.xml file. You need to add init parameters which tell the JSF portlet about the render file (JSP file).

For SyncExCalculator portlet:
<init-param>
<name>com.ibm.faces.portlet.page.view</name>
<value>/SyncExCalculatorView.jsp</value>
</init-param>

For SyncExCalculatorResult portlet:
<init-param>
<name>com.ibm.faces.portlet.page.view</name>
<value>/SyncExCalculatorResultView.jsp</value>
</init-param>

 Now, both portlets are rendered successfully and call both JSPs which look as given picture below.
 

  We need to understand how the action is being created from SyncExCalculatorView.jsp (first portlet).

 <h:commandButton id="addsubmit" action="#{CalculatorController.add}" type="submit" value="ADD" />



Here, you can see the action which is for add() method of CalculatorController bean. Now we need to understand how JSF gets executed add method. When I click on ADD button of portlet, JSF portlet looks up the entry of the managed bean (CalculatorController) in faces-config.xml file.

<managed-bean>
<managed-bean-name>CalculatorController</managed-bean-name>
<managed-bean-class>com.yash.beans.CalculatorController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
Here, scope of bean is session which contains the bean in session scope.

In this way, JSF looks up the bean and calls add method of bean. In add method of bean we provide all business logics (perform logic for adding two numbers) and store the results in FacesContext using ApplicationMap. I am storing the result value in ApplicationMap since I need to use result value in other portlet. The code lines are,

FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getApplicationMap().put("result", new Integer(result));

The result has been saved in FacesContext. Now I have to retrieve the same result value in other portlet so what will I do? I will make another method which retrieves the result value from FacesContext. The code lines are,

FacesContext context = FacesContext.getCurrentInstance();
Map requestParams = context.getExternalContext().getApplicationMap();
result1 = ((Integer)requestParams.get("result")).intValue();

You can get the result value in JSP (SyncExCalculatorResultView.jsp) of another portlet as,

<h:outputText value="#{CalculatorController.result1}" />

This code of line does the same thing. This line makes a call for result1 method of CalculatorController bean and uses the same entry of managed bean which I have used earlier for first portlet.

This is the way, We can implement Inter portlet communication feature using Java Server Faces Specification. You can mail us on my mail id for source code.

No comments: