java velocity模版_velocity模板实战

packagecom.sunline.nfs.process;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.io.StringWriter;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.Properties;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importorg.apache.velocity.Template;importorg.apache.velocity.VelocityContext;importorg.apache.velocity.app.Velocity;importorg.apache.velocity.app.VelocityEngine;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;public classvelocityEngine {static Map jsonMap = new LinkedHashMap();public static Map recursinMap(Mapmap){

net.sf.json.JSONObject jObject= newnet.sf.json.JSONObject();for(String key:map.keySet()){

String str=map.get(key).toString();if(str.startsWith("{")){/*com.alibaba.fastjson.JSONObject jObject = JSON.parseObject(str);*/Map map2 =jObject.fromObject(str);if(!map.toString().endsWith(map2.toString()+"}")){int i = map.toString().indexOf(map2.toString())+1;

String value="{" + map.toString().substring((i+map2.toString().length()),map.toString().length());

map2.put(key, value);

}returnrecursinMap(map2);

}else{if(str.startsWith("[")){

JSONArray tmpMaps=JSONArray.fromObject(str);

System.out.println("key="+key+" tmpMaps"+tmpMaps);

jsonMap.put(key, tmpMaps);/*for(int i=0;i

JSONObject job = tmpMaps.getJSONObject(i);

Map jobMap = JSONObject.fromObject(job.toString());

for(String key2:jobMap.keySet())

jsonMap.put(key+i+key2, jobMap.get(key2));

}*/}else{

jsonMap.put(key, str);

}

}

}returnjsonMap;

}

@SuppressWarnings("null")public static void main(String[] args) throwsIOException{

String filename= "ObtainAppNo.txt";

String json=readJson(filename);

net.sf.json.JSONObject jObject= newnet.sf.json.JSONObject();

Map map2 =jObject.fromObject(json);

Map jsonMap =recursinMap(map2);

System.out.println("-----" +jsonMap);//初始化参数

Properties properties=newProperties();//设置velocity资源加载方式为class

properties.setProperty("resource.loader", "class");//设置velocity资源加载方式为file时的处理类

properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");//实例化一个VelocityEngine对象

VelocityEngine velocityEngine=newVelocityEngine(properties);//实例化一个VelocityContext

VelocityContext context=newVelocityContext();for(Object key :jsonMap.keySet()){

context.put((String)key, jsonMap.get(key));

}//实例化一个StringWriter

StringWriter writer=newStringWriter();//假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm//String json2 = readJson("2.txt");//velocityEngine.mergeTemplate(json2, "gbk", context, writer);//这种方式是正确的

velocityEngine.mergeTemplate("2.txt", "gbk", context, writer);

String str=writer.toString();

str= str.replaceAll("\\s*", "");

System.out.println("-----" +str);

}public staticString readJson(String filename){

Resource resource=newClassPathResource(filename);

File file;try{

file=resource.getFile();

BufferedReader reader= new BufferedReader(newFileReader(file));

String temp=null;

String str="";while((temp=reader.readLine())!=null){

str= str +temp;

}

str=str.replace(" ", "");returnstr;

}catch(IOException e) {

e.printStackTrace();

}return null;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值