Springboot freemark自定义标签
来源:互联网 发布:好听的改编网络歌曲 编辑:程序博客网 时间:2024/06/01 23:21
spring-boot里使用freemarker自定义标签
spring-boot开发网站使用freemarker里的自定义标签方法
创建类实现 TemplateDirectiveModel 类
@Componentpublic class UserTopicDirective implements TemplateDirectiveModel { @Autowired private UserService userService; @Autowired private TopicService topicService; @Override public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { Page<Topic> page = new PageImpl<>(new ArrayList<>()); if (map.containsKey("username") && map.get("username") != null) { String username = map.get("username").toString(); if (map.containsKey("p")) { int p = map.get("p") == null ? 1 : Integer.parseInt(map.get("p").toString()); int limit = Integer.parseInt(map.get("limit").toString()); User currentUser = userService.findByUsername(username); if (currentUser != null) { page = topicService.findByUser(p, limit, currentUser); } } } DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); environment.setVariable("page", builder.build().wrap(page)); templateDirectiveBody.render(environment.getOut()); }}
创建配置类
@Componentpublic class FreemarkerConfig { @Autowired private Configuration configuration; @Autowired private UserTopicDirective userTopicDirective; @PostConstruct public void setSharedVariable() throws TemplateModelException { configuration.setSharedVariable("user_topics_tag", userTopicDirective); }}
用法
跟自定义macro用法一样,直接使用 <@xx></@xx>
来使用即可,值就直接在 user_topics_tag 标签里传就可以了
<@user_topics_tag username='tomoya' p=1 limit=10> <#list page.getContent() as topic> <p>${topic.title!}</p> </#list></@user_topics_tag>
扩展
FreemarkerConfig类不止可以加入自定义的标签,还可以加入系统自定义的变量等,下面举例说明
spring-boot里的配置文件
# application.ymlsite: baseUrl: http://localhost:8080/
对应的类是 SiteConfig.java
要取里面的值,使用方法如下
@Autowiredprivate SiteConfig siteConfig;//...siteConfig.getBaseUrl();
如果把siteConfig加入到freemarker的configuration里就可以直接在freemarker页面上使用变量了
@PostConstructpublic void setSharedVariable() throws TemplateModelException { configuration.setSharedVariable("site", siteConfig); configuration.setSharedVariable("user_topics_tag", userTopicDirective);}
页面里就可以这样来取值
<a href="${site.bashUrl}">首页</a>
是不是很方便
参考
阅读全文
0 0
- Springboot freemark自定义标签
- freemark自定义标签
- Freemark自定义标签,调用java静态方法
- freemark 标签
- freemark标签
- 如何自定义标签(.tld文件的使用) --Freemark
- SpringBoot 实现Freemarker自定义标签
- freemark标签常用示例
- freemark标签使用
- FreeMark 常用标签
- springboot+mybatis+freemark环境搭建
- 【SpringBoot教程四】:SpringBoot+FreeMark模板引擎
- jeecms学习二-------学习freeMark标签及自定义标签的用法
- freemark标签的使用案例
- freemark标签的使用案例
- SpringBoot学习(二)--集成mybatis+freemark
- freemark建立自定义标记宏 <@
- springboot 自定义权限标签(tld),在freemarker引用
- POJ
- Leetcode#38. Count and Say (模拟)
- [Leetcode]Dynamic Programming
- root分区扩容
- SAP FIORI FOR IOS框架应用简介
- Springboot freemark自定义标签
- mysql数据库分表查询(基于laravel5.2)
- 2017年9月14日训练日记
- 集群技术概述
- css3盒子模型内减模式和外加模式
- 553. Optimal Division 数学、字符串
- TeemoAttacking
- 古文觀止卷九_梓人傳_柳宗元
- 恒流源电路的几种实现方式(已通过Proteus仿真验证)