Foundations of JSP design patterns: The View Helper pattern

Learn how the View Helper pattern helps you adapt model data to an application's presentation layer

1 2 3 Page 3
Page 3 of 3
 package jspbook.ch08;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;
import java.io.BufferedReader;
import java.io.IOException;
public class ListTag extends BodyTagSupport {
   /* Tag Attributes */
   protected String format;
   /* Static Constants */
   private final static String BULLET_ORB = "orb";
   private final static String BULLET_PLUS = "plus";
   private final static String BULLET_ARROW = "arrow";
   /* Process Tag Body */
   public int doAfterBody() throws JspTagException {
      try {
         BodyContent body = getBodyContent();
         JspWriter out = body.getEnclosingWriter();
         /* Parse records and output as formatted list */
         BufferedReader contentReader = new BufferedReader(body.getReader());
         String record = "";
         while ((record = contentReader.readLine()) != null) {
            if (record.trim().length() > 0) {
               out.println(formatListItem(record.trim()));
            }
         }
      }
      catch (IOException e) {
         throw new JspTagException(e.toString());
      }
      return SKIP_BODY;
    }
   /* Process End Tag */
   public int doEndTag() throws JspTagException {
      return EVAL_PAGE;
   }
   private String formatListItem (String _input)
   {
      StringBuffer listItem = new StringBuffer();
      /* Double-space the list */
      listItem.append("<br/><br/>");
      if(format.equals(BULLET_ORB)) {
         listItem.append("<img src='/jspBook/images/orb.gif'/>");
      } else if (format.equals(BULLET_PLUS)) {
         listItem.append("<img src='/jspBook/images/plus.gif'/>");
      } else if (format.equals(BULLET_ARROW)) {
         listItem.append("<img src='/jspBook/images/arrow.gif'/>");
      }
      listItem.append(" ").append(_input);
      return listItem.toString();
   }
   /* Attribute Accessor Methods */
   public String getFormat ()
   {
      return this.format;
   }
   public void setFormat (String _format)
   {
      this.format = _format;
   }
}

In this example, you could have built a JavaBean to hold the model as you did in previous examples, but you probably get the idea of how you use these helpers to adapt actual model data to presentation code. So, in your JSP page, you simply hard-code the list items into the tag bodies rather than pull the values out of a model.

Listing 10 shows the code for the JSP page. Figure 6 shows the formatted lists.

Listing 10. listHelper.jsp

 <%-- Declare tag that we'll use as our helper --%>
<%@ taglib uri="/helpers" prefix="helpers" %>
<html>
   <head>
      <title>List Examples</title>
   </head>
   <body>
      <font face="Arial"/>
      <center>
         <h1>List Examples</h1>
         <table width="650">
            <tr>
               <td valign="top" width="150">
                  <helpers:ListTag format="orb">
                     High Card
                     Pair
                     Two Pair
                     Three of a Kind
                     Straight
                     Flush
                     Full House
                     Four of a Kind
                     Straight Flush
                     Royal Flush
                  </helpers:ListTag>
               </td>
               <td valign="top" width="150">
                  <helpers:ListTag format="plus">
                     Milwaukee Bucks
                     Detroit Pistons
                     Toronto Raptors
                     Indiana Pacers
                     Charlotte Hornets
                     Cleveland Cavaliers
                     Atlanta Hawks
                     Chicago Bulls
                  </helpers:ListTag>
               </td>
               <td valign="top" width="300">
                  <helpers:ListTag format="arrow">
                     Chapter 1 - The History of Cheese
                     Chapter 2 - The Many Faces of Cheese
                     Chapter 3 - Love and Cheese
                     Chapter 4 - Not Just for Mice
                     Chapter 5 - So You're a Cheesehead...
                     Chapter 6 - The Perfect Cheese
                     Chapter 7 - Cheddar Is Better
                     Chapter 8 - The Big Cheese
                  </helpers:ListTag>
               </td>
            </tr>
         </table>
      </center>
   </body>
</html>
Figure 6. List helper example. Click on thumbnail to view full-sized image.

Summary

This concludes the discussion of the J2EE presentation patterns as applied to JSP pages and servlets. When used in combination, they can produce a powerful request-handling framework for enterprise Web applications.

Andrew Patzer is a Web architect for a consulting firm located in the Midwest. His first book, Professional Java Server Programming, is a bestseller and one of the first books to cover J2EE technologies. Patzer recently served as a lead systems architect for an industry-leading application service provider in the insurance industry. He was directly involved in designing and building a J2EE development framework upon which the company's key product was built. Patzer has delivered several presentations over the years to both local user groups and national conferences.

Learn more about this topic

This story, "Foundations of JSP design patterns: The View Helper pattern" was originally published by JavaWorld.

Copyright © 2004 IDG Communications, Inc.

1 2 3 Page 3
Page 3 of 3