因为在前端要根据字典表中的数据去将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. lru缓存测试类

    package demo.mytest; import java.io.Serializable;import java.util.LinkedHashMap;import java.util.con ...

  2. ios xmpp demo

    为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中 在AppDelegate.m下这几个方法为: [java] view plaincopy -(void)setupStrea ...

  3. STATIC 和 CLASS

    STATIC 和 CLASS 由 王巍 (@ONEVCAT) 发布于 2015/01/28 Swift 中表示 “类型范围作用域” 这一概念有两个不同的关键字,它们分别是 static 和 class ...

  4. noip_最后一遍_2-图论部分

    大体按照 数学 图论 dp 数据结构 这样的顺序 模板集 这个真的只有模板了……………… ·spfa #include<bits/stdc++.h> using namespace std ...

  5. 封装微信分享到朋友/朋友圈js

    在页面引入: <script src="/static/lib/jquery-2.2.2.min.js"></script><script src=& ...

  6. 转:Ubuntu下ibus-sunpinyin的安装及翻页快捷键设置!

    在windows下,好多人都已经习惯了使用搜狗拼音,到ubuntu下,忽然没有极为顺手的输入法,实为郁闷,但是确实还没有for linux版本的搜狗使用,这是搜狗的商业策略,我们无法掌控,但是,如果你 ...

  7. C#学习基础概念二十五问

    C#学习基础概念二十五问 1.静态变量和非静态变量的区别?2.const 和 static readonly 区别?3.extern 是什么意思?4.abstract 是什么意思?5.internal ...

  8. 关于requirejs和grunt压缩合并是否矛盾

    requirejs主要是为了模块化开发,这样带来的好处不言而喻.但是分成多个js文件增加了请求数,那么就要用到合并压缩.合并压缩了原来的许多独立的js模块,那requirejs又是怎么冲压缩的文件中找 ...

  9. luogu2526 [SHOI2001]小狗散步

    注意一个景点只能去一次. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  10. Python常用操作符

    Python常用操作符 1.成员关系操作符in 显示的数字前面填充'0'代替空格 6.转义字符 符号 含义 \' 单引号\" 双引号\a 发出系统响铃声\b 退格符\n 换行符\t 横向制表 ...