你不能扩展整个库,但是你可以扩展库中的所有标签并为它们创建一个新的描述符,然后使用你自己的标签而不是Spring的标签。
例如,转到名为spring-form.tld 的文件。您将看到标签描述符,其中包含属性描述和标签类名称。
所以要拥有自己的标签库,你必须创建:
- my-lib.tld(指定[库的uri])
- 扩展您需要的所有标签
- 将描述符放入 my-lib.tld
- 在 my-lib.tld 中使用 URI 而不是 Spring 的
只需在 Google 上搜索“jsp 自定义标签”即可。或者看看JSP custom tags。
例如,从Struts和Spring中为[form]标签取两个类:
- org.apache.struts.taglib.html.FormTag
- org.springframework.web.servlet.tags.form.FormTag.
你必须创建类似的东西:
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);
}
}
我只发布 spring 表单标签的代码。
文件 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>
您还必须将 .tld 文件放入 JAR 文件的 META-INF 目录中。带有此标签库的 JAR 文件必须只是包含在您的 WAR 文件中的 JAR 文件,否则将无法检测到标签库。
然后将你的 taglib 包含到 JSP 文件中:
<%@ taglib prefix="html" uri="http://test.com/test.tld" %>
并使用它:
<html:form action="asd" focus="1">
<div><input type="text"></div>
<div><input type="submit"></div>
</html:form>
如果你想在它们之间切换,你还必须为 Struts 创建这样的库。
这样做时您需要记住的唯一一件事是 Spring 和 Struts 有一些不同的标签定义,因此 Struts 有“焦点”而 Spring 没有。我认为可能存在更多差异。
如果你真的想从一个切换到另一个,你必须让你的标签包含 Spring 和 Struts 的所有属性。但我真的不认为值得付出努力。