4.9.3 PortletURI

The portletURI represents a URL that can be used to navigate between modes. The PortletURI can be used to navigate to a previous mode, such as from Edit to View, or to navigate back to the same mode, such as a multi-part form in View or Edit. There is no ability to create a PortletURI object pointing to a mode not yet visited by the user.

PortletRequest.createURI returns a portletURI object pointing to the portlet in its current mode. For example, if the portletURI is created in the doView mode, the URL points to the portlet in View. The createReturnURI method returns a PortletURI object pointing to the last mode the portlet was in. This mode is commonly used in the doEdit method when the URI needs to point back to the View mode. The edit.jsp would use the PortletURI to bring the user back to the View mode when they have completed the edit or configure process.

In order for a portlet to be notified of an event, such as the user clicking a button, the portletURI must contain an associated PortletAction. Typical PortletURI construction and usage is shown in Example 4-22.

The process of adding actions to PortletURI objects has been simplified. The addAction(PortletAction) method has been deprecated and replaced with addAction(String). Since the vast majority of work with PortletActions involves no more than setting a name, this new implementation is much more convenient.

Developers are advised to use simple action string instead.

Note: Deprecated classes and interfaces are still supported in the current release but are not recommended for use because they might not be supported in future releases.

Since the DefaultPortletAction class and the PortletAction interfaces are deprecated in this release, we show the use of the Simple Action string instead, as illustrated in Example 4-22.

Example 4-22 Working with PortletURI

PortletURI uri = response.createReturnURI();
uri.addAction("save");
request.setAttribute("uri", uri.toString());

It is possible to add parameters to the PortletURI object. Parameters added to the PortletURI via code or through a form are accessed the same way via the portlet request object. This provides a mechanism to pass default values or to pass parameters not displayed in the form. Example 4-23 displays the code for adding a parameter. Be aware that parameters set via the PortletURI are not passed in the traditional HTML syntax.

Note: The DefaultPortletAction class and the PortletAction interfaces are deprecated and you should use the Simple Action string instead, as illustrated in Example 4-23.

Example 4-23 Add URI

public void doView(PortletRequest request, PortletResponse response) throws
       PortletException, IOException {
 PortletURI viewURI = response.createReturnURI();
 viewURI.addAction("save");
 viewURI.addParameter("Param1", "Param1Value");
 request.setAttribute("viewURI", viewURI.toString());
 getPortletConfig().getContext().include("/jsp/View.jsp", request,
response); }


Redbooks
ibm.com/redbooks