4.6 内置标签函数
About 536 wordsAbout 2 min
2021-03-27
4.6 内置标签函数
include include一个模板,如 :
<%include("/header.html"){}%>
如果想往子模板中传入参数,则可以后面跟一个json变量
<%include("/header.html",{'user':user,'id',user.id}){}%>
这样user,和id 可以在header.html被引用,并成为header.html的全局变量
(beetl1.2 也叫includeFileTemplate ,2.0仍然支持,但不再文档里体现了)
layout 提供一个布局功能,每个页面总是由一定布局,如页面头,菜单,页面脚,以及正文。 layout标签允许为正文指定一个布局,如下使用方式
content.html内容如下:
<% //content.html内容如下: layout("/inc/layout.html"){%> this is 正文 .......... <%}%>
layout.html 是布局文件,内容如下
<%include("/inc/header.html"){} %> this is content:${layoutContent} this is footer:
运行content.html模板文件后,,正文文件的内容将被替换到layoutContent的地方,变成如下内容
this is header this is content:this is 正文 ............ this is footer:
如果想往layout页面传入参数,则传入一个json变量,如下往layout.html页面传入一个用户登录时间
<%layout("/inc/header.html",{'date':user.loginDate,'title':"内容页面"}){%> this is 正文 .......... <%}%>
如果layoutContent 命名有冲突,可以在layout第三个参数指定,如
<%layout("/inc/header.html",{'date':user.loginDate,'title':"内容页面"},"myLayoutContent"){%> this is 正文 .......... <%}%>
cache 能Cache标签的内容,并指定多长时间刷新,如
<%:cache('key2',10,false){ %> 内容体 <%}%>
需要指定三个参数
- 第一个是cache的Key值
- 第二个是缓存存在的时间,秒为单位
- 第三个表示是否强制刷新,false表示不,true表示强制刷新
Cache默认实现org.beetl.ext.tag.cache.SimpleCacheManager. 你可以设置你自己的Cache实现,通过调用CacheTag. cacheManager= new YourCacheImplementation();
可以在程序里调用如下方法手工删除Cache:
public void clearAll(); public void clearAll(String key); public void clearAll(String... keys);
includeJSP,可以在模板里包括一个jsp文件,如:
<% includeJSP("/xxxx.jsp",{"key":"value"}){} %>
key value 都是字符串,将以parameter的形式提供给jsp,因此jsp可以通过request.getParameter("key")来获取参数
主要注意的是,这个标签并非内置,需要手工注册一下
groupTemplate.registerTag("incdlueJSP",org.beetl.ext.jsp.IncludeJSPTag.class);