Create a custom tag library which extends the Spring tag library

Bhavik Ambani picture Bhavik Ambani · Oct 27, 2012 · Viewed 23.1k times · Source

I want to create a custom tag library which should extend the existing Spring MVC 3.0 tag library. I want to do this because I want my JSP code to be independent of any framework.

That means, if I want to change from Spring to Struts then I don't have any need to change anything in JSP pages. I just change my customized tag library which will extend the Struts tag library and all work fine.

Answer

slayer_b picture slayer_b · Oct 29, 2012

You can not extend the whole library, but you can extend all tags from the library and create a new descriptor for them, and then use your own tags instead of the Spring ones.

For example, go to the file named spring-form.tld. You will see tag descriptors, which contain attributes description and a tag class name.

So to have your own tag library, you have to create:

  1. my-lib.tld (specify [uri for a library])
  2. Extend all tags you need
  3. Put descriptors to my-lib.tld
  4. Use a URI in my-lib.tld instead of the Spring ones

Just search Google for 'jsp custom tags'. Or take a look at JSP custom tags.

For example, take two classes for the [form] tag from Struts and Spring:

  • org.apache.struts.taglib.html.FormTag
  • org.springframework.web.servlet.tags.form.FormTag.

You will have to create something like:

package org.my.example.tags;

import javax.servlet.jsp.JspException;

import org.springframework.web.servlet.tags.form.FormTag;
import org.springframework.web.servlet.tags.form.TagWriter;

/**
 */
public class SpringFormTag extends FormTag {
    private static final String FOCUS_ATTRIBUTE = "focus";
    private String focus;

    public void setFocus(String focus) {
        this.focus = focus;
    }

    public String getFocus() {
        return focus;
    }

    @Override
    protected void writeDefaultAttributes(TagWriter tagWriter) throws JspException {
        writeOptionalAttribute(tagWriter, FOCUS_ATTRIBUTE, getFocus());
        super.writeDefaultAttributes(tagWriter);
    }
}

I am posting only code for spring's form tag.

File my-lib.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    <description>My tag library</description>
    <tlib-version>3.0</tlib-version>
    <short-name>html</short-name>
    <uri>http://test.com/test.tld</uri>
    <tag>
        <name>form</name>
        <tag-class>org.my.example.tags.SpringFormTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>action</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>acceptCharset</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>dir</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>disabled</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>boolean</type>
        </attribute>
        <attribute>
            <name>enctype</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>focus</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>focusIndex</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>lang</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>method</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>onreset</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>onsubmit</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>readonly</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>boolean</type>
        </attribute>
        <attribute>
            <name>scriptLanguage</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>boolean</type>
        </attribute>
        <attribute>
            <name>style</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>styleClass</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>styleId</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>target</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

You will also have to put the .tld file into the META-INF directory of a JAR file. The JAR file with this taglibrary must be just a JAR file included to your WAR file, otherwise taglibraries will not be detected.

And then include your taglib into the JSP file:

<%@ taglib prefix="html" uri="http://test.com/test.tld" %>

And use it:

<html:form action="asd" focus="1">
    <div><input type="text"></div>
    <div><input type="submit"></div>
</html:form>

You will have to create such a library for Struts also, if you want to switch between them.

The only thing that you will need to remember when doing this is that Spring and Struts have a little bit different tag definitions so Struts have 'focus' and Spring doesn't. I think there may be more differences.

You will have to make your tag to have all attributes from Spring and from Struts, if you really want to switch from one to another. But I don't really think that it is worth the effort.