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 ...
随机推荐
- CSS 边距和填充
margin and padding are the two most commonly used properties for spacing-out elements. A margin is t ...
- Python面试题之集合推导式、字典推导式
集合推导式 集合推导式(set comprehensions)跟列表推导式也是类似的, 唯一的区别在于它们使用大括号{}表示. Code: sets = {x for x in range(10)} ...
- jQuery图片列表拖拽排序布局
在线演示 本地下载
- 20162326 齐力锋 2017-2018学期 Bag类的补写博客
要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...
- JavaWeb 自定义标签库开发传统标签
自定义标签主要用于移除Jsp页面中的java代码. 移除jsp页面中的java代码,只需要完成两个步骤: 编写一个实现Tag接口的Java类,并覆盖doStartTag方法,把jsp页面中的java代 ...
- ES6中Json、String、Map、Object之间的转换
/** *字符串转json * */ static stringToJson(data){ return JSON.parse(data); } /** *json转字符串 */ static jso ...
- Spark Configuration配置
Spark可以通过三种方式配置系统: 通过SparkConf对象, 或者Java系统属性配置Spark的应用参数 通过每个节点上的conf/spark-env.sh脚本为每台机器配置环境变量 通过lo ...
- HDU1402 A * B Problem Plus
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- es6 中的let,const
在es6中,let的作用和var差不多,都是用来声明变量的,但是他们之间的区别在于作用域不同,大家都知道在js中没有块级作用域,例如: for(var i=0;i<10;i++){ consol ...
- mysql数据库优化课程---7、网站的搜索技术怎么选
mysql数据库优化课程---7.网站的搜索技术怎么选 一.总结 一句话总结: 1.量很小(像小网站)---like2.量大一点()---标签3.量超级大(像百度)---搜索引擎 1.数据库中取一列比 ...