因为在前端要根据字典表中的数据去将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. struts2的多个文件上传

                成功效果图:                 上篇文章描述了单个文件的上传和配置,下面主要讲解下不同的地方: index.jsp <head> <script ...

  2. javaEE(6)_JSP

    一.什么是JSP 1.JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术,只用JSP就可以开发动态web资源. 2.为什么J ...

  3. vue+element UI如何导出excel表

    导出excel表应按如下规则 首先要先安装如下依赖 npm install --save xlsx npm install --save file-saver 接下在在你的代码中去引用这两个 impo ...

  4. tkinter学习-文本框

    阅读目录 Entry 输入框 Text 文本框 Entry: 说明:输入控件,用于显示简单的文本内容 属性:在输入框中用代码添加和删除内容,同样也是用insert()和delete()方法 from ...

  5. 【转发】【linux】【php】centos 编译php常见错误

    configure: error: xml2-config not found. Please check your libxml2 installation. yum install libxml2 ...

  6. 什么是php?php的优缺点有哪些?与其它编程语言的优缺点?

    身为一个PHP开发者,有必要了解一下PHP的缺点,知道每种语言的优点和缺点,才能知道某种语言在什么场景下适合使用,在什么场景下不适合使用. 这个问题我曾经面试的时候遇到过,我之前没总结过,第一问大部分 ...

  7. jQuery和Vue

    jQuery 概述 是js的一种函数库有美国人 John Resig编写 特点 写的少,做的多,国内用的jq1.0版本,可以兼容低版本的浏览器,支持链式编程或链式调用和隐式迭代 链式编程 $(this ...

  8. set的应用:UVa10815-Andy's First Dictionary

    Andy's First Dictionary Andy, 8, has a dream - he wants to produce his very own dictionary. This is ...

  9. 2019年最新 Python 模拟登录知乎 支持验证码

    知乎的登录页面已经改版多次,加强了身份验证,网络上大部分模拟登录均已失效,所以我重写了一份完整的,并实现了提交验证码 (包括中文验证码),本文我对分析过程和代码进行步骤分解,完整的代码请见末尾 Git ...

  10. angularJs模块ui-router之多视图

    可以给ui-view指定名称,这样一个模板中就可以有多个ui-view.假设您有一个应用,需要动态填充graph.table data和filters,像下面这样: 当您需要使用多视图时,需要用到状态 ...