Developing Custom Tag Libraries

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 11.  Creating Your Own Tag Libraries


Developing Custom Tag Libraries

While our goal in this chapter is to develop a custom tag library that encapsulates the database management of our forum application, this is not to say that we will completely replace the use of JSTL. Rather, we want to design a number of custom tags that are capable of processing forum information. We will still use JSTL for formatting, iteration, flow, and other basic presentation-level issues.

It is important to correctly spread the application across the necessary tags. This requires some design to determine the best mix of classes and tags that will be used by your JSP pages. In the next section, we examine the tags that we created for our forum application.

Tags Used by the Forum Application

The forum application's tag library consists of 16 tags. These tags are summarized in Table 11.1.

Table 11.1. Custom Tags Used by the Forum Application

Tag Name

Attributes

Purpose

createForum

code, name, sequence

Creates a new forum.

deleteForum

code

Deletes an existing forum.

deleteMessage

code, number

Deletes a forum message.

deleteUser

id

Deletes a user.

editForum

code, name, sequence

Edits a forum.

editUser

id, type

Edits a user.

expire

 

Checks to see if the login has expired.

listForums

scope, var

Lists all of the forums.

listMessages

code, scope, var

Lists all of the messages in a forum.

listUsers

var

Lists all of the users.

loadForum

code, scope, var

Loads a forum.

loadUser

id, scope,var

Loads a user.

loginUser

id, password, scope, var

Logs in the specified user.

newUser

id, password, scope, var

Creates a new user.

postMessage

code, from, message, subject

Posts a new message.

saveUser

var

Saves the specified user.

These tags are used throughout the JSP pages that make up our forum application. The structure of the JSP pages remains the same. The only difference is that each of these JSP pages uses the tags listed in Table 11.1 rather than the JSTL SQL tags to process information.

If you examine the tags in Table 11.1, you'll notice that they map closely to the SQL queries and updates performed by our forum application. This allows implementation details to be separated from the presentations. For example, when a user first logs in, the last login, number of failed logins, and number of connections are all updated. In our forum application, this update task was performed by a SQL UPDATE command that we embedded into the login page. Now we have a tag called <forum:loginUser> that performs this operation. The JSP programmer simply calls <forum:loginUser> and the user is logged in. The JSP programmer does not need to be concerned with what data is updated behind the scenes as the <forum:loginUser> tag is executed.

When the <forum:loginUser> tag is called and a successful login occurs, an object is returned that contains the information specific to the user who just logged in. In this case, a class named User is returned that contains the user. The other tags used by the forum application return data in a variety of ways. Some return simple strings or numbers, but most use three classes that we created to hold the data for users, forums, and messages. We examine these data classes in the next section.

For this application, we chose to use a number of tags that process data in the form of several data classes. In the next sections, we discuss the overall structure of the forum application.

Data Structures Used by the Forum Application

As you will recall from Chapter 7, "Accessing Data with SQL Tags," three primary tables make up our forum application. These three tables store the users, forums, and messages. To implement the tag library, we need to create three Java classes capable of holding each of these three record types. The first of these is the User class.

Storing Users

The forum application stores basic information about the users that access it. This data is stored in the t_users table. When the forum application uses the tag library to access data from the t_users table, it returns records using the User class, shown in Listing 11.1.

Listing 11.1 The User Class (User.java)
package com.heaton.forum; import java.util.*; public class User  {   /**    * The login id of this user.    */   private String id;   /**    * The password for this user.    */   private String password;   /**    * The number of times the user has accessed the forum.    */   private int accesses;   /**    * The first time time the user has connected.    */   private Date first;   /**    * The last time the user has logged on.    */   private Date last;   /**    * The number of failed logins for this user.    */   private int bad;   /**    * The number of messages posted.    */   private int posted;   /**    * The type of this user.    * A = admin    * R = regular    * G = guest    */   private String type;   public String getId()   {     return this.id;   }   public void setId(String id)   {     this.id = id.toLowerCase();   }   public String getPassword()   {     return this.password;   }   public void setPassword(String password)   {     this.password = password;   }   public int getAccesses()   {     return this.accesses;   }   public void setAccesses(int accesses)   {     this.accesses = accesses;   }   public Date getFirst()   {     return this.first;   }   public void setFirst(Date first)   {     this.first = first;   }   public Date getLast()   {     return this.last;   }   public void setLast(Date last)   {     this.last = last;   }   public int getBad()   {     return this.bad;   }   public void setBad(int bad)   {     this.bad = bad;   }   public int getPosted()   {     return this.posted;   }   public void setPosted(int posted)   {     this.posted = posted;   }   public String getType()   {     return this.type;   }   public void setType(String type)   {     this.type = type;   } } 

Listing 11.1 contains an ordinary Java data class that has one field property for each of the fields in the underlying table.

Storing Forums

The t_forums table stores the individual forums where the users post and read messages. When the forum application uses the tag library to access data from the t_forums table, it returns records using the Forum class, shown in Listing 11.2.

Listing 11.2 The Forum Class (Forum.java)
package com.heaton.forum; import java.util.*; public class Forum {   /**    * The code that identifies this forum.    */   private String code;   /**    * The name of this forum    */   private String name;   /**    * The sequence number of this forum.    * Forum sequence numbers are used to    * determine the order that the forums are    * listed in.    */   private int sequence;   public String getCode()   {     return this.code;   }   public void setCode(String code)   {     this.code = code.toUpperCase();   }   public String getName()   {     return this.name;   }   public void setName(String name)   {     this.name = name;   }   public int getSequence()   {     return this.sequence;   }   public void setSequence(int sequence)   {     this.sequence = sequence;   } } 

This structure represents only the forums, or message areas. The Forum class does not store individual messages. All individual messages are stored in a common table. Let's now examine the Java class used to hold individual messages.

Storing Messages

A forum may have many different messages. These messages are stored in the t_messages table. The individual records from this table are stored in instances of the Message class, shown in Listing 11.3.

Listing 11.3 Storing a Message (Message.java)
package com.heaton.forum; import java.util.*; public class Message {   /**    * The forum code that designates the forum    * that this message belongs to.    */   private String forumCode;   /**    * The message number.    */   private int number;   /**    * When this message was posted.    */   private Date posted;   /**    * The subject of this message.    */   private String subject;   /**    * The sender of this message.    */   private String sender;   /**    * The message body.    */   private String message;   public String getForumCode()   {     return this.forumCode;   }   public void setForumCode(String forumCode)   {     this.forumCode = forumCode.toUpperCase();   }   public int getNumber()   {     return this.number;   }   public void setNumber(int number)   {     this.number = number;   }   public Date getPosted()   {     return this.posted;   }   public void setPosted(Date posted)   {     this.posted = posted;   }   public String getSubject()   {     return this.subject;   }   public void setSubject(String subject)   {     this.subject = subject;   }   public String getSender()   {     return this.sender;   }   public void setSender(String sender)   {     this.sender = sender;   }   public String getMessage()   {     return this.message;   }   public void setMessage(String message)   {     this.message = message;   } } 

    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch11lev1sec1]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

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