java自定义配置文件_自定义配置文件如何配置

@Component

public class RoleDirectiveModel implements TemplateDirectiveModel {

/**

*

* @param environment 环境变量

* @param params 指令参数(存储你所需要的值,随便是什么Key-Value你懂的)

* @param loopVars 循环变量 返回值

* @param templateDirectiveBody 指令内容

* 除了params外,其他的都能是Null。

* @throws TemplateException

* @throws IOException

*/

@Override

public void execute(Environment environment, Map params, TemplateModel[] loopVars, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

System.out.println("=========");

TemplateScalarModel user=(TemplateScalarModel)params.get("user");

TemplateScalarModel role=(TemplateScalarModel)params.get("role");

if("123456".equals(user.getAsString()) && "admin".equals(role.getAsString()) ){

loopVars[0]=TemplateBooleanModel.TRUE;

}

List otherRights=new ArrayList<>();

otherRights.add("add");

otherRights.add("update");

otherRights.add("delete");

loopVars[1]=new SimpleSequence(otherRights);

templateDirectiveBody.render(environment.getOut());

}

}

这是自定义指令的定义类

这是加载自定义指令的freemarker java配置类import com.example.demo.model.tag.RoleDirectiveModel;

import com.example.demo.model.tag.SortMethod;

import freemarker.template.TemplateModelException;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j

@Configuration

public class FreeMarkerAutoConfiguration {

@Autowired

private freemarker.template.Configuration configuration;

@Autowired

private RoleDirectiveModel roleDirectiveModel;

@Autowired

private SortMethod sortMethod;

@PostConstruct

public void setSharedVariable() {

try {

//自定义标签

configuration.setSharedVariable("role", roleDirectiveModel);

configuration.setSharedVariable("sort_int",sortMethod);

} catch (Exception e) {

log.error("Custom tags failed to load:{}", e.getMessage());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值