因为在前端要根据字典表中的数据去将1、2这些值转换成对应的文字解释
1.首先要创建一个类去实现 TemplateDirectiveModel 类
@Component
public class DictDirective implements TemplateDirectiveModel { @Override
public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if(map.containsKey("type") && map.get("type") != null){
String type = map.get("type").toString();
List<Dict> dictList = DictUtils.getDictList(type);
if(map.containsKey("value") && map.get("value") != null){
String value = map.get("value").toString();
Dict dict = null;
for (Dict dict1 : dictList) {
if(value.equals(dict1.getValue().toString())){
dict = dict1;
}
}
environment.setVariable("dict", builder.build().wrap(dict));
}else{
environment.setVariable("dictList", builder.build().wrap(dictList));
}
}
if(templateDirectiveBody!=null){
templateDirectiveBody.render(environment.getOut());
}
}
}
2.创建一个配置类
@Component
public class FreemarkerConfig {
@Autowired
private Configuration configuration;
@Autowired
private DictDirective dictDirective; @PostConstruct
public void setSharedVariable() throws TemplateModelException {
configuration.setSharedVariable("dict_tag", dictDirective);
}
}
然后就可以在页面上去调用了
<@dict_tag type="news_source" value="${news.source}">
${dict.label}
</@dict_tag>

前端可以任意传递参数,像type、value这样的,所有传递的参数都会被存到map里面,后台直接去取就可以了

SpringBoot整合freemarker中自定义标签获取字典表的数据的更多相关文章

  1. SpringBoot 整合mongoDB并自定义连接池

    SpringBoot 整合mongoDB并自定义连接池 得力于SpringBoot的特性,整合mongoDB是很容易的,我们整合mongoDB的目的就是想用它给我们提供的mongoTemplate,它 ...

  2. SpringBoot整合freemarker 引用基础

    原 ElasticSearch学习笔记Ⅲ - SpringBoot整合ES 新建一个SpringBoot项目.添加es的maven坐标如下: <dependency> <groupI ...

  3. springboot整合freemarker

    前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配 ...

  4. springboot 整合 freemarker

    springboot 整合 freemarker 依赖 <parent> <groupId>org.springframework.boot</groupId> & ...

  5. 【SpringBoot】09.SpringBoot整合Freemarker

    SpringBoot整合Freemarker 1.修改pom文件,添加坐标freemarker启动器坐标 <project xmlns="http://maven.apache.org ...

  6. springboot整合freemarker模板引擎后在页面获取basePath绝对路径

    在项目中引用静态资源文件或者进行ajax请求时我们有时候会使用 ${basePath} ,其实这就是一种获取绝对路径的方式: 那么在springboot项目中要怎么配置才能使用 basePaht呢? ...

  7. springMVC+freemarker实现自定义标签

    在开发过程中,有些需要引用到重复的页面,或者动态的数据 修改数据库是可以实现,但是太麻烦了. freemarker自定义标签在开发中用途很广,就说个入门实例吧 基于springmvc. 首先需要导入对 ...

  8. jsp页面中自定义标签的小演示

    在实习期遇到公司的pg自定义标签了,同事要我自己自学一下 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet.JSP标签扩展可以让你创建新的标签并且可以直接 ...

  9. 基于SpringBoot 、AOP与自定义注解转义字典值

    一直以来,前端展示字典一般以中文展示为主,若在表中存字典值中文,当字典表更改字典值对应的中文,会造成数据不一致,为此设置冗余字段并非最优方案,若由前端自己写死转义,不够灵活,若在业务代码转义,臃肿也不 ...

随机推荐

  1. layui和jquery冲突:Syntax error, unrecognized expression: +

    问题 layui创建table数据表格,但点击第二页时控制台报错,错误信息如下: 解决方法 https://fly.layui.com/jie/24224/ http://www.layui.com/ ...

  2. Bluefruit LE Sniffer - Bluetooth Low Energy (BLE 4.0) - nRF51822 驱动安装及使用

    BLE Sniffer https://www.adafruit.com/product/2269 Bluefruit LE Sniffer - Bluetooth Low Energy (BLE 4 ...

  3. mysql8忘记root密码修改密码(mac)

    0.在/etc/my.cnf修改验证方式 [mysqld] default_authentication_plugin=mysql_native_password 1.切换root权限: sudo s ...

  4. 【git】自动换行转换autocrlf

    #####windows git config --global core.autocrlf true #####linux git config --global core.autocrlf inp ...

  5. Linux 磁盘相关

    挂载文件系统 mount mount [-t fstype] filesystem dir ##mount /dev/sdb /data 卸载文件系统 umount umount /dev/sdb u ...

  6. PyQt5(2)、垃圾分类小程序(2)——初代窗口程序可执行文件

    又是一天时间(又没做大作业).今天的心路历程:(1)前端后端怎么连接?(2)后端数据库插数据(3)完全没用上之前的字典反查法(4)突然发现面向对象编程其实很好用,甚至越用越上瘾(5)QLineEdit ...

  7. One-to-one

    创建模型 在本例中,Place 和 Restaurant 为一对一关系 from django.db import models class Place(models.Model): name = m ...

  8. Python9-day3-作业

    ascli  字母,数字.特殊字符,1个字节.8位 unicode:16位 两个字节,升级32位,四个字节 utf-8:最少一个字节 8位,英文字母, 1,有变量name = "aleX l ...

  9. 3,bool值之间的转换,和str的各个功能属性。

    bool值之间的转换 and 空字符串即为False   字符串内有内容即为True. a = 11 c = str(a) #int转换成str print(type(c)) a = ' b = in ...

  10. python基础-面向对象(类)

    类 类的定义 >>> class P: ...     pass ... >>> P <class __main__.P at 0x0000000001F4B ...