日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Strut2和FreeMarker整合时的一些问题

發(fā)布時間:2024/9/27 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Strut2和FreeMarker整合时的一些问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在struts2與freemarker整合時,可以在項目的類路徑下放置freemarker.properties配置文件,

struts2會自動讀取(struts2封裝了)。今天用freemarker作為模板生成靜態(tài)文件時,發(fā)現(xiàn)并不讀取freemarker.properties配置文件。

看一下freemarker源碼,freemarker本身并不加載freemarker.properties配置文件,需要代碼中指定配置文件。

代碼如下:

?

Java代碼 ?
  • package?com.ldl.freemarker; ??
  • ??
  • import?java.io.BufferedWriter; ??
  • import?java.io.IOException; ??
  • import?java.io.OutputStreamWriter; ??
  • import?java.io.Writer; ??
  • import?java.util.HashMap; ??
  • import?java.util.Map; ??
  • import?java.util.Properties; ??
  • ??
  • import?freemarker.cache.ClassTemplateLoader; ??
  • import?freemarker.template.Configuration; ??
  • import?freemarker.template.DefaultObjectWrapper; ??
  • import?freemarker.template.Template; ??
  • import?freemarker.template.TemplateException; ??
  • ??
  • public?class?freemarkerTest?{ ??
  • ??
  • ????/** ?
  • ?????*?@param?args ?
  • ?????*?@throws?IOException? ?
  • ?????*?@throws?TemplateException? ?
  • ?????*/??
  • ????@SuppressWarnings("unchecked") ??
  • ????public?static?void?main(String[]?args)?throws?IOException,?TemplateException?{ ??
  • ????????Configuration?cfg?=?new?Configuration(); ??
  • ????????cfg.setTemplateLoader(new?ClassTemplateLoader(freemarkerTest.class,"")); ??
  • ????????cfg.setObjectWrapper(new?DefaultObjectWrapper()); ??
  • ????????//加載并設(shè)置freemarker.properties ??
  • ????????Properties?p?=?new?Properties(); ??
  • ????????p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("freemarker.properties")); ??
  • ????????cfg.setSettings(p); ??
  • ????????/*?在整個應(yīng)用的生命周期中,這個工作你可以執(zhí)行多次?*/??
  • ????????/*?獲取或創(chuàng)建模板*/??
  • ????????Template?temp?=?cfg.getTemplate("test.xml"); ??
  • ????????/*?創(chuàng)建數(shù)據(jù)模型?*/??
  • ????????Map?root?=?new?HashMap(); ??
  • ????????root.put("user",?"Big?Joe"); ??
  • ????????/*?將模板和數(shù)據(jù)模型合并?*/??
  • ????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(System.out)); ??
  • ????????temp.process(root,?out); ??
  • ????????out.flush(); ??
  • ????} ??
  • }??
  • package com.ldl.freemarker;import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import java.util.Properties;import freemarker.cache.ClassTemplateLoader; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException;public class freemarkerTest {/*** @param args* @throws IOException * @throws TemplateException */@SuppressWarnings("unchecked")public static void main(String[] args) throws IOException, TemplateException {Configuration cfg = new Configuration();cfg.setTemplateLoader(new ClassTemplateLoader(freemarkerTest.class,""));cfg.setObjectWrapper(new DefaultObjectWrapper());//加載并設(shè)置freemarker.propertiesProperties p = new Properties();p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("freemarker.properties"));cfg.setSettings(p);/* 在整個應(yīng)用的生命周期中,這個工作你可以執(zhí)行多次 *//* 獲取或創(chuàng)建模板*/Template temp = cfg.getTemplate("test.xml");/* 創(chuàng)建數(shù)據(jù)模型 */Map root = new HashMap();root.put("user", "Big Joe");/* 將模板和數(shù)據(jù)模型合并 */Writer out = new BufferedWriter(new OutputStreamWriter(System.out));temp.process(root, out);out.flush();} }

    ?測試代碼在附件中,freemarker.properties里的default_encoding值為utf-8,看控制臺打印的是否有亂碼,如果有亂碼說明生效了。

    總結(jié)

    以上是生活随笔為你收集整理的Strut2和FreeMarker整合时的一些问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。