首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

在项目中调整FreeMarker框架

2012-09-10 
在项目中整合FreeMarker框架? FreeMarker是一个用Java编写的模板引擎,既可以在基于MVC的应用程序中作为视

在项目中整合FreeMarker框架

? FreeMarker是一个用Java编写的模板引擎,既可以在基于MVC的应用程序中作为视图组件,也可以在非Web应用程序中使用,比如用于邮件模板的动态生成、字符串的动态生成等等。FreeMarker模板文件的扩展名为ftl,支持多种模板装载方式:1、从ClassPath路径装载,2、从Web应用程序目录装载,3、字符串变量等。下面将基于freemarker-2.3.10版本详细介绍如何将FreeMarker框架整合到Web系统中。

一、接口类?
???? 1、模板渲染接口类 —— 装载模板,用提供的数据渲染模板,返回输出字符串。

public interface TemplateRender {/** * 装载模板,用提供的数据渲染模板,返回输出字符串 *  * @param dataModel 数据模型 * @param template 模板 */public String render(Object dataModel, String template)throws Exception;}

?

2、字符串模板装载器 —— 来自于一个字符串变量

?????

public class StringTemplateLoader implements TemplateLoader {public void closeTemplateSource(Object templateSource) throws IOException {return;}public Object findTemplateSource(String name) throws IOException {return name;}public long getLastModified(Object templateSource) {return System.currentTimeMillis();}public Reader getReader(Object templateSource, String encoding) throws IOException {String ftlString = (String)templateSource;return new StringReader(ftlString);}}

?二、模板渲染接口类的实现类?
?????? 1、从ClassPath装载模板的渲染实现类

???????

public class ClassPathTemplateRender implements TemplateRender {private static Configuration config = null;public static ClassPathTemplateRender getInstance(){return new ClassPathTemplateRender();}public ClassPathTemplateRender(){if(config == null){config = new Configuration();config.setClassForTemplateLoading(this.getClass(), "/com/jeyo/common/freemarker/template"); //第二个参数指定模板所在的根目录,必须以“/”开头。try{config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss");config.setLocale(Locale.CHINA);}catch(Exception ex){ex.printStackTrace();}}}public String render(Object dataModel, String ftlFile) throws Exception {StringWriter stringWriter = new StringWriter();BufferedWriter writer = new BufferedWriter(stringWriter);Template template = config.getTemplate(ftlFile, Locale.CHINA, "UTF-8");template.process(dataModel,writer);writer.flush();return stringWriter.toString();}}

?

? 2、从Web应用程序目录装载模板的渲染实现类

???

public class ContextPathTemplateRender implements TemplateRender {private static Configuration config = null;public static ContextPathTemplateRender getInstance(ServletContext sc){return new ContextPathTemplateRender(sc);}public ContextPathTemplateRender(ServletContext sc){if(config == null){config = new Configuration();config.setServletContextForTemplateLoading(sc, "/freemarker/template"); //第二个参数指定模板所在的根目录,必须以“/”开头。try{config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss");config.setLocale(Locale.CHINA);}catch(Exception ex){ex.printStackTrace();}}}public String render(Object dataModel, String ftlFile)throws Exception {StringWriter stringWriter = new StringWriter();BufferedWriter writer = new BufferedWriter(stringWriter);Template template = config.getTemplate(ftlFile, Locale.CHINA, "UTF-8");template.process(dataModel, writer);writer.flush();return stringWriter.toString();}}

?3、从字符串变量装载模板的渲染实现类

???

public class StringTemplateRender implements TemplateRender {private static Configuration config = null;public static StringTemplateRender getInstance(){return new StringTemplateRender();}public StringTemplateRender(){if(config == null){config = new Configuration();config.setTemplateLoader(new StringTemplateLoader());try{config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss");config.setLocale(Locale.CHINA);}catch(Exception ex){ex.printStackTrace();}}}public String render(Object dataModel, String text) throws Exception {String ret = null;if((dataModel instanceof Map) == false){throw new IllegalArgumentException("参数dataModel必须为一个Map对象");}BufferedReader reader = new BufferedReader(new StringReader(text));Template template = null;try{template = new Template(null, reader, config, "UTF-8");}catch(Exception ex){ex.printStackTrace();}SimpleHash root = new SimpleHash();root.putAll((Map)dataModel);try{StringWriter stringWriter = new StringWriter();BufferedWriter writer = new BufferedWriter(stringWriter);template.process(root, writer);writer.flush();ret = stringWriter.toString();}catch(Exception ex){ex.printStackTrace();ret = null;}return ret;}}

????? 三、工具类

???

public class FtlUtil {public static String renderString(Map dataModel, String text)throws Exception{String ret = StringTemplateRender.getInstance().render(dataModel, text);return ret;}public static String renderFile(Object dataModel, String ftlFile)throws Exception{String ret = ClassPathTemplateRender.getInstance().render(dataModel, ftlFile);return ret;}public static String renderFile(ServletContext sc, Object dataModel, String ftlFile)throws Exception{String ret = ContextPathTemplateRender.getInstance(sc).render(dataModel, ftlFile);return ret;}}

?

四、代码实例?
????? 1、FTL文件 —— test.ftl

        ${username},您好:          ${content}                          系统管理员                               ${date}

?2、从ClassPath路径装载模板

??

Map dataModel = new HashMap();dataModel.put("username", "张三");dataModel.put("content", "您提交的申请我们正在处理中,感谢您的支持!");dataModel.put("date", commonUtil.formatDate(new java.util.Date(), "yyyy-MM-dd"));String resultStr = FtlUtil.renderFile(dataModel, "test.ftl");输出结果:张三,您好:您提交的申请我们正在处理中,感谢您的支持!系统管理员2008-09-04

????? 3、从字符串变量装载模板

???

String stringTemplate = "您好!注册已成功,您的用户名为:${username},密码为:${password}。感谢您的支持!";Map dataModel = new HashMap();dataModel.put("username", "张三");dataModel.put("password", "123456");String resultStr = FtlUtil.renderFile(dataModel, stringTemplate);输出结果:您好!注册已成功,您的用户名为:张三,密码为:123456。感谢您的支持!

?

4、从Web应用程序目录装载模板
???????? 参考第2点。

热点排行