Wednesday, October 13, 2010

Design Confusion for IBM Struts Portlet for WebSphere Portal Server

IBM has done a good job in adopting Struts concept for implementing MVC pattern for WebSphere portlet development. But the concept, if not understood properly within the context of portlet, may lead to portlet design issues.

To understand this better, listen to the below conversaion I had with one of my colleague.

Colleague: How can I create four struts portlet in the same WAR file?


Me: You cannot create four Struts portlets in the same WAR file using RAD. Why do you want to do that anyway?

Colleague: I have a requirement to create a portal page to display four portlets. The portlet are perosonal information, company information, account information and application access information. Since these information are related, I want to put them into single WAR file for bettern maintanence. So I thought of designing them as four portlets, but when I start creating them as Struts portlet in the same WAR file, I am facing a problem. I cannot create more than one struts portlet in a WAR file. So what should I do? shall I create four basic portlets instead of Struts Portlet?

Me: Ok, I see the problem. Before I answer, let me ask you another question. Forget that you are implementing this solution as portlet. Assume that you are implementing this as a Struts Web Application. Assume that the four porltet is four different pages in the web application. In that case what will you do? How many Struts Servlet you will configure in your application?

Colleague: I will need only one Struts servlet, but will create four different actions one for each of the pages.

Me: Ok, now you look back to your portlet scenario. You need only one Struts portlet but required four actions to display the required portlet information.

Colleague: But in the case of web application, I have four pages and in portal I have one page and I need to add four portlet to the page. So If I am creating only one Struts Portlet, then I need to add the same portlet four times but each one of them should display different information. How is that possible with Struts Portlet.

Me: Simple, use a configuration parameter to determine what to display. Then you clone the portlet four times, and configure each of the clone to display the required information. [Alternatively you can use an edit mode to do that instead of cloning]

Colleague: Ok thanks, that will work.
 
I have seen the same confusion happening with other developers as well. Most of the time what happens is the project ends up with single WAR file with many basic portlets and that will have all the issues of not following a proper MVC design. I have a below recommended solution to the above design problem and you can find it below.


Recommended Design for above scenario

I assume you are familiar with Struts Portlet development using RAD.

Portlet Development

1. Create a Struts Portlet Project in RAD

2. Create a four different struts actions, one for each of perosonal information, company information, account information and application access information. let us name the actions as personalAction, companyAction, accountAction and accessAction

3. Create another action say "forkAction" that will forward the request to one of the above four actions based on a portlet configuration parameter.

e.g. user a configuration parameter such as "user.mode" and use following values to determine the desired information.

user.mode=personal - Display Personal information in this case

user.mode=account - Display Account information in this case

user.mode=company - Display Company information in this case

user.mode=access - Display Access information in this case

4. create four local forwards to the forkAction same as the value of the above configuration parameter in struts config. i.e. create following local forwards

name=personal action=personalAction

name=company action=companyAction

name=account action=accountAction

name=access action=accessAction

5. Now in the forkAction read the configuratoin value from portletConfig.

6. Create an ActionForward with the above value read from the configuration and return the forward action.


Portal Administration

1. after deploying the portlet, create 4 clones from the same portlet such as PersonalPortlet, AccountPortlet, CompanyPortlet and AccessPortlet.

2. configure the user.mode parameter of PersonalPortlet as "personal"

3. configure the user.mode parameter of AccountPortlet as "account"

4. configure the user.mode parameter of AccessPortlet as "access"

5. configure the user.mode parameter of CompanyPortlet as "company"

6. Now you can add these four portlet to the same page to display four different information.

njoy.
Sajith

No comments: