看到了这个,主要是之前在电商项目中用到了freemarker,所以 弄下来也指不定哪天回派上用场!
不过没注释。师父啊,你为什么步步不注释啊,我心累cry.
public class FreeMarkerUtils{
protected static final Configuration cfg = new Configuration( new Version(2,3,0) );
//注册freemarker
public static final void register(Object context , String path){
//cfg.setDirectoryForTemplateLoading( new File("/templates") );--我是说项目中怎么有这个文件,原来在这里用到的啊
//cfg.setServletContextForTemplateLoading( getServletContext(),"WEB-INF/templates" );--,你找的我苦啊,555555
cfg.setServletContextForTemplateLoading( context,path);
}
//获取数据
public static final String getData(Map<String ,Object> map,String templateFile)throws Exception{
Template t = cfg.getTemplate(templateFile); //获取模板文件
File f = File.createTemplateFile("tmp",".txt"); //创建临时文件f,这个很有局限性,只能 .txt 吗?
//在程序退出时删除临时文件
f.deleteOnExit(); --这个应该只是个开关效果
OutputStream os = new FileOutputStream(f);
try{
t.process(map,new OutputStreamWriter(os)); // 应该是吧 map数据 写出到 临时文件f中去。
os.flush();
}finally{
os.close();
}
BufferedReader reader = new BufferedReader( new FileReader(f) );
try{
String str = "";
StringBuffer result = new StringBuffer();
while( (str = reader.readLine()) != null ){
result.append(str);
}
return result.toString();
}finally{
reader.close();
}
//测试
public static final void test() throws Exception{
Map<String ,Object> root = new HashMap<>();
root.put("name","FreeMarker!");
root.put("msg","您已经完成了第一个FreeMarker的示例");
System.out.println(getData(root,"test.ftl"));
}
}
}//类尾
GsonHelper
具体这个类是干嘛的,我也不是很清楚。只是看到他好像可以把 list 转换成json 数据格式。但是能把 list转换成json格式的应该不指这一个吧,我们之前学习的时候没用过这个。一起看看吧:
package com.xxxxx.common.utils
import java.text.DateFormat;
import com.google.gson.FieldNamingPolicy; --字段命名策略
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
//原来是 google 的 工具类啊
public class GsonHelper{
//内部类单例模式,延迟加载,线程安全(java中class加载时互斥的原因),也减少了内存消耗
private static class SingletonHolder{
private static Gson instance =
new GsonBuilder().setDateFormat(DateFormat.LONG)
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting().create();
}
public static Gson getInstance(){
return SingletonHolder.instance;
}
}
GsonHelper 在项目里面的用法:
在MobileUtils中用到,都是在common.utils包下
public class MobileUtils{
//配置cookie位生成的http请求都添加这个cookie
protected static final CookieStore cookieStore = new BasicCookieStore();
//发送消息
public static void sendMessage(String sender,String sendername,String receiver,String receivername,String title,String message)throws Exception{
//发送消息json 设置
Map<String ,String> json = new HashMap<>();
json.put("systemID","4");
json.put("messageID",StringUtils.getUUID19()); //19位UUID码作为消息Id
json.put("messageTitle",title);
json.put("messageBody",message);
json.put("createTime",DateUtils.toString(new Date(),"yyyy-MM-dd HH:mm:ss"));
json.put("sendNumber",sender); //发送者手机号
json.put("senderName",sendername);//发送短信者
json.put("recipientType","1");
//接收消息recipientJson设置
List<Map<String,String>> recipientJson = new LinkedList<>();
Map<String,String > item = new HashMap<>();
item.put("RecipientNumber",recevier); //收信者号码
item.put(“RecipientName”,receivername);//收信者名字
recipientJson.add(item);
json.put("recipientJson",GsonHelper.getInstance().toJson(recipientJson));
}
//main 方法测试
public static void main (String[] args) throws Exception{
MobileUtils.sendMessage(
"00000","销售一站式平台","27481","宋总","【印鉴外带申请单(00000000143)】待审批","【印鉴外带申请单(00000000143)】待审批"
);
}
}