2.20 Portlet JSPs

 <  Day Day Up  >  

When designing your portlet applications, you will generally use the MVC Model 2 architecture discussed in 2.3.2, "Portlet MVC architecture" on page 57. For the development of dynamic portlet JSPs, a rich tag library is provided with WebSphere Portal Server. There are several custom tag libraries supplied with WebSphere Portal Server depending on the installation type and what additional components are installed.

  • portlet.tld This tag library contains the tags used in day-to-day JSP development when working with JSPs.

  • c2a.tld This tag library contains the tags to be used in cooperative portlets using the declarative approach. For details, see Chapter 12, "Cooperative portlets" on page 371.

  • engine.tld This tag library is intended to be used in the construction of themes and skins.

  • extend.tld This tag library is only supplied if the installation type is extend or experience. These tags are not available with the enable installation.

  • content.tld This tag is used in JSPs working with the PortletContent Organizer.

  • menu.tld This tag library provides access to Collaborative functionality in the themes.

  • person.tld This tag library provides access to Collaborative functionality inside your portlets.

2.20.1 Portlet tag library

Like all tag libraries in the WebSphere Portal Server, the portlet.tld is located in the <WP-ROOT>app\wps.ear\wps.war\WEB-INF\tld directory. Example 2-43 illustrates referencing the tag library at the beginning of a JSP.

Example 2-43. Referencing a tag library
 <%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %> 

