English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Custom tags are user-defined elements of the JSP language. When a custom tag is included in a JSP page, it is converted to a servlet, and the tag is converted to the operation of the The operation of the object called tag handler, that is, when the servlet is executed, the Web container calls those operations.
JSP tag extension allows you to create new tags and directly insert them into a JSP page. JSP 2introduced Simple Tag Handlers in the JSP 2.0 specification to write these custom tags.
You can inherit the SimpleTagSupport class and override the doTag() method to develop the simplest custom tag.
Next, we want to create a custom tag called <ex:Hello>, the tag format is as follows:
<ex:Hello />
To create a custom JSP tag, you must first create a Java class that handles the tag. So, let's create a HelloTag class as follows:
package com.w3codebox; 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, which 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 the CLASSPATH directory of the environment variable. Finally, create the following tag library: <Tomcat installation directory>webapps\ROOT\WEB-INF\custom.tld.
The name of the attribute is "message", so the setter method is setMessage(). Now let's add this attribute to the <attribute> element in the TLD file: <taglib-version>jsp</version>1<jsp/<tlib-version>jsp</version> tlib-version>jsp</version>2<jsp/.0</jsp-version>jsp</version> <short-name>Example TLD</short-name> <tag <name>Hello</name>/name> <tag-class>com.w3codebox.HelloTag/tag-class> <body-content>empty</body-content> </tag> </taglib>
Next, we can use the Hello tag in the JSP file as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A Sample Custom Tag</title>/title> </head> body> <ex:Hello/> </body> </html>
The output result of the above program is:
Hello Custom Tag!
You can include message content in the tag like the standard tag library. If we want to include content in our custom Hello, the format is as follows:
<ex:Hello> This is message body </ex:Hello>
We can modify the tag handler class file, the code is as follows:
package com.w3codebox; 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 follows:
The name of the attribute is "message", so the setter method is setMessage(). Now let's add this attribute to the <attribute> element in the TLD file: <taglib-version>jsp</version>1<jsp/<tlib-version>jsp</version> tlib-version>jsp</version>2<jsp/.0</jsp-version>jsp</version> <short-name>Example TLD with Body</name>/short-name> <tag <name>Hello</name>/name> <tag-class>com.w3codebox.HelloTag/tag-class> <body-content>scriptless</content>/body-content> </tag> </taglib>
Now we can use the modified tag in JSP as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A Sample Custom Tag</title>/title> </head> body> <ex:Hello> This is message body </ex:Hello> </body> </html>
The output result of the above program is as follows:
This is message body
You can set various properties in the custom standard. To receive properties, the value custom tag class must implement the setter method, the setter method in JavaBean is as follows:
package com.w3codebox; 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) { /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println(message); else { /* Use message from content 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 add this attribute to the <attribute> element in the TLD file: <taglib-version>jsp</version>1<jsp/<tlib-version>jsp</version> tlib-version>jsp</version>2<jsp/.0</jsp-version>jsp</version> <short-name>Example TLD with Body</name>/short-name> <tag <name>Hello</name>/name> <tag-class>com.w3codebox.HelloTag/tag-class> <body-content>scriptless</content>/body-content> <attribute> <name>message</name>/name> </attribute> </tag> </taglib>
Now we can use the message attribute in JSP files as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A Sample Custom Tag</title>/title> </head> body> <ex:Hello message="This is a custom tag" /> </body> </html>
The output result of the above example data is:
This is a custom tag
You can also include the following attributes:
Attribute | Description |
---|---|
name | Define the name of the attribute. Each tag's attribute name must be unique. |
required | Specify whether the attribute is required or optional. If set to false, it is optional. |
rtexprvalue | Declare whether the tag attribute is valid when the expression is executed. |
type | Define the Java class type of the attribute. The default is String |
description | description information |
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>attribute_name</name> <required>false</required> <type>java.util.Date</type> <fragment>false</fragment> </attribute> .....
If you have used two attributes, modify the TLD file as follows:
..... <attribute> <name>attribute_name1</name> <required>false</required> <type>java.util.Boolean</type> <fragment>false</fragment> </attribute> <attribute> <name>attribute_name2</name> <required>true</required> <type>java.util.Date</type> </attribute> .....