JSP and JSTL Tutorials - Tutorial Addendum - JSP Custom Tag
| |
What is a Custom Tag?
Custom Tag is an activity tag authentic by the user through the JSP tag addendum facility.
It can be acclimated to move JSP page assembly logics and advice into a tag Java class, and invoke
it by an activity tag that is affiliated to that class. There are two capital advantages of using
custom tags:
- Repeatable JSP page logics and advice can be simplified and centralized into a individual tag.
For example, we can ascertain a custom tag alleged <my:copyright/> for bearing
the absorb advice that charge to be acclimated on every page of server.
- Moving circuitous business logics from the JSP to a tag class, so the JSP page
author can apply on the presentation logics only. For example, we can ascertain a
custom tag alleged <my:userList/> for bearing a HTML table abounding with a account of users.
The tag chic will administer how the put anniversary user into a row, and anniversary user acreage
into a column.
I am abiding that the functionalities provided by custom tags can aswell be archived by
using JavaBean and scripting elements together. But tags assume to be simpler to use
for some artless JSP page authors.
"Hello world!" Custom Tag
Before we go into any abstruse details, let me use a actual simple archetype to appearance
you the accomplish to ascertain and use a custom tag.
I wish to ascertain a tag alleged <hy:hello/> to aftermath the "Hello world!" in the calling
JSP page.
1. Autograph the tag class. Actuality is my first tag class, HelloTag.java, which extends
the TagSupport chic provided in the JSP package:
/**
* HelloTag.java
* Absorb (c) 2003 by Dr. Yang. All rights reserved.
*/
import java.io.*;
import javax.servlet.jsp.tagext.*;
public chic HelloTag extends TagSupport {
accessible int doStartTag() {
try {
pageContext.getOut().write("Hello world!");
} bolt (IOException e) {
System.err.println(e.toString());
}
acknowledgment SKIP_BODY;
}
}
2. Installing the tag class. I aggregate HelloTag.java with JDK 1.3.1, and servlet.jar
provided by the Bobcat 4.1.18 server. Like the JavaBean chic files, tag chic files
also charge to be installed in the chic aisle of the bobcat server. So I affected
the HelloTag.class to localjakarta-tomcat-4.1.18webapps
ootweb-infclasses directory.
(Continued on next part...)
|
class, custom, hellotag, logics, define, world, tomcat, provided, called, information, example, copyright, server, , custom tag, tag class, jsp page, hellotag java, tag called, called <, tutorials tutorial notes, jstl tutorials tutorial, |
Also see ...
i(Continued from antecedent part...)/i3. Autograph the tag library descriptor (tld) file. Now, I charge to ascertain a tag in a tag librarydescriptor book to use the tag class. Actuality is my first tld file, HyT
Tag Aspect Setter MethodThe JSP tag addendum ability maps anniversary aspect encountered in a customtag to an acreage of the aforementioned name of the tag object. So if you wish b
i(Continued from antecedent part...)/iTag Aspect Amount Blazon ConversionIn my antecedent example, the aspect amount is anesthetized to the tag item propertyas a cord
i(Continued from antecedent part...)/iThe JSP file: <?xml version="1.0"?<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jstl/core"
i(Continued from antecedent part...)/iTag Aspect Amount Announcement Archetype AttObjectTag.javaThe tag class: /** * AttObjectTag.java
javax.servlet.jsp.tagext.* Packagejavax.servlet.jsp.tagext.* is a Java amalgamation authentic in J2EE (I saw it in J2EE 1.3.1). This amalgamation contains:
i(Continued from antecedent part...)/iImplenting BodyTag Interface TraceTag.javaIn adjustment to affirm my compassionate of the IterationTag interface, I wrotethe af
i(Continued from antecedent part...)/iThe Servlet Chic TraceTagTest_jsp.javaAs we all understand that JSP pages are translated into Servlet classes beforeexecution.
Nested JSTL TagsWhen tags are nested, JSP tag addendum ability offers the adolescent tag anaccess to the ancestor tag by the setParent() adjustment call. You can yield advantage
i(Continued from antecedent part...)/iI am abiding you can apprehend this page, and accept what I am accomplishing with the loop. Warning, my "break" tag is not a absolutely breach statement. If there is any addi