Freemarker 自定义标签 实现TemplateDirectiveModel
1 自定义标签需要实现TemplateDirectiveModel这个接口中的execute方法 实例代码如下
public class UserListDirective implements TemplateDirectiveModel{
@Autowired
private UserDAO userDao;
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars,
TemplateDirectiveBody body)
throws TemplateException, IOException {
String name = params.get("name").toString();
List<User> userlist = userDao.findByProperty("name", name);
env.setVariable("userList", getBeansWrapper().wrap(userlist));
body.render(env.getOut());
}
public static BeansWrapper getBeansWrapper(){
BeansWrapper beansWrapper =
new BeansWrapperBuilder(Configuration.VERSION_2_3_21).build();
return beansWrapper;
}
}
2 配置 UserListDirective 到spring bean xml中
<bean id="userListDirective" class="com.action.directive.UserListDirective"></bean>
3 将spring bean 设置到freemarkerConfig全局变量中去。
<bean id="freemarkerConfig2"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/" />
<property name="freemarkerVariables">
<map >
<entry key="userListDirective" value="userListTag" />
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
</props>
</property>
</bean>
4 ftl文件中的访问方式
<@userListTag name="zhangsan">
<#if userList?? && userList?size gt 0>
<#list userList as user>
<a href="">${user.name}</a>
</#list>
</#if>
</@userListTag>
Freemarker 自定义标签 实现TemplateDirectiveModel的更多相关文章
- spring整合freemarker 自定义标签
1.自定义标签实现 TemplateDirectiveModel 接口 2.spring 配置,注意标红的两行 <bean id="freemarkerConfig" cla ...
- freemarker自定义标签(与java合用)
自定义类继承FreemarkerManager类,重写protected Configuration createConfiguration(ServletContext servletContext ...
- OneBlog开源博客-详细介绍如何实现freemarker自定义标签
前言 OneBlog中使用到了springboot + freemarker的技术,同时项目里多个controller中都需要查询一个公有的数据集合,一般做法是直接在每个controller的方法中通 ...
- freemarker自定义标签报错(六)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...
- freemarker自定义标签报错(五)
freemarker自定义标签 1.错误描述 六月 05, 2014 11:40:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...
- freemarker自定义标签报错(四)
freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...
- freemarker自定义标签(一)
freemarker自定义标签 1.自定义标签说明 宏变量存储模板片段可以被用作自定义指令macro 2.示例说明 <html> <head> <meta http-eq ...
- freemarker自定义标签报错(三)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...
- freemarker自定义标签报错(二)
freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker ...
随机推荐
- myelcipse中SVN进行代码更新和提交
感谢博主(John的专栏)的奉献,http://blog.csdn.net/tangzenglei/article/details/50175639
- ReadyBoost
ReadyBoost是Windows Vista中的新技术,在继Windows Vista的下一代操作系统Windows 7中,同样包含了这项技术,它利用了闪存随机读写及零碎文档读写上的优势来提高计算 ...
- EasyUI:datagrid控件简介
EasyUI:datagrid控件简介 1,水平滚动条属性: //显示滚动条 fitColumns:false //不显示滚动条 fitColumns:true
- JavaScript右下角信息提示插件Notyf
在线演示 本地下载
- jedis客户端,取redis服务的值
package com.common.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- shell script语法高亮和自动缩进的配置
编辑/etc/profile文件,在文件末尾加一下内容: export TERM=xterm-color 接着让其变为全局变量 source /etc/profile 编辑/etc/vimrc文件,在 ...
- Word Search, 在矩阵中寻找字符串,回溯算法
问题描述: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...
- Flume-NG中的Channel与Transaction关系(原创)
在sink和source中(不管是内置还是自定义的),基本都有如下代码,这些代码在sink中的process方法中,而在source中自己不需要去写,在source中getChannelProcess ...
- vue-cli 一分钟搭建自己的vue项目
创建项目 1.安装全局vue-cli npm install vue-cli -g 2.生成项目模板(my_projuct为项目名称) vue init webpack my_projuct 3.进入 ...
- 【LABVIEW到C#】4》String的操作之Search and Replace.vi
C#封装如下: public class SearchAndRepalce : Darrenstring { public bool replaced; private string stringou ...