标签:over except freemark utils boot template exec tag source
@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()); } } }
@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>
SpringBoot整合freemarker中自定义标签获取字典表的数据
标签:over except freemark utils boot template exec tag source
原文地址:https://www.cnblogs.com/liangshandada/p/8761463.html