Skip to main content link. Accesskey S
  • Log In
  • Help
  • IBM Logo
  • IBM Digital Experience wiki
  • All Wikis
  • All Forums
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • IBM Redbooks
  • API Documentation
Search
Community Articles > IBM Web Content Manager > Flexible JSPs in WCM using parameters
  • New Article
  • Share Show Menu▼
  • Subscribe Show Menu▼

About the Original Author

Click to view profileMaarten Pol
Contribution Summary:
  • Articles authored: 2
  • Articles edited: 2
  • Comments Posted: 2

Recent articles by this author

Flexible JSPs in WCM using parameters

JSPs offer loads of flexibility. But sometimes you end up creating several copies with minor changes just to accommodate slightly different purposes. Avoid this by making the JSPs respond to parameters. The parameters are set from the invoking JSP component, or passed in through the query string. ...

Full Control Over Markup In WCM While Using JSPs

Prevent frequent JSP updates and deployments by delegating control over the rendered content to WCM components
Community articleFlexible JSPs in WCM using parameters
Added by Maarten Pol | Edited by Maarten Pol on March 3, 2015 | Version 5
  • Edit
  • More Actions Show Menu▼
Rate this article 1 starsRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars
expanded Abstract
collapsed Abstract
JSPs offer loads of flexibility. But sometimes you end up creating several copies with minor changes just to accommodate slightly different purposes. Avoid this by making the JSPs respond to parameters. The parameters are set from the invoking JSP component, or passed in through the query string. Use this ability to limit maintenance and the need for deployments.
Tags: JSP, parameter, control, dynamic, steer, query string, wcm, portal, authoring, rendering. JSP component, flexible
ShowTable of Contents
HideTable of Contents
  • 1 Introduction
    • 1.1 Pre-requisites
    • 1.2 Desire
  • 2 Adding flexibility to JSPs invoked from WCM
    • 2.1 Hello, World!
    • 2.2 Adding flexibility to an existing JSP
      • 2.2.1 JSP code with static values
      • 2.2.2 JSP code with flexibility added
    • 2.3 Other applications
  • 3 Best practices
    • 3.1 Downloads
  • 4 Conclusion

Introduction

We have created a JSP before (see Part 1: Full Control Over Markup In WCM While Using JSPs). For other purposes we want to re-use the functionality offered by the JSP (i.e. the JSP delegates generation of the markup to WCM), but simply show a different component. All this should be possible by simply adding flexibility to a single JSP.

The article starts off with the basics (hello World) and then moves on to extend the code from the previous article.

Pre-requisites

  • Advanced working knowledge with WCM
  • Ability to read Java code
  • Working knowledge of IDE (e.g. Rational Software Architect, Eclipse, etc)
  • Ability to build, package and deploy code.

Desire

We want to control the behaviour of JSPs by passing in parameters. Such parameters might drive the selection of content to display, the component to render for each result, or a search condition to apply.

Referring to part 1: Full Control Over Markup In WCM While Using JSPs, we take it a step further by making the JSP respond to parameters that we pass into it. 

Adding flexibility to JSPs invoked from WCM

Hello, World!

Let’s start from scratch. In its simplest form, the JSP reads a request parameter and displays its value.

 


<%@page language="java" contentType="text/html; charset=ISO-8859-1"

      pageEncoding="ISO-8859-1"%>

<%    String name = request.getParameter("name"); // get name

      if (name==null) name="World"; // assume default value for name when none provided

%>Hello, <%=name %>!

When the EAR file (see below) is deployed and running on WebSphere Portal, you can invoke the JSP by providing its path in a JSP component in WCM. With the chosen context root of “wcmcustom”, the path to the JSP is:

/wcmcustom;/render/helloWorld.jsp

Preview the JSP component to view its output.

Now, you can influence what name is displayed simply by adding a parameter to the query string in the URL. Simply add the query string parameter name=Moon to the URL.

Additionally, you can set parameters by adding parameters to the JSP path in the JSP component.

/wcmcustom;/render/helloWorld.jsp?name=Stars

Preview the JSP again to view the result.

Note: the parameters provided in the JSP component take precedence over the parameters specified in the query string from the URL.

Adding flexibility to an existing JSP

With the basic principle down, you can now add logic to your own JSPs. In the example provided in the previous article , we can replace the static strings defined in the JSP and expect these to be provided as parameters at the time of invocation of the JSP.

