freemark(2)list,局部变量,计算,函数

@Test
	public void testAssign() {
//		<#assign epAmount=0><#list amount as amt>epAmount=epAmount+${amt}</#list>${epAmount}
		//取list第一个直接[0];如果list里面放的Map,可以直接点属性
		//函数前直接加问号即可,例如:?replace
		//设置本地变量<#assign epAmount=0> ; 注意: 边量的计算是在变量符号里,且不需要美元取值符号,<>里都不需要再用${}取值 <#assign epAmount=epAmount+amt>
		String template = "${date_startDate[0]?replace(\"-\",\"/\")}-${date_endDate[0]?replace(\"-\",\"/\")}期间,交易时间异常,<#assign epAmount=0><#list amount as amt><#assign epAmount=epAmount+amt></#list>合计${epAmount}元,<#assign epCount=0><#list count as cnt><#assign epCount=epCount+cnt></#list>${epCount}笔。";
		Map<String,Object> freeParaMap = new HashMap<String,Object>();
		//交易时间
		List<String> date_startDate=new ArrayList<String>();
		date_startDate.add("2018-08-30");
		freeParaMap.put("date_startDate", date_startDate);
		
		List<String> date_endDate=new ArrayList<String>();
		date_endDate.add("2018-09-30");
		freeParaMap.put("date_endDate", date_endDate);
		
		
		//每10分钟交易金额
		List<Integer> amount=new ArrayList<Integer>();
		amount.add(600);
		amount.add(5000);
		freeParaMap.put("amount", amount);
		//每10分钟交易笔数
		List<Integer> count=new ArrayList<Integer>();
		count.add(2);
		count.add(3);
		freeParaMap.put("count", count);
		
		System.out.println(JSON.toJSONString(freeParaMap));
		//2018/08/30-2018/09/30期间,交易时间异常,合计5,600元,5笔。
		System.out.println(freemarkerProcess(freeParaMap, template));
	}
	
	
	public static String freemarkerProcess(Map input, String templateStr) {
		StringTemplateLoader stringLoader = new StringTemplateLoader();
		String template = "content";
		stringLoader.putTemplate(template, templateStr);
		Configuration cfg = new Configuration();
		cfg.setTemplateLoader(stringLoader);
		try {
			Template templateCon = cfg.getTemplate(template);
			StringWriter writer = new StringWriter();
			templateCon.process(input, writer);
			return writer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		return null;
	}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值