Java – create a custom tag library that extends the spring tag library

I want to create a custom tag library that 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

This means that if I want to change from spring to struts, I don't need to change anything in the JSP page I just changed my custom tag library, it will extend the struts tag library, and everything works normally

Solution

You cannot extend the entire library, but you can extend all tags in the library, create new descriptors for them, and then use your own tags instead of spring tags

For example, go to a file named spring - form TLD file You will see the tag descriptor, which contains the attribute description and tag class name

Therefore, to have your own tag library, you must create:

> my-lib. TLD (specify [the URI of the library]) > extend all the tags you need > put the descriptor in my lib In TLD > in my lib Use URI in TLD instead of spring

Just search Google for "JSP custom tags" Or look at @ L_ 301_ 5@.

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 similar:

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 only published the code of 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 must also The TLD file is placed in the meta - inf directory of the jar file The jar file with this taglibrary must be just the jar file contained in the war file, otherwise taglibraries. Will not be detected

Then include your taglib in 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>

If you want to switch between struts, you must also create such a library for struts

When doing this, the only thing you need to remember is that spring and struts have some different tag definitions, so struts has' focus' and spring does not I think there may be more differences

If you really want to switch from one to another, you must make your tag have all the properties of spring and struts But I really don't think it's worth it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>