ShowTable of Contents
Introduction
This article illustrates how to combine a Java
TM portlet and IBM® WebSphere® Portal event processing to be used to achieve navigation in a portlet-based Web application, as well as information passing. The sample application is built with IBM Rational® Application Developer (RAD) for WebSphere v8.0.4.
Creating a portlet project
The simple application developed in this article consists of two Web pages and three portlets. Let us first create a portlet project with portlets and an associated enterprise application project. The Portlet Project wizard in RAD supports the creation of JSR 286-compliant portlet projects:
1. Select File --- New --- Portal Web Project, to open the wizard. Specify the details in the New Portlet Project window, as shown in figure 1:
a) Name the project DN286.
b) Enable (check mark) the Add project to an EAR option, and type in the EAR name, such as DN286EAR; click Finish.
Figure 1. New Portlet Project window
2. To test an application in a RAD environment we must create an instance of WebSphere Portal server. Open the Servers tab, right-click on it, and select New --- Server.
3. Select WebSphere Portal v7.0 server, and click Next. Select one of preconfigured profiles, click Next, and then Finish (see figure 2).
Figure 2. Define a New Server window
Adding the required portlets
The next step is to add the required portlets to the portlet application that you created. Given that we cover three portlets in the sample application, you can add these portlets to the portlet project as follows:
- Right-click the portlet deployment descriptor and select New --- Portlet.
- Enter the Portlet name as DN286Portlet, and click Next.
- Select Portlet Type as Java Portlet, and set com.ibm.dn286 as the package name.
- Repeat these steps to add the other two portlets, CN286Portlet and ON286Portlet.
When you create a portlet in RAD environment and select the Empty Portlet or Basic Portlet option (see figure 3), a default JSP file – view is created for the corresponding portlet. Alternatively, we can right-click on the application project and select New --- JSP File.
Figure 3. New Portlet window
Verify that the Portlet Java implementation contains the following code inside its doView() method (see figure 4):
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(VIEW_JSP));
rd.include(request,response);
Figure 4. Portlet .jsps
Configuring portlet events
Lets us now configure portlet events:
- In Project Explorer, double-click Portlet Deployment Descriptor and select the Events tab.
- Click the Add button and create an event named DN2CN with custom namespace of http://DN286.com , as shown in figure 5.
- Repeat this step for the other CN2ON event.
Figure 5. Event Definitions window
4. Now navigate to the Portlets tab in Portlet Deployment Descriptor, where you will see the three portlets we created earlier: DN286, CN286, and ON286:
a) Click on portlet DN286, and in the Supported Publishing Events window, click Select, and add event DN2CN.
b) Click on portlet CN286, click Select in the Supported Publishing Events window, add event CN2ON. Then click Select in Supported Processing Events window, and add event DN2CN.
c) Click on portlet ON286, click Select in the Supported Processing Events window, add event CN2ON; save the changes.
5. Start a WebSphere Portal server, open the Administration page and create two pages, EventsPage and ResultPage. The EventsPage will host two portlets, DN286 and CN286; the ResultPage will host one portlet, ON286.
6. We export the application as WAR archive. Right-click on the DN286 project in Project Explorer, select Export --- WAR, and name the exported file dn286.war.
7. Open the Portal administration console, and on the left-hand navigation find the Web modules link. Click on it, and then click the Install button.
8. Select the dn286.war file we just exported and select the Start Application radio button; click Finish.
The application has now been deployed with three portlets onto WebSphere Portal server (see figure 6).
Figure 6. Manage Web Modules window
Now we can create pages and place portlets on them. Perform these steps on the Administration console:
- Click Manage Pages and then click New Page.
- Enter the Title and Unique Name fields; click OK.
- Click the Edit Page Layout button and, on the Content tab, add the portlets as shown in figure 7.
Figure 7. Edit Layout window
Now let's configure the wiring between portlets on the page:
1. Click the Wires tab and add the events, as shown in figure 8.
Figure 8. Portlet Wiring Tool Wires tab
2. Define the global target in the portlet wiring configuration, making sure that “Global” is checked on both EventsPage and ResultPage; click OK (see figure 9).
Figure 9. Define Global Targets for Portlets on page: EventsPage window
3. In the Manage Web Modules window, click Install (see figure 10).
Figure 10. Manage Web Modules window
The Manage Portlets window displays, showing our installed module portlet configuration (see figure 11).
Figure 11. Manage Portlets window
Now we create three JSP pages as views for each portlet, DN286PortletView.jsp, CN286PortletView.jsp, and ON286PortletView.jsp:
The doView(…) method of each portlet should follow the pattern shown in listing 1.
Listing 1. Code pattern for doView(...) method
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType(request.getResponseContentType());
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("DN286PortletView.jsp");
rd.include(request,response);
}
When a user clicks a link or a button on the JSP page, the processAction(…) function will be invoked:
<a href='<portlet:actionURL></portlet:actionURL>'>Link to ON286</a>
1. Modify either the processAction() or processEvent() methods to publish the event:
Listing 2. DN286Portlet
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, java.io.IOException {
QName qName = new Qname("http://DN286.com", "DN2CN");
String symbol = (String) request.getParameter("symbol");
response.setEvent(qName, symbol);
}
Listing 3. CN286Portlet
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
QName qName = new Qname("http://DN286.com", "CN2ON");
response.setEvent(qName, stockValue);
}
@Override
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {
super.processEvent(request, response);
stockValue = request.getEvent().getValue().toString();
response.setRenderParameter("symbol", stockValue);
}
Listing 4. ON286Portlet
@Override
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {
super.processEvent(request, response);
String value = request.getEvent().getValue().toString();
stockValue = value;
}
2. Observe the Portal landing page in a Web browser. On the EventsPage Web page type in any symbol name in a text box, click the Quote button, and verify that the CN286 portlet displays the typed-in value (see figure 12).
The value was sent as an event payload; of course, we could add additional data processing for this payload.
Figure 12. EventsPage Web page
3. Now click the link to ON286; you are redirected to ResultPage with ON286 on it. The portlet received the event that was sent in processAction(…) method of CN286 portlet, and the received event CN2ON activated the ResultPage.
Figure 13. ON286Portlet
This is an example of how an event can be used as a mechanism to navigate between pages. In addition, the String payload value “IBM” propagated to the ON286 portlet that displayed this value on its JSP view.
Conclusion
The example demonstrated in this article uses portlet events in WebSphere Portal. This approach has the advantage of following JSR286 portlet specifications with minimal Portal proprietary configuration, thus making the application portable and less dependant on third-party frameworks.
Tell us what you think
Please visit this link to take a one-question survey about this article:
Resources
developerWorks® WebSphere Portal zone:
http://www.ibm.com/developerworks/websphere/zones/portal/
WebSphere Portal discussion forum:
http://www.ibm.com/developerworks/forums/forum.jspa?forumID=168
developerWorks Rational Application Developer for WebSphere Software product page:
http://www.ibm.com/developerworks/rational/products/rad/
About the author
Dmitri Nevedrov works at IBM Global Business Services, has been involved in design and development of middleware solutions for over ten years, and is based in Denver, Colorado. You can reach him at
dnevedr@us.ibm.com.