JSP code with static values


<%@page import="com.ibm.workplace.wcm.api.*"%>

<%@page import="java.util.*"%>

<%@page import="java.text.*"%>

<%@page trimDirectiveWhitespaces="true"%>

<%@page language="java" contentType="text/html; charset=ISO-8859-1"

      pageEncoding="ISO-8859-1"%>

<%@page session="false"%>

<%@taglib uri="/WEB-INF/tld/wcm.tld" prefix="wcm"%>

<wcm:initworkspace />

<%--

  - Author(s): Maarten Pol

  - Date: 24/09/2014

  - Description: JSP to invoke a component from WCM for each result in a list (navigator, menu, search results, etc)

  - Version History

  - 0.1 - September 24, 2014 - Initial version

--%>

<%

      final String COMPONENT_NAME = "Menu - Show Responses";

      final String DESIGN_LIBRARY_NAME = "Design Library";

      // Get the current content item. Please note that getCurrentResultId requires both <wcm:initworkspace />

      // and compute="always" in calling the JSP component in the result design for the component

  RenderingContext renderingContext = (RenderingContext) request

         .getAttribute(Workspace.WCM_RENDERINGCONTEXT_KEY);

  DocumentId currentResultId = renderingContext.getCurrentResultId();

  Workspace workspace = renderingContext.getContent().getSourceWorkspace();

  Content content = (Content) workspace.getById(currentResultId, true, false);

      // Get the component to render

  DocumentLibrary libDesign = (DocumentLibrary) workspace.getDocumentLibrary(DESIGN_LIBRARY_NAME);

  workspace.setCurrentDocumentLibrary(libDesign); // get design library

  DocumentIdIterator docIds = workspace.findByName(DocumentTypes.LibraryComponent, COMPONENT_NAME,

         Workspace.WORKFLOWSTATUS_PUBLISHED); // find component by name

      if (docIds.hasNext()) {

     DocumentId docId = (DocumentId) docIds.next();

     LibraryComponent libComp = (LibraryComponent) workspace.getById(docId, true, false);

            // Set the rendering context to the current item

     DocumentId idSA = content.getDirectParent();

     SiteArea sa = (SiteArea) workspace.getById(idSA, true, false);

     renderingContext.setRenderedContent(content, sa);

            // Render the markup

%><%=workspace.render(renderingContext, libComp)%>

<%

      }

%>

In the code above, the final string values for COMPONENT_NAME and DESIGN_LIBRARY_NAME can be read from the request. When no value is provided a default value is assumed.

JSP code with flexibility added


<%@page import="com.ibm.workplace.wcm.api.*"%>


<%@page import="java.util.*"%>

<%@page import="java.text.*"%>

<%@page trimDirectiveWhitespaces="true"%>

<%@page language="java" contentType="text/html; charset=ISO-8859-1"

      pageEncoding="ISO-8859-1"%>

<%@page session="false"%>

<%@taglib uri="/WEB-INF/tld/wcm.tld" prefix="wcm"%>

<wcm:initworkspace />

<%--

  - Author(s): Maarten Pol

  - Date: 03/03/2015

  - Description: JSP to invoke a component from WCM for each result in a list (navigator, menu, search results, etc)

  - Version History

  - 0.1 - September 24, 2014 - Initial version

  - 0.2 - March 3, 2015 - Added flexibility through request parameters

--%>

<%

      String componentName = request.getParameter("component"); // get component name

      if (componentName==null) componentName="Menu - Show Responses"; // assume default value for component name when none provided

      String designLibraryName = request.getParameter("library"); // get design library name

      if (designLibraryName==null) designLibraryName="Design Library"; // assume default value for design library name when none provided

     // Get the current content item. Please note that getCurrentResultId requires both <wcm:initworkspace />

      // and compute="always" in calling the JSP component in the result design for the component

      RenderingContext renderingContext = (RenderingContext) request

                  .getAttribute(Workspace.WCM_RENDERINGCONTEXT_KEY);

      DocumentId currentResultId = renderingContext.getCurrentResultId();

      Workspace workspace = renderingContext.getContent().getSourceWorkspace();

      Content content = (Content) workspace.getById(currentResultId, true, false);

      // Get the component to render

      DocumentLibrary libDesign = (DocumentLibrary) workspace.getDocumentLibrary(designLibraryName);

      workspace.setCurrentDocumentLibrary(libDesign); // get design library

      DocumentIdIterator docIds = workspace.findByName(DocumentTypes.LibraryComponent, componentName,

                  Workspace.WORKFLOWSTATUS_PUBLISHED); // find component by name

      if (docIds.hasNext()) {

            DocumentId docId = (DocumentId) docIds.next();

            LibraryComponent libComp = (LibraryComponent) workspace.getById(docId, true, false);

            // Set the rendering context to the current item

            DocumentId idSA = content.getDirectParent();

            SiteArea sa = (SiteArea) workspace.getById(idSA, true, false);

            renderingContext.setRenderedContent(content, sa);

            // Render the markup

%><%=workspace.render(renderingContext, libComp)%>

<%

      }

