Skip to main content

skip to main content

developerWorks  >  Sample IT projects  >

The Making of MetroSphere, Part 15: Making tag libraries available throughout the portal

Use a custom tag library from outside a portlet application

developerWorks
Document options

Document options requiring JavaScript are not displayed

Discuss


Rate this page

Help us improve this content


Level: Introductory

Nicholas Chase, President, Chase and Chase, Inc.

01 Jul 2003

Adding functionality such as JSP tag libraries to a portlet application is easy: simply include the appropriate files when creating the WAR file. But what if you need to use that tag library outside the portlet application? This tip follows the addition of the blogutil tag library to the MetroSphere portal's registration page.

You'll need to have a working installation of IBM WebSphere Portal Server Version 4.x to follow along with this tip.

Editor's update

The Web site MetroSphere.com -- the online technical community discussed in this article -- is no longer live. However, the information and screen captures regarding the installation of IBM WebSphere Portal are still accurate and relevant.

What we want to accomplish

The MetroSphere portal is a community weblog aimed at technology professionals, and one of our team's major goals is the personalization of content. As such, it's important that we know what categories of information a particular user would like to see. Although the personalization engine that comes with portal provides all kinds of sophisticated ways to figure that out through tracking and comparing a user's actions to those of other users, there is one very simple way for us to know what topics interest a user.

We can ask.

To do that, we're going to add a list of topics to the registration page, as in Figure 1.


Figure 1. The completed page
The completed page

This list is only temporary, of course. As we get closer to completion, we'll refine it further, so it's natural to pull the topics from a database. And because this is just one of many places in which we're displaying a list of topics, it makes sense to use a tag library to do it.



Back to top


Adding the tag library to the page

The body of the registration page is contained in the file:

<WebSphere Home>/PortalServer/app/wps.ear/wps.war/screens/html/UserProfileForm.jsp

In addition to changing the layout of the page slightly, we added a reference to the tag library and the tags themselves:


Listing 1. Adding the tags to the page
<%@ page session="false" buffer="none" %>
<%-- Licensed Materials - Property of IBM, 5724-B88, (C) Copyright IBM Corp. 2001, 2002 - 
All Rights reserved. --%>
 
<%@ taglib uri="/WEB-INF/tld/engine.tld" prefix="wps" %>
<%@ taglib uri="/WEB-INF/tld/blogtaglib.tld" prefix="blogutil" %>
<%@ include file="BidiInclude.jsp" %>

<wps:constants />

<jsp:useBean id="userWrapper" class="com.ibm.wps.puma.UserWrapper" scope="request"/>
<jsp:useBean id="errorBean"  class="com.ibm.wps.puma.UserState" scope="request"/>
<jsp:useBean id="reqAttributes" class="java.util.Vector" scope="request" />

<%@ page import="com.ibm.wps.datastore.LanguageDescriptor" %>
...
                 </td><td style="background-color: #ECECEC" valign="top">
<table>
   <tr><td colspan="3">&nbsp;</td></tr>
   <tr>
       <td align="<%= bidiAlignRight %>"><%=  
           reqAttributes.contains("FAVORITETOPICS")?"*":" &nbsp;&nbsp;" %></td>
       <td align="<%= bidiAlignLeft %>" class="wpsEditText">
           <b><label for="wps.FavoriteTopics"><wps:text key="metro.favTopics" 
                     bundle="nls.metro">Preferred Topics</wps:text></label></b>
       </td>
   </tr>
<blogutil:topiclist selected="-1">
   <jsp:useBean id="topicelement" class="com.metrosphere.util.TopicBean" />
   <tr>
       <td><input type="checkbox" name="topicChoice" 
          value="<jsp:getProperty name="topicelement" property="topicId" />"/>
       </td>
       <td><jsp:getProperty name="topicelement" property="topicName" /></td>
       <td>&nbsp;</td>
   </tr>
</blogutil:topiclist>
</table>

</td></tr>
</table>
<span class ="wpsEditSmText" ><wps:text key="requiredfield" 
   bundle="nls.registration" /></span>
              </td></tr>
        </td>
    </tr>
    </table>
  
<%-- start admin footer --%>
<table cellspacing="4" cellpadding="0" border="0" width="100%">
...
			

In an upcoming article on using custom user attributes, we'll look at making sure that checkboxes are preselected if the user is editing his or her profile rather than creating a new one, but for now we're just thinking about getting the tag library in place. Ultimately, we'll also have to add the topics tags to the UserProfileConf.jsp page.



Back to top


Adding the classes and tag library file

For any of this to work, we need to make sure that the tag classes are available to the portal itself. When we were using the tags within a portlet application, we didn't have to worry about it; we simply included the classes in the application. Now we need to make the classes available to the portal itself.

When using J2EE, there are typically two ways to make a class available to the application. The first, and most convenient, is to take the relevant class files and add them to a JAR file, and then add the JAR file to the /WEB-INF/lib directory. In some cases, however, including some versions of WebSphere Application Server included with WebSphere Portal, these JAR files are not automatically added to the classpath, so you need to add the individual classes to the /WEB-INF/classes directory of the overall application server, taking into account the package hierarchy:

<WebSphere Home>/AppServer/classes/com/metrosphere/util/BlogConnection.class
<WebSphere Home>/AppServer/classes/com/metrosphere/util/TopicBean.class
<WebSphere Home>/AppServer/classes/com/metrosphere/util/TopicListTag.class
<WebSphere Home>/AppServer/classes/com/metrosphere/util/TopicTag.class

We also need to make the tag library definition file, blogtaglib.tld, available to the portal application. The trick is in understanding that what we think of as the "portal" itself is actually a J2EE application in the PortalServer directory. So in order to add the blog utility classes to the overall portal, we need to add the .tld file to the following directory:

<WebSphere Home>/PortalServer/app/wps.ear/wps.war/WEB-INF/tld/

This is the directory referenced by the page directive:

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

You can also use the web.xml file to specify aliases for this tag library.



Back to top


Using the web.xml file

In some cases, you may want to create an alias for the tag library by adding it to the web.xml file. This file is located in:

<WebSphere Home>/PortalServer/app/wps.ear/wps.war/WEB-INF/tld/

To add the tag library to the file, you can simply add a taglib element:


Listing 1. Adding an alias to the web.xml file.
...
    <servlet-mapping id="ServletMapping_tr_3">
        <servlet-name>TranscodingUtilities</servlet-name>
        <url-pattern>/TranscodingUtilities/*</url-pattern>
    </servlet-mapping>
    <taglib>
        <taglib-uri>/WEB-INF/tld/blogtaglib.tld</taglib-uri>
        <taglib-location>/WEB-INF/tld/blogtaglib.tld </taglib-location>
    </taglib>
    <taglib>
        <taglib-uri>engine</taglib-uri>
        <taglib-location>WEB-INF/tld/engine.tld</taglib-location>
    </taglib>
...

One reason to do this is for mantainability. You can create an alias for the file, and then if you decide to change it later, say, to create a new version, you won't have to alter any of the actual JSP files. Instead, you can just make one change to the web.xml file.



Back to top


Summary

By making the topic list tag library available to the portal, you can greatly increase maintainability for pages that are not part of a portlet application. To do that, you add the classes to the classes directory of the application server itself, and the tag library definition to the WEB-INF/tld directory within the PortalServer directory.



Resources



About the author

Nicholas Chase

Nicholas Chase, a Studio B author, has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of an interactive development firm in Clearwater, Florida, and is the author of four books on Web development, including XML Primer Plus (Sams).




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top