Create a custom tag library which extends the Spring tag library

23,583

Solution 1

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.

Solution 2

I have actually done something similar to what you are asking. We have a number of projects that have to have the same design, usability and ease of maintenance. Since we use Spring MVC, some of my tags are wrappers around spring form tags. For example an input tag:

import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.tags.form.InputTag;

public class InputText extends AbstractInputTag {

    private String maxlength;

    @Override
    public void initInput() throws Exception {
        InputTag input = new InputTag();
        if ( StringUtils.isNotEmpty( maxlength ) ) {
            input.setMaxlength( maxlength );
        }
        setInput( input );
    }

    public void setMaxlength( String maxlength ) {
        this.maxlength = maxlength;
    }
}  

It extends from an abstract tag that adds a label to the input and also takes care of error codes:

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.tags.form.AbstractHtmlInputElementTag;
import org.springframework.web.servlet.tags.form.ErrorsTag;

public abstract class AbstractInputTag extends AbstractTag {

    private String path;
    private String code;
    private String labelWidth = "35%";
    private String valueWidth = "";
    private AbstractHtmlInputElementTag input;

    @Override
    public int doStart() throws Exception {
        TaglibUtils.assertHasAncestorOfType( this, Form.class );
        initInput();

        TaglibUtils.doLabel( pageContext, getId(), code, labelWidth );

        setParent( input );
        input.setPageContext( pageContext );
        input.setCssErrorClass( "zsa-validationerror-input" );
        input.setPath( path );
        input.setId( getId() );

        if( !StringUtils.isEmpty( valueWidth ) ) {
            input.setCssStyle( "width: " + valueWidth );
        }
        pageContext.getOut().print( "<div>" );
        input.doStartTag();
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEnd() throws Exception {
        input.doEndTag();
        input.release();
        pageContext.getOut().print( "</div>" );

        ErrorsTag errorsTag = new ErrorsTag();
        errorsTag.setParent( this );
        errorsTag.setPageContext( pageContext );
        errorsTag.setPath( path );
        errorsTag.doStartTag();
        JspWriter out = pageContext.pushBody();
        out.print( "<span class=\"zsa-validationerror-flag\"></span>" );
        errorsTag.setBodyContent( ( BodyContent )out );
        errorsTag.doInitBody();
        errorsTag.doAfterBody();
        errorsTag.doEndTag();
        out = pageContext.popBody();
        errorsTag.release();
        return EVAL_PAGE;
    }

    public abstract void initInput() throws Exception;
}

Before my forms looked something like this:

 <portlet:actionURL var="coreSearch" portletMode="view" windowState="NORMAL"></portlet:actionURL>
   <form:form commandName="searchContext" id="searchComplex" action="${coreSearch}" method="POST">
    <div class="rightDivPart" style="width: 50%; padding-left: 50px">
        <label class="label" for="tin">TIN: </label>
        <form:input cssErrorClass="inputErrorClass" path="tin" cssClass="medium"/>
     ...       

After making wrapper tags they now look like this:

<ics:form id="searchComplex" commandName="searchContext" action="searchComplex" type="action">
    <ics:panel id="searchComplex">
      <ics:inputText id="mrn" path="mrnCriteria.mrn"/>
    </ics:panel>        
</ics:form>  

Now my JSP-s are moslty free of JavaScript, Css, JQuery and other tags. In a very large project I think it's worth the invesment because of the ease of maintenance.

Solution 3

You can encapsulate your behavior into the tag files. This will be some kind of composition if applying to spring tag library or any other tag library.

Share:
23,583
Bhavik Ambani
Author by

Bhavik Ambani

Working as Architect at Myntra, Bangalore. GitHub Hacker Rank Quora Career 2.0 Profile LinkedIn Profile Facebook Google+ Skype Id - bhavik-ambani Bhavik Ambani

Updated on July 05, 2022

Comments

  • Bhavik Ambani
    Bhavik Ambani almost 2 years

    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.