spring-boot里使用freemarker自定义标签

时间 2017-06-08 08:00:00   Tomoya
原文   https://tomoya92.github.io/2017/06/08/spring-boot-freemarker-tag/
主题  freemarker  Spring Boot

spring-boot开发网站使用freemarker里的自定义标签方法

创建类实现 TemplateDirectiveModel 类

@Component
public class UserTopicDirective implements TemplateDirectiveModel {@Autowiredprivate UserService userService;@Autowiredprivate TopicService topicService;@Overridepublic 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());}
}

创建配置类

@Component
public class FreemarkerConfig {@Autowiredprivate Configuration configuration;@Autowiredprivate UserTopicDirective userTopicDirective;@PostConstructpublic 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.yml
site:
  baseUrl: http://localhost:8080/

对应的类是 SiteConfig.java 要取里面的值,使用方法如下

@Autowired
private SiteConfig siteConfig;//...
siteConfig.getBaseUrl();

如果把siteConfig加入到freemarker的configuration里就可以直接在freemarker页面上使用变量了

@PostConstruct
public void setSharedVariable() throws TemplateModelException {configuration.setSharedVariable("site", siteConfig);configuration.setSharedVariable("user_topics_tag", userTopicDirective);
}

页面里就可以这样来取值

<a href="${site.bashUrl}">首页</a>

是不是很方便

参考

Springboot freemark自定义标签相关推荐

  1. springboot+thymeleaf自定义select标签

    效果 <sys:dict type="DISEASES_OPTION" name="option_code" class="" /&g ...

  2. spring thymeleaf 自定义标签

    概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...

  3. 【Spring源码】4. 自己搞个标签?~自定义标签保姆级全过程(图解向,堆图预警)

    [Spring源码系列- IOC] 1 [Spring源码]0.安装Gradle环境 2 [Spring源码]1.下载与编译_pom relocation to an other version nu ...

  4. thymeleaf自定义标签

    前言 使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版) 由于使用shiro,我们需要与thymeleaf整合 ...

  5. javaweb学习总结(二十三)——jsp自定义标签开发入门

    一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...

  6. [PHP] JQuery+Layer实现添加删除自定义标签代码

    JQuery+Layer实现添加删除自定义标签代码 实现效果如下: 实现代码如下: <!doctype html> <html> <head> <meta c ...

  7. JSP自定义标签rtexprvalue属性

    rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...

  8. velocity自定义标签和指令

    velocity本身支持自定义标签和指令的扩展, 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用,而以井号 # 开头的语句则为 Velocity 的指令(Direct ...

  9. java中velocity定义宏标签_velocity自定义标签和指令(转:zwj)

    velocity本身支持自定义标签和指令的扩展,我们看看扩展指令的步骤及searchweb2的应用场景, 1.使用方法 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用 ...

最新文章

  1. Immutable Collections(3)Immutable List实现原理(中)变化中的不变
  2. Linux部署项目的步骤
  3. Slim下的函数介绍(一)(转)
  4. PCB Layout的设计要点
  5. XX数据中心技术方案
  6. PMF镜像中文件提取/diskgenius做的镜像文件
  7. 云服务器5m带宽会有什么样的速度?
  8. 从特斯拉AI团队学到的九条方法论
  9. uni-app中使用腾旭位置服务,实现周边搜索功能,并获取到前往对应位置所需的时间(步行或驾车)
  10. 关于Office365邮箱附件大小限制问题
  11. Idea多次启动同一个服务
  12. 优鲜遇冷,叮咚还能如期上市吗?
  13. d3-绘制直线-横线
  14. 机器视觉光源选型总结---颜色选择
  15. L3-001 凑零钱 (30 分)
  16. 【detectron2】detectron2在ubuntu16.04系统下安装报错问题
  17. 论文写作之常见错误总结
  18. 破冰船是怎么破冰的?和你想到一点不一样,6米高的冰墙直接就撞
  19. Android登录注册功能封装
  20. excel表格转pdf格式的方法介绍

热门文章

  1. 用计算机听音乐教案,听音乐教案
  2. 使用 TensorFlow 在卷积神经网络上实现 L2 约束的 softmax 损失函数
  3. 汽车和工业发动机管理MCU 芯片选型
  4. Nginx负载均衡(架构之路)
  5. 一个真实的项目经历,很多东西大家可以借鉴下
  6. App的手势密码你做对了吗?
  7. 计算机考试社会考和学校考的区别,【学生篇】国家软考与计算机等级考试有什么区别...
  8. 百变小精灵,CRMEB Java 单商户系统也要当!
  9. python中利用re模块正则表达式匹配ip地址
  10. 哔哩哔哩:笔试题(20190910)