首先,您需要创建一个类来完成自定义标签的工作。它应该继承自javax.servlet.jsp.tagext.SimpleTagSupport
. 如果所需的输出是隐藏的 html 输入标记,您可能需要id
和的属性name
。自定义标签的用户应该提供这些,因此它们成为带有 getter 和 setter 的自定义标签类的字段。用户还将指定从 spring bean 中获取的属性,因此这也将是类中的一个字段。
package com.example;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class MyCustomTag extends SimpleTagSupport {
private String name;
private String id;
private String property;
@Override
public void doTag() throws JspException, IOException {
// Overriding doTag() from SimpleTagSupport, you do your Java work.
try {
PageContext pc = (PageContext) getJspContext();
ServletContext sc = pc.getServletContext();
ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
ReloadableResourceBundleMessageSource mySpringBean = (ReloadableResourceBundleMessageSource)appCtx.getBean("messageSource");
// use this.property to allow your tag's user to get any message from the bean.
String gridColumnValues = mySpringBean.getMessage(this.property, null, Locale.US);
// write your desired output, in this case a complete hidden html form field
JspWriter out = pc.getOut();
out.print("<input type=\"hidden\" ");
if (this.name != null)
out.print("name=\"" + this.name + "\" ");
if (this.id != null)
out.print("id=\"" + this.id + "\" ");
out.println("value=\"" + gridColumnValues + "\"/>");
} catch (Exception e) {
throw new JspException(e);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
接下来,创建一个 tld 文件/WEB-INF/tld/mycustomtag.tld
。此文件定义自定义标签并按名称标识上述类。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>MyCustomTag</short-name>
<uri>http://example.com/mytags</uri>
<tag>
<name>custom</name> <!-- The name is how your users will invoke the tag in a jsp. You can call it anything you want. -->
<tag-class>com.example.MyCustomTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
要在 jsp 中使用它,请使用 taglib 指令声明您的自定义标记库。前缀将是您希望标签使用的 xml 命名空间。在这个例子中,我称它为“我的”。tld 文件中的标签名称是“custom”,因此可以使用<my:custom/>
<%@ taglib prefix="my" uri="http://example.com/mytags" %>
<my:custom id="myhiddenfield" name="hiddenname" property="propertyfile"/>
当您运行它并查看 html 源代码时,您将看到一个隐藏的输入标记,其中包含 Spring bean 消息的值。