SpringBoot 实现Freemarker自定义标签
2017-10-12 10:16
471 查看
SpringBoot 实现Freemarker自定义标签
1.创建GlobalTag类实现TemplateDirectiveModel接口,重写里面的execute方法
@Component public class GlobalTag implements TemplateDirectiveModel{ @Autowired private GuavaDataCache guavaDataCache; @Override public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException { if(map.containsKey("key")&&map.get("key")!=null){//判断是否含有此变量 String key= map.get("key").toString();//获取变量值 String value= (String) guavaDataCache.query(key);//根据key获取缓存中的数据 DefaultObjectWrapperBuilder builder=new DefaultObjectWrapperBuilder//获取编译器(Configuration.VERSION_2_3_25); environment.setVariable(key,builder.build().wrap(value));//在环境中设置变量 //templateDirectiveBody.render(environment.getOut());//当含有标签体式才需要 } } }
2.配置标签与自定义的标签类进行关联
@Component public class FreemarkerConfig { @Autowired private Configuration configuration; @Autowired private GlobalTag globalTag; @PostConstruct public void setSharedVariable(){ configuration.setSharedVariable("global",globalTag);//标签名与标签类 } }
3.页面使用
<@global key=”a”>相关文章推荐
- springboot+thymeleaf 实现thymeleaf自定义方言系统 自定义标签, cms系统搭建(一)
- springMVC+freemarker实现自定义标签
- springboot+thymeleaf 实现thymeleaf自定义方言系统 自定义标签, cms系统搭建(二)
- (十三)SpringBoot之Spring-Data-Jpa(二)CRUD实现以及添加自定义方法
- Java开发框架spring实现自定义缓存标签
- spring boot 自定义starter的实现教程
- SpringBoot中自定义注解实现控制器访问次数限制
- Springboot + Freemarker项目中使用自定义注解
- Freemarker 自定义标签 实现TemplateDirectiveModel
- (十三)SpringBoot之Spring-Data-Jpa(二)CRUD实现以及添加自定义方法
- freemarker中使用<@spring.*>标签实现国际化
- Spring boot中结合Specification自定义Repository实现
- Spring boot data JPA 自定义JPQL语句,以及PagingAndSortingRepository接口实现分页查询
- idea+spring boot+spring Security+freemarker配置标签
- spring自定义缓存标签的实现
- springboot 自定义注解+AOP 实现日志记录
- 二十三、Spring Boot自定义Redis实现缓存机制
- SpringBoot添加自定义拦截器的实现代码
- Springboot+Mybatis+Jquery实现自定义二级联动
- 使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验