Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:00:37+00:00 2026-06-13T18:00:37+00:00

I want to create a custom tag library which should extend the existing Spring

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:00:38+00:00Added an answer on June 13, 2026 at 6:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom tag which would mark a field readonly on
I want to create a custom tag library but in the handler class I
I want to create custom tag, but i get XML parsing error on JSPVersion
i want to know about this custom tag that google use for social plug
I want to create a custom container that supports iterators. It looks like this:
I want to create custom xml serialization by implementing IXmlSerializable. I've got this test
i want create a custom json data from the mssql 2008 results so that
I want to create custom button and I need it to be circle. How
I want to create a custom navigation bar to put in my header.phtml file.
I want to create a custom set that will automatically convert objects into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.