%>

Make sure to provide values for the parameters named component and library in the invoking JSP component.

Once you have deployed or updated the provided EAR file, open the JSP component (see part 1 Full Control Over Markup In WCM While Using JSPs) and add parameters to the JSP path:

Change JSP path from:
/wcmcustom;/render/component.jsp
to
/wcmcustom;/render/component.jsp?component=Menu+-+Show+Responses&library=Design+Library

Note: The values of the parameters cannot contain spaces. Replace spaces with + or %20.

View the Blog Landing page. Since we provided the same values as the static values provided before, the page should look exactly the same. However, when you now wish to display different variations of the same page, you can re-use the JSP, simply by providing different parameters.

Other applications

The example above renders components based on parameters passed in to the JSP. When using JSPs to render content, other possible applications of parameters are:

  • pass in the name of an element to use for rendering
  • pass in the name of the library to retrieve the content from
  • pass in the name of the site area to retrieve content from
  • pass in filter values to limit the number of results returned (…&filterByType=blog&…)
  • pass in tag values to limit the number of results returned (…&tag=audio,recording,mastering&…)
  • pass in settings for search methods (AND/OR) (…&searchOperation=AND&…)
  • pass in settings for search results ordering (for anything out of the ordinary)
  • etc

In addition to rendering, JSPs can also be used in authoring templates to extend the functionality offered by WCM. These JSPs take parameters just as well. Provide the JSP path in the Custom JSP field in the element properties defined in the default content tab of the authoring template. Add parameters to the JSP path. Take into account that you can invoke different JSPs for editing and authoring (see http://www-01.ibm.com/support/knowledgecenter/SSHRKX_8.5.0/help/panel_help/wcm_dev_custom_jsp.dita?lang=en)

 

Best practices

Make sure to do the following:

  • Decide if you need run-time parameters passed in to the JSP
  • Determine if parameters can have one or multiple values
  • Check any values passed in for validity
  • Decide what to do with empty values (e.g. &tag=,&category=product&…)
  • Decide on how to handle errors
  • Decide on how to handle large amounts of data
  • Provide default values for parameters that are omitted in the request
  • Keep your JSPs simple; limit complexity for maintenance purposes

Downloads

 

Download and deploy the updated EAR file provided with this article to get a head start:

  • Enterprise Archive (EAR) - (https://dl.dropboxusercontent.com/u/19549758/WCM%20article/Part2/CustomJSPsEAR.ear)

Conclusion

Combine passing in parameters from both the JSP Component in WCM and through the query string in the URL in WebSphere Portal for full flexibility in steering the behaviour of your JSPs.  


  • Edit
  • More Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (5)
collapsed Versions (5)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (5)Mar 3, 2015, 7:17:09 AMMaarten Pol  Minor change
4Mar 3, 2015, 6:32:53 AMMaarten Pol  added resources (EAR file link)
3Mar 3, 2015, 6:25:00 AMMaarten Pol  Minor change
2Mar 3, 2015, 6:24:08 AMMaarten Pol  Minor change
1Mar 3, 2015, 6:23:21 AMMaarten Pol  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedHelpAbout
  • IBM Collaboration Solutions wikis
  • IBM developerWorks
  • IBM Software support
  • Twitter LinkIBMSocialBizUX on Twitter
  • FacebookIBMSocialBizUX on Facebook
  • ForumsLotus product forums
  • BlogsIBM Social Business UX blog
  • Community LinkThe Social Lounge
  • Wiki Help
  • Forgot user name/password
  • Wiki design feedback
  • Content feedback
  • About the wiki
  • About IBM
  • Privacy
  • Accessibility
  • IBM Terms of use
  • Wiki terms of use