29.01.2013 Views

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

*/<br />

public HelloBean() {<br />

super();<br />

}<br />

/**<br />

* Returns a string saying hello to the name argument.<br />

*<br />

* @return A hello message<br />

* @param name java.lang.String The name to say hello to<br />

*/<br />

public String hello(String name) {<br />

return "Hello " + name;<br />

}<br />

Now we have the syntax for our tag defined, and the bean that provides the<br />

implementation. The rest of the steps are best described as cookbook code. That<br />

is, the code is largely the same whatever tag you are implementing. The classes<br />

and methods you need to define are always the same. The first few times you go<br />

through these steps will perhaps seem tedious, but creating them soon becomes<br />

relatively trivial.<br />

Tag handler class<br />

The tag handler class extends the javax.servlet.jsp.tagext.TagSupport class, and<br />

provides an endTag() method that will be executed when our tag has been<br />

parsed by the Web container. We also need to create an instance variable called<br />

name that will hold the value of our tag’s attribute. Our tag handler class is shown<br />

in Example 9-2. Important sections of code are in bold, and comments are<br />

provided to explain what is happening.<br />

Example 9-2 Simple custom tag example: HelloTag.java<br />

//Simple custom tag example: HelloTag.java<br />

package com.ibm.itso.j2eebook.tagexamples;<br />

import java.io.IOException;<br />

import javax.servlet.jsp.*;<br />

import javax.servlet.jsp.tagext.*;<br />

import com.ibm.itso.j2eebook.tagexamples.beans.*;<br />

/**<br />

* Handles the hello tag.<br />

*<br />

* @author: Adrian Spender<br />

*/<br />

public class HelloTag extends javax.servlet.jsp.tagext.TagSupport {<br />

Chapter 9. Using tag libraries 281

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!