まず、カスタム タグの作業を行うクラスを作成する必要があります。から継承する必要がありjavax.servlet.jsp.tagext.SimpleTagSupport
ます。id
目的の出力が非表示の html 入力タグである場合、おそらくおよびの属性が必要になるでしょうname
。カスタムタグのユーザーはこれらを提供する必要があるため、ゲッターとセッターを持つカスタムタグクラスのフィールドになります。ユーザーは、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 名前空間になります。この例では、それを「my」と呼びました。tld ファイルのタグの名前は「custom」であるため、次のように呼び出されます。<my:custom/>
<%@ taglib prefix="my" uri="http://example.com/mytags" %>
<my:custom id="myhiddenfield" name="hiddenname" property="propertyfile"/>
これを実行して HTML ソースを表示すると、Spring Bean のメッセージの値を持つ非表示の入力タグが表示されます。