在 Spring Boot 使用 Freemarker 模板來快速建立客製化的標籤 (taglib) 是非常簡單的事. 作下列幾個步驟即可.
public class FtlTemplateTag extends TagSupport {
private static final long serialVersionUID = 1L;
private String fileName = null;
private String paramsStr = null;
private String columnsStr = null;
private ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setParamsStr(String paramsStr) {
this.paramsStr = paramsStr;
}
public void setColumnsStr(String columnsStr) {
this.columnsStr = columnsStr;
}
/**
* JSON 字串轉換成 MAP
* @return
*/
private Map setParams() {
Gson gson = new Gson();
return gson.fromJson(paramsStr, new TypeToken
<?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>Polin WEI Tag Library</description>
<tlib-version>1.0</tlib-version>
<short-name>pw</short-name>
<uri>/polinwei/tags</uri>
<tag>
<description>Get the file name of freemarker template</description>
<name>FtlSampleTemplate</name>
<tag-class>com.spring.jwt.tablibs.FtlTemplateTag</tag-class>
<body-content>empty</body-content>
<attribute>
<description> Freemarker Template 的檔案</description>
<name>fileName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>parmsStr</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>columnsStr</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
(繼續閱讀...)
沒有留言:
張貼留言