- springmvc freemarker 动态加载(不用重启tomcat立即生效)
- FreeMarker 判断List不为空
- Cannot expose request attribute 'website' because of an existing model object of the same name 的解决方案
- Java模板引擎 FreeMarker介绍
- FreeMarker入门教程
- FreeMarker基本标签的使用
- FreeMarker基本数据类型
- FreeMarker集合(List、Map)
- FreeMarker自定义指令
- FreeMarker四种变量的用法
- FreeMarker模板包含及名称空间
- freemarker 空值处理
知识点
相关文章
更多最近更新
更多FreeMarker自定义指令
2019-03-05 21:42|来源: 领悟书生
FreeMarker自定义指令就像struts2的set标签,为了好理解,也可以认为是定义一个方法,就像js的function.在freemarker中的这个标签就是macro。在定义一个指令的时候,可以指定参数,当然也可以不指定,这里要注意了,我这里只贴出模板文件及输出结果,freemarker的工具类请参照FreeMarker入门教程,其实也没有什么改变,直接拿过来用即可
自定义一个不带参数的指令
<#macro hello> helloworld </#macro> <@hello/> |
helloworld |
自定义一个带参数的指令
如果指令带有参数,在用这个指令的时候,参数名是不能省略的,要不会出异常
<#--对于hello这个自定义指令而言,hello后的都是参数,有两个参数一个是num一个是ok--> <#macro hello numok> <#list 1..num as n> <h1>Hello${ok}${n}</h1> </#list> </#macro> <#--<@hello/>会报错,因为hello有两个参数,在定义参数的值的时候参数名不能省略--> <@hello num=3 ok="World"/> |
<h1>HelloWorld1</h1> <h1>HelloWorld2</h1> <h1>HelloWorld3</h1> |
自定义指令的同时给参数定义了初始值,此时在调用该指令就可以省略参数,如果省略会使用默认值
<#--为repeat的两个参数定义了初始值,此时在调用该指令就可以省略参数,如果省略会使用默认值--> <#macro repeat num=10 ok="World"> <#list 1..num as n> <h1>Hello${ok}${n}</h1> </#list> </#macro> <@repeat/> |
<h1>HelloWorld1</h1> <h1>HelloWorld2</h1> |
nested标签会输出指令中的内容
<#macro test> <#--nested会输出指令中的内容--> <#nested 12 33/> <#nested 11 22/> <#nested 22 33/> </#macro>
<@test;x,y> <h1>你好啊--${x}--${y}</h1> </@test> |
<h1>你好啊--12--33</h1> <h1>你好啊--11--22</h1> <h1>你好啊--22--33</h1> |
本文链接:FreeMarker自定义指令,本文由huangyineng原创,转载请注明出处
相关问答
更多-
springboot 使用freemarker作为展现后,怎么加入自定义指令[2024-02-29]
#include #include #include void code(int n) { char c; while((c=getchar())!='\n') { if(c>='a'&&c<='z') -
freemarker自定义指令 怎么获取参数[2023-04-15]
在项目应用中,遇到这样一个问题,当文本过长时,需要将前面的文本省略一部分,用…代替,而使用css只能在文本最后加… 我们可以通过freemarker自定义指令的方式实现上述功能。 freemarker自定义指令需要继承TemplateDirectiveModel接口, Java代码 收藏代码 package com.nexusy.freemarker.directive; import java.io.IOException; import java.util.Map; import freemarker. ... -
如何实现自定义freemarker标签[2023-07-06]
env:环境变量 params:指令参数(储存你所需要的值,随便是什么Key-Value你懂的) loopVars:循环变量 body:指令内容 除了Map外,其他的都能使null。 -
freemarker 中如何自定义标签[2023-11-23]
10) Java语言是多线程的。在Java语言中,线程是一种特殊的对象,它必须由Thread类或其子(孙)类来创建。通常有两种方法来创建线程:其一,使用型构为Thread(Runnable) 的构造子将一个实现了Runnable接口的对象包装成一个线程,其二,从Thread类派生出子类并重写run方法,使用该子类创建的对象即为线程。值得注意的是Thread类已经实现了Runnable接口,因此,任何一个线程均有它的run方法,而run方法中包含了线程所要运行的代码。线程的活动由一组方法来控制。 Java语言 ... -
freemarker 怎么引用jsp自定义标签[2021-06-15]
struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp sitemesh-freemarker org.apache.struts2.sitemesh.FreeMarkerPageFilter default_encoding UTF-8 struts2 org.apache.struts2.dispatcher.FilterDispatcher struts-cleanup /* sitemesh-freemarker /* st ... -
freemarker 怎样自定义标签[2022-07-19]
env:环境变量 params:指令参数(储存你所需要的值,随便是什么Key-Value你懂的) loopVars:循环变量 body:指令内容 除了Map外,其他的都能使null。 -
我的一个朋友发给我这个链接: http://cppoon.wordpress.com/2013/02/27/how-to-create-a-struts-2-component-with-freemarker/ 这就是我想要的。 要点是将自定义标记更改为不扩展TagSupport而将AbstractUITag更改为Struts标记而不是JSP标记。 这样可以自动链接(通过名称和路径约定)到我的Freemarker模板。 我基本上按照该页面上的说明进行操作。 我只添加了超类中抽象的方法,因此必须实现它们。 I ...
-
您可以将自定义配置全局注册到组件,然后将其应用于所有Freemarker端点。 或者使用configuration URI参数从本地到单个端点。 将配置注册到组件: FreemarkerComponent freemarkerComponent = getContext().getComponent("freemarker", FreemarkerComponent.class); freemarkerComponent.setConfiguration(myConfiguration); 将配置注册到单 ...
-
使用freemarker为自定义变量格式化xml模板(format xml template for the custom variables using freemarker)[2023-08-01]
我注意到你说的是“尝试使用map:{_prop.Var1:XXX},但这也不起作用”。 我不确定这意味着什么(比如,Java代码是什么),但是如果你尝试过类似variables.put("_prop.Var1", "XXX") ,那么这将不起作用,因为当你编写的模板_prop.Var1然后FreeMarker将首先查找_prop ,如果找到了,那么在它里面寻找Var1 。 它不会查找“_prop.Var1”键(因为您必须在模板中编写_prop\.Var1 ,以便将该点视为名称的一部分,而不是作为运算符)。 ... -
如果它将这些变量放入数据模型(模板上下文),则由Web应用程序框架决定; FreeMarker本身并没有做任何事情。 虽然,如果Web应用程序框架使用的是freemarker.ext.servlet.FreemarkerServlet ,它应该类似于Session.someAttributeName 。 That's up to the Web Application Framework if it puts such variables into the data-model (template cont ...