Friday, June 18, 2010

Understanding Java Server Faces Portlet Workflow

For understanding the JSF portlet workflow, Let's start with portlet life cycle in which we are using two important methods. These are,
1) doView()
2) processAction()

When portlet renders first time, it calls doView() method of portlet life cycle. When we create any action (Click on link, submit button) processAction() method gets called, followed by doView() method. In doView() method, we usually include JSP which represents portlet's view. We perform these (portlet mode) operations on portlet class which includes in <portlet-class> tag under portlet.xml file. This class is modifiable.

In the same way, Java Server Faces specification also looks the portlet entry in <portlet-class> tag under portlet.xml file.

<portlet-class>com.ibm.faces.portlet.FacesPortlet</portlet-class>

Here, FacesPortlet is not allowed to modify the code. It contains all modes as I described in portlet life cycle and it understands all actions and JSP page includes in its own way. Here, I will describe you how JSF will do this.Firstly, I will let you know how JSF includes JSP on rendering phase of portlet. You have to include following lines in portlet.xml file.


<init-param>
<name>com.ibm.faces.portlet.page.view</name>
<value>/CalculatorPortletView.jsp</value>
</init-param>

JSF portlet class is much smarter which understands JSP file and includes in to render phase. We do not need to create an instance of RequestDispatcher and include the JSP file as we do in portlet life cycle.

Now, we need to understand, how action is handled by JSF? since we can not modify FacesPortlet class as we can do in portlet life cycle. This is also a very tricky approach which is done by JSF. When we click on any button or link in JSP page, JSF internally makes a call of processAction() method of portlet which finds managed bean class in faces-config.xml file and creates an instance of bean in different scope (request, session). You can understand this by following example,

Here, we are creating one action which executes when we click on submit button. It creates an action add which will get in CalculatorController managed bean .

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

The managed bean entry is present in faces-config.xml which creates a instance of CalculatorController bean and calls add() (event action called from JSP page) method in it.

<managed-bean>
<managed-bean-name>CalculatorController</managed-bean-name>
<managed-bean-class>com.yash.CalculatorController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now action created from JSP file executes completely. Thereafter, it looks up the navigation rule which provides the way for redirecting control to another JSP file or the same file. The codes for navigation rule are,

<navigation-rule>
<from-view-id>/CalculatorPortletView.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/result.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/CalculatorPortletView.jsp</to-view-id>
</navigation-case>
</navigation-rule>

This code represents source JSP file CalculatorPortletView.jsp (form-view-id) from where the action creates and according to the return statement of add method, it forwards the request to another JSP file.

1 comment:

Anonymous said...

short and simple explanation.Very nicely explained