taglibにfreemarkerからListを渡す

ほんとは配列を渡したかったんですけどうまくいかなかったのでとりあえず。


とにもかくにもとりあえずtaglibを作ります。
適当にこんな。

public class TestTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	
	private List<String> testList;

	public void setTestList(List<String> testList) {
		this.testList= testList;
	}

	public int doStartTag() throws JspException {
	
		System.out.println(testList.toString());

		return SKIP_BODY;
	}
}


次にftlから呼び出すためにtldファイルを作ります。

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd">

<taglib>
	<tlibversion>1.0</tlibversion>
	<jspversion>1.1</jspversion>
	<shortname>Test Tag Libraryy</shortname>

	<uri>http://tm8r.com/test.tld</uri>

	<tag>
		<name>testTag</name>
		<tagclass>com.tm8r.TestTag</tagclass>
		<bodycontent>empty</bodycontent>
		<attribute>
			<name>testList</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

jspのバージョンは1.1でも1.2でもよいです。
1.2の場合は若干xmlの要素の名前が違うので気をつけましょう。

最後にftlを作ります。

<#assign test=JspTaglibs["http://tm8r.com/test.tld"]>

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset=utf-8>
<title>test</title>
</head>
<body>
test.
<#assign testArr=["piyo","fuga"]>
<@test.testTag id="hoge" testList=testArr/>
</body>
</html>


できた!


ちなみにこう書くとargument type missmatch的なことを言われます。

<#assign testArr={"piyo","fuga"}>


変数定義しないで直接ぶち込む場合はこんな。

<@test.testTag id="hoge" testList=["piyo","fuga"]/>


気になったのでやってみたけどあんまり使わなさそう…!

スポンサーリンク