编写自定义的 Velocity 指令 #cache
来源:互联网 发布:电视推荐 知乎 编辑:程序博客网 时间:2024/05/20 08:42
[代码] CacheDirective.java
01
/**
02
* Velocity模板上用于控制缓存的指令
03
* @author Winter Lau
04
* @date 2009-3-16 下午04:40:19
05
*/
06
public
class
CacheDirective
extends
Directive {
07
08
final
static
Hashtable<String,String> body_tpls =
new
Hashtable<String, String>();
09
10
@Override
11
public
String getName() {
return
"cache"
; }
//指定指令的名称
12
13
@Override
14
public
int
getType() {
return
BLOCK; }
//指定指令类型为块指令
15
16
/* (non-Javadoc)
17
* @see org.apache.velocity.runtime.directive.Directive#render()
18
*/
19
@Override
20
public
boolean
render(InternalContextAdapter context, Writer writer, Node node)
21
throws
IOException, ResourceNotFoundException, ParseErrorException,
22
MethodInvocationException
23
{
24
//获得缓存信息
25
SimpleNode sn_region = (SimpleNode) node.jjtGetChild(
0
);
26
String region = (String)sn_region.value(context);
27
SimpleNode sn_key = (SimpleNode) node.jjtGetChild(
1
);
28
Serializable key = (Serializable)sn_key.value(context);
29
30
Node body = node.jjtGetChild(
2
);
31
//检查内容是否有变化
32
String tpl_key = key+
"@"
+region;
33
String body_tpl = body.literal();
34
String old_body_tpl = body_tpls.get(tpl_key);
35
String cache_html = CacheHelper.get(String.
class
, region, key);
36
if
(cache_html ==
null
|| !StringUtils.equals(body_tpl, old_body_tpl)){
37
StringWriter sw =
new
StringWriter();
38
body.render(context, sw);
39
cache_html = sw.toString();
40
CacheHelper.set(region, key, cache_html);
41
body_tpls.put(tpl_key, body_tpl);
42
}
43
writer.write(cache_html);
44
return
true
;
45
}
46
}
[代码] 使用方法
01
#cache("News","home")
02
## 读取数据库中最新新闻并显示
03
<
ul
>
04
#foreach($news in $NewsTool.ListTopNews(10))
05
<
li
>
06
<
span
class
=
'date'
>
07
$date.format("yyyy-MM-dd",${news.pub_time})
08
</
span
>
09
<
span
class
=
'title'
>${news.title}</
span
>
10
</
li
>
11
#end
12
</
ul
>
13
#end
- 编写自定义的 Velocity 指令 #cache
- 编写自定义的 Velocity 指令
- 编写自定义的 Velocity 指令
- 编写自定义的 Velocity 指令
- velocity中自定义指令的用法
- velocity自定义指令示例
- Velocity自定义指令(函数)
- velocity自定义标签和指令
- velocity自定义标签和指令
- velocity自定义标签和指令
- Velocity 指令的转义
- spring boot velocity自定义指令 java
- Velocity 循环指令一种好的解决方法
- distributed Cache (Velocity R3)
- 指令Cache
- AngularJs 第一个自定义指令编写
- Velocity #set指令
- Velocity #if/#else指令
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--类设计1[使用IE浏览本页]
- 网络视频编码器功能特点及技术参数
- NYOJ 279 队花的烦恼二和NYOJ 176 整数划分(二)【dp问题或递归】
- nginx1.0+resin4配置简单的反向代理
- VMware安装Ghost XP SP3 经验总结
- 编写自定义的 Velocity 指令 #cache
- 深入研究Servlet线程安全性问题
- Linux内核同步机制之completion
- http长连接与短连接
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--类设计2[使用IE浏览本页]
- 搭建Android应用开发环境(Windows)
- 最近碰到的问题已解决
- 关于request对象的应用
- 适配器模式