Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Listing 11.1 shows the first example of using custom tags. The example creates an new tag that iterates through a section of the JSP page a certain number of times. The functional components will be discussed as after each component has been presented.
|
Code View:
Scroll
/
Show All
package com.javadesktop.utiltags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class LoopTag extends BodyTagSupport {
private int maxCount = 0;
private int currentCount = 0;
public void setCount(int count) {
this.maxCount = count;
}
public int doStartTag() {
if (currentCount < maxCount) {
return EVAL_BODY_TAG;
} else {
return SKIP_BODY;
}
}
public int doAfterBody() throws JspException {
currentCount++;
if (currentCount < maxCount) {
return EVAL_BODY_TAG;
} else {
return SKIP_BODY;
}
}
public int doEndTag() throws JspException {
try {
if(bodyContent != null)
bodyContent.writeOut(bodyContent.getEnclosingWriter());
} catch(java.io.IOException e) {
throw new JspException("IO Error: " + e.getMessage());
}
return EVAL_PAGE;
}
public void release() {
super.release();
currentCount = 0;
}
}
|