FreeMarkerUtils,GsonHelper,MobileUtils

看到了這個,主要是之前在電商項目中用到了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)】待審批"	
		);

	}







}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章