人啊,有的時候就是沒有辦法堅持一些事情,總是因為各種理由在推脫,逐漸就變成了拖延症!例如胖先生的減肥計划,其實本來就沒有計划,屬於散漫形式的!一直減肥,一直在肥!總是說沒有時間,沒有時間!其實有時候就是自己懶!
人啊,有時候想去堅持,總是搞不懂自己要什么?也是自己沒有毅力吧!
話題扯的有點遠了,再次更新一下關於Freemarker的使用,關於迭代集合,這個還是很實用的!
今天買了一本書,希望自己能堅持讀完吧《編碼高質量代碼--改善Java程序的151個建議》算不算推廣呢
/** * 測試研究題目為: * 數組,map以及list */ @Test public void 測試集合() { // 1.定義一個數組 String[] usernames = new String[]{"悟空","八戒","唐僧","沙僧"};// // 2.定義個Map Map<String, Object> map = new HashMap<String, Object>(); map.put("book", "紅樓夢"); map.put("name", "黛玉"); //3.定義一個List中存儲的為Map List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>(); mapList.add(map); map = new HashMap<String, Object>(); map.put("book", "三國殺"); map.put("name", "劉備"); mapList.add(map); //4.定義一個List中存儲數據為User對象 List<User> userList = new ArrayList<User>(); userList.add(new User("林沖", 18)); userList.add(new User("孫二娘", 20)); //以上的數據准備完畢,現在要傳遞數據到模板中並且要生成HTML頁面 //1.創建數據模型 Map<String, Object> root = new HashMap<String, Object>(); root.put("shxt_array", usernames); root.put("shxt_map", map); root.put("shxt_list_map", mapList); root.put("shxt_list_object", userList); // 3.生成HTML文件 fu.exportHtml("demo05.ftl", root, "集合聯系.html"); }
模板文件代碼demo05.ftl:
<!DOCTYPE html> <html> <head> <title>處理集合數據</title> <meta charset="UTF-8"> </head> <body> <#--我是注釋--> <h2>遍歷數組</h2> <#list shxt_array as username> ${username} </#list> <h2>遍歷固定的數組范圍:數據引用</h2> <#-- 定義變量 --> <#assign news=shxt_array[1..2]/> <#list news as username> ${username} </#list> <hr/> <h2>遍歷map:首先要取得所有的keys</h2> <#assign shxts=shxt_map?keys/> <#-- 遍歷shxts --> <#list shxts as key> ${shxt_map[key]} </#list> <hr/> <h2>遍歷List中存儲的是Map,兩種寫法</h2> <#list shxt_list_map as map> ${map.book}--${map["name"]}<br/> </#list> <h2>遍歷List中存儲的是User</h2> <#list shxt_list_object as user> ${user.account}--${user["age"]}<br/> </#list> </body> </html>
運行結果:


如果你感覺胖先生的文章對你有所幫助,請加微信,發發紅包,給我一點動力!
