How to create a link to a portlet (Standard API) that passes parameters to that portlet

Technote (FAQ)


Question

Many times when developing an application, you want to create a link to a portlet and pass in some parameters. You might want to do this from a theme or from another portlet. How do you create such a link?

Answer

Setup procedures

To create a link in a theme or on a portlet to go directly to another page and portlet, perform the following steps:

1. Give a unique name to the page using either the XML Configuration Interface (XMLAccess) or the administrator portlet.

2. Give a unique name to the portlet on the page. You have to give a unique name to the portlet window or control on the page; simply giving a portlet a unique name in the Admin Portlet is not enough.

Note: The assigning of the unique name to the portlet on the page can only be done through XMLAccess.

Use XMLAccess to export the reference to the portlet on a page. Refer to the Sample Files in the XML Configuration Interface (XMLAccess) documentation found in the WebSphere Portal Information Center. You can also use the WebSphere Portal Admin portlets for managing pages to export the page, which will also contain the portlet window.

The XML Configuration Interface will generate a file similar to the one attached here:
pageExport.xml

3. Give the component that contains the portlet window a unique name such as p2psample4.win.SearchResult shown below. (This is done by updating the text in the xml file.)

<component action="update" active="true" deletable="undefined" domain="rel" modifiable="undefined" objectid="7_3A7U7FH20OO9702T3BOU8700G2" ordinal="100" type="control" uniquename="p2psample4.win.SearchResult" width="undefined">
<portletinstance action="update" domain="rel" objectid="5_3A7U7FH20OO9702T3BOU8700G6" portletref="3_3A7U7FH20OO9702T3BOU870002"/>
</component>

4. Then reimport this file either by using the command line XMLAccess interface or by using the import xml portlet within the WebSphere Portal Admin pages.



Creating a link from the theme

1. Make sure that your theme has this tag-lib already defined. These tags are only supported in the theme and should not be used in a portlet.

<%@ taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.0/portal-navigation"
prefix="portal-navigation" %>

2. Add the following code:

< portal-navigation :urlGeneration contentNode=" p2psample.SearchResult " layoutNode=" p2psample4.win.SearchResult "accessControlCheck="NoCheck">    
         < portal-navigation :urlParam name="myJSRParam" value="August" type="render" />
                   <a href="<% wpsURL.write(out); %>" class="wpsLinkBarLink">
               Call and pass the param now
            </a>
      </ portal-navigation :urlGeneration>



Create a link from a portlet to a portlet

1. First decide if you want to use the URL generation helper classes or use the NavigationModel SPI directly.

2. To use the Helper classes, download them from "Portal 6.0 Advanced URL Generation Helper classes" (# 1265900).

3. Then add that jar to your portlets web-inf folder.

4. Add code like the following in your JSP

<%
try {
HashMap map = new  HashMap();
  String[] value1 = { "testthis6" };
  map.put( " myJSRParam " , value1);

String url1 = PortletURLHelper.generateUrl( "p2psample.SearchResult" , "p2psample4.win.SearchResult" , map, true , renderRequest, renderResponse);

%> The URL is .... <%= url1 %> < br >
< a   href =" <%= url1 %> " > url  to action and mode </ a >
<%  
} catch (Exception e)
{
%><%= e.getMessage() %>
<%
e.printStackTrace(System.out);
}
%>

5. If you decide to use the Navigation Model SPIs directly, then add the following code to your portlet or JSP:

private   PortletServiceHome psHome  = null ;
PortletStateManager mgr = null ;
if  ( psHome  == null  ){
Context ctx = new  InitialContext();
      psHome  = (PortletServiceHome) ctx.lookup( "portletservice/com.ibm.portal.state.service.PortletStateManagerService" );      }
try  {
PortletStateManagerService service = (PortletStateManagerService) psHome .getPortletService(PortletStateManagerService. class );
 mgr = service.getPortletStateManager(request, response);
} catch  (Exception ex) {
System. out .println( "the portlet service was unable to be retrieved" );
}
URLFactory urlFactory = mgr.getURLFactory();
boolean saveState = false;
HashMap params = new  HashMap();
String portletName = "p2psample4.win.SearchResult"
  String[] value1 = { "testthis6" };
  map.put( " myJSRParam " , value1);
String finalUrl = "";
 try {
  final EngineURL url ;
 
  if(saveState) {
  url = urlFactory.newURL(Constants.SMART_COPY);
  } else {
  url = urlFactory.newURL(Constants.EMPTY_COPY);
  }
  final com.ibm.portal.state.accessors.selection.SelectionAccessorFactory selectionFactory = (com.ibm.portal.state.accessors.selection.SelectionAccessorFactory) mgr.getAccessorFactory(com.ibm.portal.state.accessors.selection.SelectionAccessorFactory.class);
  final SelectionAccessorController selectionCtrl = selectionFactory.getSelectionAccessorController(url.getState());
  selectionCtrl.setSelection( "p2psample.SearchResult" );
  selectionCtrl.dispose();
  if(portletName != null) {
    final PortletAccessorFactory portletAccessorFactory = (PortletAccessorFactory) mgr.getAccessorFactory(PortletAccessorFactory.class);
  final PortletAccessorController portletCtrl = portletAccessorFactory.getPortletAccessorController(portletName, url.getState());
  if(params != null) {
  portletCtrl.getParameters().putAll(params);
  }
  // Dispose the accessor (avoids memory leak)
  portletCtrl.dispose();
  }
  finalUrl = url.writeDispose(new StringWriter()).toString();
 } finally {
  urlFactory.dispose();
 }

Then the link will be like this sample:
   <a href="<%=finalUrl%>Link to portlet</a>



How to retrieve the parameters

In your called Portlet code to retrieve the parameter, add this in the doview method:

if(sessionBean != null) {
String passedParam = request.getParameter("myJSRParam");
sessionBean.setPassedParam(passedParam);
}


Rate this page:

(0 users)Average rating

Add comments

Document information


More support for:

WebSphere Portal
Application Development

Software version:

6.0, 6.1

Operating system(s):

AIX, HP-UX, Linux, Solaris, Windows, i5/OS, z/OS

Software edition:

Enable, Express, Extend, Server

Reference #:

1322423

Modified date:

2008-10-22

Translate my page

Machine Translation

Content navigation