This section will cover the tags available in the portlet.tld tag library and some of their most common uses.

  • init <port1letAPI:init />

    This tag must be called if you wish to access the PortletRequest, PortletResponse or PortletConfig objects in the JSP. This tag simply initializes three variables for you: portletRequest, portletResponse , and portletConfig . Attempting to access these variables without calling the init tag will cause the page compilation of the JSP to fail. However, you still have full access to the javax.servlet.http.HttpServlet objects via the normal variable names .

  • createReturnURI <portletAPI:ecreateReturnURI />

    This tag returns a String pointing to the portlet in the previous mode. The resulting URI could be used to create a Back button or to specify and an action on a FORM. If you wish to add a PortletAction to the URI object in order to notify any applicable listeners, you can include the URIAction tag in the body of the createReturnURI tags. Example 2-44 illustrates this approach.

    Example 2-44. Adding a PortletAction to the PortletURI
     <portletAPI:createReturnURI >    <portletAPI:URIAction name="submit" /> </portletAPI:createReturnURI> 

    You can also add a parameter to the PortletURI object using a similar approach to the one used with the PortletAction.

    Example 2-45. Adding a Parameter to the PortletURI and the resulting URI
     <portletAPI:createReturnURI >    <portletAPI:URIParameter name="fname" value="john" /> </portletAPI:createReturnURI>  Result:  /wps/myportal/.cmd/ActionDispatcher/_pagr/104/_pa.104/113/.md/-/.piid/188/.ciid /223/.reqid/-1/PC_188_fname/john#223 
  • createURI <portletAPI:createURI />

    This tag returns a String pointing to the portlet in the current mode. As with the createReturnURI tag, PortletActions and parameters can be added to the resulting URI. Though the documentation indicates that the state can be controlled by passing a string attribute, this functionality is not implemented.

  • URIAction <portletAPI:URIAction name="sting"/>

    This tag is only used when creating a PortletURI object. Example 2-44 illustrates the use of this tag. This tag requires that a name attribute be specified.

  • URIParameter <portletAPI:URIParamter name="string" value="string"/>

    This tag is only used when creating a PortletURI object. Example 2-45 on page 119 illustrates the use of this tag. This tag requires that name and value attributes be specified.

  • dataAttribute <portleAPI:dataAttribute name="string" />

    This tag will retrieve from the PortletData object the attribute specified by the name attribute. If the attribute does not exist in the PortletData, nothing is returned. When the dataAttribute tag is used in the body of the a dataLoop tag, it does need to specify the name of the attribute.

    Example 2-46. Retrieving a single PortletData attribute
     Welcome <portletAPI:dataAttribute name = "pref.nick_name" /> to your page. 
  • dataLoop <portletAPI:dataLoop pattern="string">

    </portletAPI:dataLoop>

    This tag provides a loop through all the attributes stored in the PortletData object. Though by default, it will iterate through all attributes, it is possible to specify a pattern to limit the attributes it locates . Omitting the pattern attribute will return all attributes. Example 2-47 illustrates the usage of this tag. Notice the loop simply iterates through the collection of attributes; it does not retrieve the value. To retrieve a PortletData value, use the dataAttribute tag.

    Example 2-47. Looping through the attributes in the PortletData object
     <portletAPI:dataLoop pattern="pref.*">    <portletAPI:dataAttribute/><br> </portletAPI:dataLoop> 

    Though using an asterisk in the pattern is helpful for readability and reliability, the pattern attribute in fact does not need to use an asterisk at all. The tag will attempt to find the value specified by the pattern attribute anywhere in the name of the attribute. For example, if an attribute is stored in the PortletData with the name "pref.greeting" , the code in Example 2-48 would successfully locate the attribute. However, it is important to note that the pattern is case-sensitive. Therefore, the pattern "name" would not locate the attribute "Name" .

    Example 2-48. Using the Pattern attribute
     <portletAPI:dataLoop pattern="eet"> 
  • settingsAttribute <portletAPI:settingAttribute name="string" />

    This tag provides access to the parameters set in the <config-param> blocks in the portlet.xml's concrete portlet section. When the dataAttribute tag is used in the body of the settingsLoop tag, it does need to specify the name of the attribute.

    Example 2-49. Accessing the PortletSettings attributes
     For support contact <portletAPI:settingsAttribute name = "author" /> 
  • settingsLoop <portletAPI:settingsLoop pattern="string">

    </portletAPI:settingsLoop>

    If several configuration parameters have been set in the portlet.xml, they can all be retrieved with this tag. The pattern tag is optional.

    Example 2-50. Looping through the PortletSettings attributes
     <portletAPI:settingsLoop pattern="info.">     <portletAPI:settingsAttribute/><BR> </portletAPI:settingsLoop> 

    If you do not include the pattern attribute or enter an empty string, it will return all attributes in the PortletSettings object. As with the dataLoop tag, the settingsLoop tag will attempt to locate the specified pattern anywhere in the attribute's name. For example, if a <config-param> were set in the portlet.xml with a name of "info.author" , the code in Example 2-51 would successfully retrieve the attribute. However, it is important to note that the pattern is case-sensitive. Therefore the pattern "author" would not locate the attribute "Author" .

    Example 2-51. Using pattern to locate an attribute
     <portletAPI:settingsLoop pattern="thor"> 
  • encodeNameSpace <portletAPI:encodeNamespace value="string" />

    When including JavaScript functions or other variables that will be returned to the aggregated portal page, it is important to ensure the values are unique in order to avoid name collisions. This tag prefixes the namespace of the portlet to the string it is passed. This tag should be used when creating the variable and when accessing it. Example 2-52 on page 121 illustrates the usage and result of this tag.

    Example 2-52. Encoding the name space
     <portletAPI:encodeNamespace value="function1" />  Result:  PC_189_function1 
  • encodeURI

    This tag will prefix the full URL of the portal to the passed path value. For example, if the image yourco.jpg is in the images folder directly under the root of the deployed portlet application, the code shown in Example 2-53 would successfully locate the image and create a fully qualified URL.

    Example 2-53. Creating afully qualified URL
     <img src= <portletAPI:encodeURI path="/images/yourco.jpg" /> >  Result:  http://ka0kkhc.sg246897.com/wps/WPS_PA_206/images/yourco.jpg 
  • if <portletAPI:if attribute= "string">

    </portletAPI:if>

    This tag allows you test some of the more common conditions a portlet may face. When the attribute evaluates to true, the body of the if tag is executed. There are several attributes you can evaluate.

    - mode

    - state

    - locale

    - mime type

    - markup

    - capabilities

    Though the infocenter indicates that a previous mode can be evaluated as well, the previousMode attribute is not functional. You may choose to execute several if statements individually as shown in Example 2-54 on page 122.

    Example 2-54. Executing If tags individually
     <portletAPI:if state = "Normal"> state is normal </portletAPI:if> <portletAPI:if state = "Maximized"> state is maximized </portletAPI:if> <portletAPI:if locale = "en"> Locale is english </portletAPI:if> <portletAPI:if markup = "html"> Markup is html </portletAPI:if>< <portletAPI:if mimetype = "text/html"> mime type is text html </portletAPI:if><BR> <portletAPI:if mode="view"> Mode is View </portletAPI:if ><BR> 

    You can evaluate more than one condition on a single attribute. In this case, if any of the conditions are true, that attribute will evaluate to true. Example 2-55 illustrates this.

    Example 2-55. Evaluating multiple conditions on a single attribute
     <portletAPI:if state="Normal, Maximized" > 

    You may also evaluate multiple attributes in the same tag as illustrated in Example 2-56. All conditions must evaluate to true for the if tag to return true.

    Example 2-56. Evaluating multiple attributes
     <portletAPI:if state="Normal" mode="view" localE="en">    Displaying the normal English view </portletAPI:if> 
  • log <porteltAPI:log text="sting" level="string"/>

    This tag will write the value passed to the log file located in the <WP-ROOT>\log directory. The text attribute contains the string you wish to write to the log file. The level attribute indicates which level this message should be written under. This tag does not evaluate whether the requested level is enabled before it attempts to write the message. For more information on writing to the log, see 2.9.12, "PortletLog object" on page 92. Example 2-57 illustrates the usage of this tag. The valid values for the level attribute are error, warn, debug and info. If you omit the level tag, the default level is error.

    Example 2-57. Using the log tag
     <portletAPI:log text="There was an error" level="warn"/> 
  • text <portletAPI:text key="string" bundle="string">

    Note

    The text tag has been deprecated in this release. You should now use the fmt tag from the JSP Standard Tag Library (JSTL). For details, see Chapter 8, "National Language Support (NLS)" on page 249.


    This tag was used to provide access to key-value pairs in resource bundles.

  • bidi <portletAPI:bidi locale="string" dir="ltr rtl" />

    This tag is used to support text for bidirectional languages. Bidirectional languages are read from right to left or from bottom to top. The attributes are not required. For example, if the request indicates that the client is Hebrew or Arabic, it will execute the tag contents if dir is set to rtl .

 <  Day Day Up  >  


IBM WebSphere Portal V5 A Guide for Portlet Application Development
IBM Websphere Portal V5: A Guide for Portlet Application Development
ISBN: 0738498513
EAN: 2147483647
Year: 2004
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net