Latest web development tutorials

JSP custom tags

Custom labels are user-defined JSP language elements. When the JSP page contains a custom tag will be converted to servlet, label conversion for the object is called a tag handler's operations that, when executed Web container servlet call those operations.

JSP tag extension allows you to create a new label and can be inserted directly into a JSP page. JSP 2.0 specification introduced Simple Tag Handlers to write these custom tags.

You can inherit SimpleTagSupport class and override the doTag () method to develop a simple custom labels.


Create a "Hello" label

Next, we want to create a custom label called <ex: Hello>, tag format:

<ex:Hello />

To create a custom JSP tags, you must first create a label deal with Java classes. So, let's create a HelloTag class, as follows:

package com.w3big;

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, the method used getJspContext () method to get the current JspContext object, and "Hello Custom Tag!" Is passed to JspWriter object.

Compile the above classes, and copy it to the environment variable CLASSPATH directory. Finally, create the following tag library: <Tomcat installation directory> webapps \ ROOT \ WEB-INF \ custom.tld.

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.w3big.HelloTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>

Next, we can use the Hello tag in a JSP file:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello/>
  </body>
</html>

The above program output is:

Hello Custom Tag!

Access tab body

You can be like the same standard tag library contains the message content in the label. As we want to include content in Hello, we custom format is as follows:

<ex:Hello>
   This is message body
</ex:Hello>

We can modify the tag handler class file, as follows:

package com.w3big;

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:

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.w3big.HelloTag</tag-class>
    <body-content>scriptless</body-content>
  </tag>
</taglib>

Now we can use the modified label in the JSP, as follows:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello>
        This is message body
    </ex:Hello>
  </body>
</html>

The above program output is as follows:

This is message body

Custom tag attributes

You can set custom criteria in various properties, to receive the property, the value of the custom tag class must implement setter method, the JavaBean setter methods are as follows:

package com.w3big;

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) {
          /* 从属性中使用消息 */
          JspWriter out = getJspContext().getOut();
          out.println( message );
       }
       else {
          /* 从内容体中使用消息 */
          getJspBody().invoke(sw);
          getJspContext().getOut().println(sw.toString());
       }
   }

}

Name of the property is "message", so the setter is the setMessage (). Now let's use the TLD file <attribute> element to add this property:

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD with Body</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.w3big.HelloTag</tag-class>
    <body-content>scriptless</body-content>
    <attribute>
       <name>message</name>
    </attribute>
  </tag>
</taglib>

Now we can use message properties in the JSP file, as shown below:

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello message="This is custom tag" />
  </body>
</html>

Examples of the above data output is:

This is custom tag

You can also include the following properties:

Attributes description
name Define the name of the property. Each tag is the attribute name must be unique.
required Specifies whether the attribute is required or optional, if set to false optional.
rtexprvalue When you run the statement expression, tag attributes are valid.
type Define the Java class type of the property. The default is specified asString
description Description
fragment If you declare the property, the property value will be treated as aJspFragment.

These are the attributes associated with the specified instance:

.....
    <attribute>
      <name>attribute_name</name>
      <required>false</required>
      <type>java.util.Date</type>
      <fragment>false</fragment>
    </attribute>
.....

If you use two properties, modify 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>
.....