SpringBoot整合freemarker中自定义标签获取字典表的数据

因为在前端要根据字典表中的数据去将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里面,后台直接去取就可以了

原文地址:https://www.cnblogs.com/liangshandada/p/8761463.html

时间: 2024-08-25 07:27:38

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

springboot整合freemarker

前后端分离现在越来越多,如何有效的使用springboot来整合我们的页面是一个很重要的问题. springboot整合freemarker有以下几个步骤,也总结下我所犯的错误: 1.加依赖: 2.配置文件修改: 3.在templates下面编写后缀为ftl的页面: 4.错误出现:404问题: (1)检查是@RestController还是@Controller,如果要返回页面必须用@Controller (2)这次问题出现的很粗心:(漏掉一个小点) 原文地址:https://www.cnblo

SpringSecurity解决跨域问题,在SpringBoot整合SprinSecurity中如何用前后端分离Ajax登录,Ajax登录返回状态200还是近error

先说说SpringSecurity如何实现前后端分离Ajax登录? 今天使用SpringBoot整合SpringSecurity中想使用Ajax替代SpringSecurit的Form表单提交,在这里我们的提交方式还是使用表单提交 http.formLogin().loginProcessingUrl("/authentication/form") loginProcessingUrl方法表示你登录请求的地址,在这里SpringSecurity默认登录页面地址是/login ,填写了u

jsp自定义标签获取标签体内容输出到页面展示《三》

1.BodyTag.java package com.zy.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tage

django中自定义标签和过滤器

想要实现自定义标签和过滤器需要进行准备工作: 准备(必需)工作: 1  在某个app下创建一个名为templatetags(必需,且包名不可变)的包.假设我们在名为polls的app下创建了一个templatetags的包,并在该包下创建了一个名为mytags的文件.那么目录结构看起来应该就像这样: polls/ __init__.py models.py templatetags/ __init__.py mytags.py views.py 2  settings文件中的INSTALLD_A

FreeMarker中if标签内的判断条件

reeMarker中的<#if>标签除了里面直接判断 boolean 类型的变量外,也可以进行表达式判断,有几个细节记录一下 1. 判断对象是否存在(null) 经常会用到,如果对象 != null 则xxxx,在freemarker中表达比较奇怪,例如判断 target 是否为null,如果不为 nll 则做xxx动作 <#if target??> xxxx </#if> (目标变量后面连续两个??) 2. 字符串或数字比较 java里标准字符串比较需要 .equal

springboot整合freemarker自动加载宏

springboot引入freemarker最大的问题,在于springboot的application.properties配置文件,不能覆盖所有的freemarker配置 如果freemarker有自定义宏应该怎样引入呢? 首先 application.properties增加配置(与freemarker.properties一样) auto_import = _auto_include = /layout/pageShow.html 然后增加FreemarkerConfig类,启动即可 /

SpringBoot整合Freemarker+Mybatis

开发工具 , 开始 新建工程 .选择Spring Initializr 下一步 下一步,选择需要的组件 ..改一下工程名,Finish ..目录结构 首先,修改pom文件 然后,将application.properties改成yml文件,并且配置相关参数 我的数据库很简单,user表,int类型的自增id,varchar类型的name. 之后建立各个文件,目录结构如下: HelloController package com.example.controller; import com.exa

SpringBoot学习8:springboot整合freemarker

1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dep

在oracle中怎么把一张表的数据插入到另一张表中

把table2表的数据插入到table1中 insert   into   table1   select   *   from   table2