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的更多相关文章

  1. spring整合freemarker 自定义标签

    1.自定义标签实现 TemplateDirectiveModel 接口 2.spring 配置,注意标红的两行 <bean id="freemarkerConfig" cla ...

  2. freemarker自定义标签(与java合用)

    自定义类继承FreemarkerManager类,重写protected Configuration createConfiguration(ServletContext servletContext ...

  3. OneBlog开源博客-详细介绍如何实现freemarker自定义标签

    前言 OneBlog中使用到了springboot + freemarker的技术,同时项目里多个controller中都需要查询一个公有的数据集合,一般做法是直接在每个controller的方法中通 ...

  4. freemarker自定义标签报错(六)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...

  5. freemarker自定义标签报错(五)

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:40:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  6. freemarker自定义标签报错(四)

    freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严 ...

  7. freemarker自定义标签(一)

    freemarker自定义标签 1.自定义标签说明 宏变量存储模板片段可以被用作自定义指令macro 2.示例说明 <html> <head> <meta http-eq ...

  8. freemarker自定义标签报错(三)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column ...

  9. freemarker自定义标签报错(二)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Unexpected end of file reached. at freemarker ...

随机推荐

  1. 【leetcode刷题笔记】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. MySQL-5.7密码策略及用户资源限制

    1.密码策略 在mysql 5.6对密码的强度进行了加强,推出了validate_password 插件.支持密码的强度要求. (1)安装插件 [root@localhost ~]# ll /usr/ ...

  3. 强大的jQuery幻灯片播放插件 支持全拼、拖拽和下载等功能

    在线演示 本地下载

  4. jQuery多层级垂直手风琴菜单

    在线演示 本地下载

  5. COS-7设备管理

    操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行.   操作系 ...

  6. Hive-行转列(explode)

    场景:通过parseHtml UDF解析一串HTML,返回一以 @@ 分割的字符串,使用split分割字符串进数组中,然后将数组的元素转列. 开始的写法 SELECT id, legal_person ...

  7. php中那些我还没弄明白的名词解释

    1.正则表达式引擎,引擎是什么? 2.语言构造器和函数的区别 3.可变函数

  8. Scala学习手记1 - 快速体验

    又重新开始了scala的学习,因为中断了太长时间,所以这次还得从零开始.学习的过程就记录在这个博客上了. 这次学习的教程是<scala程序设计 java虚拟机多核编程实战>,我在多看上买了 ...

  9. hive 导出数据到本地

    有时候需要将hive库中的部分数据导入至本地,这样子做可视化和小规模的数据挖掘实验都是比较方便的.数据导入至本地的HQL语法如下: INSERT OVERWRITE [LOCAL] DIRECTORY ...

  10. 在数据库中添加数据以后,使用Mybatis进行查询结果为空

    在数据库中添加数据以后,使用Mybatis进行查询结果为空,这是因为数据库中添加数据忘记commit的缘故.