Springboot freemark自定义标签
spring-boot里使用freemarker自定义标签
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自定义标签相关推荐
- springboot+thymeleaf自定义select标签
效果 <sys:dict type="DISEASES_OPTION" name="option_code" class="" /&g ...
- spring thymeleaf 自定义标签
概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...
- 【Spring源码】4. 自己搞个标签?~自定义标签保姆级全过程(图解向,堆图预警)
[Spring源码系列- IOC] 1 [Spring源码]0.安装Gradle环境 2 [Spring源码]1.下载与编译_pom relocation to an other version nu ...
- thymeleaf自定义标签
前言 使用thymeleaf自定义标签,环境:springboot 2.3.7 + thymeleaf 3.0.11(2021-01-14最新版) 由于使用shiro,我们需要与thymeleaf整合 ...
- javaweb学习总结(二十三)——jsp自定义标签开发入门
一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...
- [PHP] JQuery+Layer实现添加删除自定义标签代码
JQuery+Layer实现添加删除自定义标签代码 实现效果如下: 实现代码如下: <!doctype html> <html> <head> <meta c ...
- JSP自定义标签rtexprvalue属性
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.(比如EL表达式或OGNL表达式). 当在<attribute>标 ...
- velocity自定义标签和指令
velocity本身支持自定义标签和指令的扩展, 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用,而以井号 # 开头的语句则为 Velocity 的指令(Direct ...
- java中velocity定义宏标签_velocity自定义标签和指令(转:zwj)
velocity本身支持自定义标签和指令的扩展,我们看看扩展指令的步骤及searchweb2的应用场景, 1.使用方法 在 Velocity 模板语言的语法中,以美元符 $ 开头的为变量的声明或者引用 ...
最新文章
- Immutable Collections(3)Immutable List实现原理(中)变化中的不变
- Linux部署项目的步骤
- Slim下的函数介绍(一)(转)
- PCB Layout的设计要点
- XX数据中心技术方案
- PMF镜像中文件提取/diskgenius做的镜像文件
- 云服务器5m带宽会有什么样的速度?
- 从特斯拉AI团队学到的九条方法论
- uni-app中使用腾旭位置服务,实现周边搜索功能,并获取到前往对应位置所需的时间(步行或驾车)
- 关于Office365邮箱附件大小限制问题
- Idea多次启动同一个服务
- 优鲜遇冷,叮咚还能如期上市吗?
- d3-绘制直线-横线
- 机器视觉光源选型总结---颜色选择
- L3-001 凑零钱 (30 分)
- 【detectron2】detectron2在ubuntu16.04系统下安装报错问题
- 论文写作之常见错误总结
- 破冰船是怎么破冰的?和你想到一点不一样,6米高的冰墙直接就撞
- Android登录注册功能封装
- excel表格转pdf格式的方法介绍