JSP Custom Tags
Custom tags are user-defined JSP language elements. When a JSP page containing a custom tag is translated into a servlet, the tag is converted into operations on an object called a tag handler, which the Web container invokes when the servlet executes.
JSP tag extensions let you create new tags that can be inserted directly into a JSP page. The JSP 2.0 specification introduced Simple Tag Handlers for writing these custom tags.
You can extend the SimpleTagSupport class and override the doTag() method to develop the simplest custom tag.
Creating the "Hello" Tag
Next, we want to create a custom tag calledex:Hellowith the tag format:
To create a custom JSP tag, you must first create the Java class that handles the tag. So let's create a HelloTag class as follows:
package com.runoob;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.println("Hello Custom Tag!");
}
}
The following code overrides the doTag() method; it uses the getJspContext() method to obtain the current JspContext object and passes "Hello Custom Tag!" to the JspWriter object.
Compile the above class and copy it to a directory in the CLASSPATH environment variable. Finally, create the following tag library:
1.0
2.0
Example TLD
Hello
com.runoob.HelloTag
empty
Next, we can use the Hello tag in a JSP file:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
A sample custom tag
The output of the above program is:
Hello Custom Tag!
Accessing the Tag Body
You can include message content in a tag just as with a standard tag library. If we want to include content in our custom Hello tag, the format is as follows:
This is message body
We can modify the tag handler class file; the code is as follows:
package com.runoob;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
StringWriter sw = new StringWriter();
public void doTag()
throws JspException, IOException
{
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
Next we need to modify the TLD file, as shown below:
1.0
2.0
Example TLD with Body
Hello
com.runoob.HelloTag
scriptless
Now we can use the modified tag in JSP, as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
A sample custom tag
This is message body
The output of the above program is as follows:
This is message body
Custom Tag Attributes
You can set various attributes in a custom tag. To receive attributes, the custom tag class must implement setter methods; a setter method in a JavaBean looks like this:
package com.runoob;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
private String message;
public void setMessage(String msg) {
this.message = msg;
}
StringWriter sw = new StringWriter();
public void doTag()
throws JspException, IOException
{
if (message != null) {
/* Using messages in a subordinate */
JspWriter out = getJspContext().getOut();
out.println( message );
}
else {
/* Use message from body */
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
}
The name of the attribute is "message", so the setter method is setMessage(). Now let's use the
1.0
2.0
Example TLD with Body
Hello
com.runoob.HelloTag
scriptless
message
Now we can use the message attribute in a JSP file, as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
A sample custom tag
The output of the above example is:
This is custom tag
You can also include the following attributes:
| property | Description |
|---|---|
| name | Defines the name of the attribute. The attribute name must be unique for each tag. |
| required | Specifies whether the attribute is required or optional; if set to false, it is optional. |
| rtexprvalue | Declares whether the tag attribute is valid when used as a runtime expression. |
| type | Defines the Java class type of the attribute. The default is String |
| description | Description |
| fragment | If this attribute is declared, the attribute value will be treated as a JspFragment。 |
The following is an example of specifying related attributes:
.....
attribute_name
false
java.util.Date
false
.....
If you use two attributes, modify the TLD file as follows:
.....
attribute_name1
false
java.util.Boolean
false
attribute_name2
true
java.util.Date
.....