Freemarker自定义标签
一、 用macro實(shí)現(xiàn)自定義指令,例如:
自定義指令可以使用macro指令來(lái)定義。
???? <#macro greet person>
??????????? <font size="+2">Hello ${person}!</font>
???? </#macro>
???? macro指令自身不打印任何內(nèi)容,它只是用來(lái)創(chuàng)建宏變量,所以就會(huì)有一個(gè)名為greet的變量。
使用這個(gè)宏:
??? <@greet person="Fred"/>
會(huì)打印出:
??? <font size="+2">Hello Fred!</font>
二、用java代碼標(biāo)簽實(shí)現(xiàn)自定義指令:
可以使用TemplateDirectiveModel接口在Java代碼中實(shí)現(xiàn)自定義指令。
簡(jiǎn)單示例如下:
1、實(shí)現(xiàn)TemplateDirectiveModel接口。
??????????? public class UpperDirective implements TemplateDirectiveModel {
????????????????? public void execute(Environment env,
?????????????????????????? Map params, TemplateModel[] loopVars,
?????????????????????????? TemplateDirectiveBody body)
?????????????????????????? throws TemplateException, IOException {
??????????????????????????? if (!params.isEmpty()) {
???????????????????????????? throw new TemplateModelException(
?????????????????????????????? "parameters 此處沒(méi)有值!");
??????????????????????????? }
?????????????????????????? if (loopVars.length != 0) {
?????????????????????????? throw new TemplateModelException(
????????????????????????? " variables 此處沒(méi)有值!");
????????????????????????? }
???????????????????????? if (body != null) {
???????????????????????? //執(zhí)行nested body? 與FTL中 <#nested> 類似。
???????????????????? body.render(new UpperCaseFilterWriter(env.getOut()));
???????????????????????? } else {
??????????????????????? throw new RuntimeException("missing body");
???????????????????????? }
??????????????????? }
?????????? private static class UpperCaseFilterWriter extends Writer {
?????????? private final Writer out;
?????????? UpperCaseFilterWriter (Writer out) {
??????????? this.out = out;
??????? }?????? ?
??????? public void write(char[] cbuf, int off, int len)
??????????? throws IOException {
??????????? char[] transformedCbuf = new char[len];
??????????????? for (int i = 0; i < len; i++) {
?????????????????????????????? transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]);
??????????????? }
??????????????????? out.write(transformedCbuf);
??????????? }
??????? public void flush() throws IOException {
??????????????? out.flush();
??????????? }
??????????????? public void close() throws IOException {
??????????????? out.close();
??????????????? }
?????????? }
?????? }
???? 說(shuō)明:<#nested>指令執(zhí)行位于開(kāi)始和結(jié)束標(biāo)記指令之間的模板代碼段。
2、注入FreeMarkerConfigurer的freemarkerVariables中。
???? 例如:在applicationContext.xml
??? <entry key="upper" value-ref="upper"/>
??? <bean id="upper" class="com.example.UpperDirective" />
?? 說(shuō)明:
?? FreeMarkerConfigurer. 、setFreemarkerVariables(Map<String,Object> variables)
?? 底層調(diào)用了FreeMarker的Configuration.setAllSharedVariables()方法。
?? 因?yàn)楦玫膶?shí)踐是將常用的指令作為共享變量放到Configuration中。
3、調(diào)用自定義指令:
???? [@upper]
??????????? bar
??????????? [#list ["red", "green", "blue"] as color]
????????????????? ${color}
??????????? [/#list]
??????????? baaz
???? [/@upper]
4、顯示輸出結(jié)果:
????? BAR RED GREEN BLUE BAAZ
轉(zhuǎn)載于:https://www.cnblogs.com/pengw/p/3944429.html
總結(jié)
以上是生活随笔為你收集整理的Freemarker自定义标签的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php变量什么情况下加大括号{}
- 下一篇: Prim算法和Kruskal算法