- 浏览: 1419751 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (409)
- Java (48)
- Spring (29)
- struts2 (17)
- hibernate (4)
- 设计模式 (24)
- jbpm (1)
- JavaScript (5)
- 统计报表 (7)
- ExtJS_3.0 (35)
- struts1 (1)
- 分析设计 (3)
- Flex3 (24)
- UML (2)
- 数据库 (18)
- PowerDesigner (2)
- 应用服务器 (3)
- WebService (5)
- ActiveMQ_5.3.2 (6)
- Java通信技术 (11)
- GWT (6)
- OSGi (15)
- android (11)
- liferay6.0.6 (13)
- jquery (13)
- Linux (3)
- java.util.concurrent (16)
- guava (9)
- 开发模式 (1)
- 大数据 (2)
- 互联网金融 (4)
- treegrid-3.0 (7)
- 分布式 (8)
- GO语言 (4)
- maven (1)
- 缓存技术 (6)
- 其他 (2)
- 前端页面 (1)
- heasy (1)
- spring cloud(F版) (21)
- springboot (12)
- springmvc (5)
- mybatis (3)
- dubbo (1)
- 物联网 (0)
最新评论
-
raymond.chen:
谢谢您的分享
使用Ngrok解决通过外网访问内网web应用 -
wangyudong:
速度有点慢,不过在也找到了一个开源的holer,配置一个key ...
使用Ngrok解决通过外网访问内网web应用 -
a1006458222:
...
Axis2的部署和应用 -
偷师来了:
不好意思 这样的博客我觉得就灭有必要分享出来了 命令大家都会看 ...
Consul框架介绍 -
lliiqiang:
怎么直接删除文件夹啊?固定的几个文件可以删除,不固定的呢?需要 ...
Flex AIR —— 文件读写
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点。
发表评论
-
keytool的使用
2019-08-28 15:12 457keytool是密钥和数字证书的管理工具。它使用户能够管理 ... -
Bitset数据结构的使用
2019-03-08 13:53 1834Bitset是Java中的一种数据结构。Bitset中主要 ... -
Disruptor:高性能低延迟的内存有界队列框架
2019-02-24 10:45 891Disruptor是用于在多个线程之间通信的高性能低延时的 ... -
java的类加载机制
2019-02-18 21:37 356ClassLoader的双亲委派模 ... -
ThreadLocal的使用范例
2019-02-16 19:30 491ThreadLocal用于保存某个线程的共享变量。 Thr ... -
反射工具包Reflections的使用
2019-02-16 17:51 2937Reflections 通过扫描 classpath,索引元 ... -
使用CGLIB对实现类进行动态代理
2019-01-31 19:12 2375CGLIB(Code Generation Library ... -
基于JDK动态代理实现Mybatis的Mapper功能
2019-01-31 18:40 853Mybatis通过定义Mapper接口类,类中的方法与map ... -
Java8新特性
2019-01-20 22:04 4991、Lambda表达式 ... -
使用百度API识别图片文字
2018-09-21 22:41 24531、注册百度账号 https://login.bce.b ... -
HanLP自然语言处理包的使用
2018-09-16 23:06 3057HanLP是由一系列模型与算法组成的Java工具包,目标是 ... -
org.apache.commons常用类的使用
2018-09-14 23:29 783/** * 文本相似度计算 */ ... -
图片转换为单色
2017-04-01 00:10 1425/** * 转为单色图片 */ privat ... -
Java事件机制范例
2016-11-28 15:22 2448java事件机制的参与者: event object:事件 ... -
编程方式的quartz2例子
2016-11-09 14:53 640Job类: public class MyJob imp ... -
数字证书格式
2016-11-06 20:44 1929PFX 或 P12 指以pkcs#12 ... -
Drools6使用范例
2016-10-15 23:50 27491、创建kmodule.xml文件 在s ... -
生成带logo的二维码图片
2016-05-25 18:21 1338源码如下: private static final in ... -
用HttpClient访问CXF的RESTful接口
2016-05-18 16:50 4410用CXF可以开发RESTful服务,服务接口的输入和输出支持 ... -
commons-configuration使用范例
2016-05-02 23:50 14701、访问属性文件 PropertiesConfigurat ...
相关推荐
ssm框架的freemarker---Demo
ssm项目整合,外加freemarker框架,maven项目,欢迎下载学习!!!
完整的maven+springmvc+mybatis+freemarker+mysql框架整合
框架整合 SpringBoot+SpringDataJpa+FreeMarker源码样例
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
Struts2整合FreeMarker实例框架初学例子. myeclipse 工程代码. 利用Struts2生成静态页面其实很灵活,很强大,尤其是利用Struts2对Freemarker较好的支持,充分利用Freemarker的模板功能来生成静态页面。 比较简单....
网上找不到springboot框架集成freemarker过程中再集成shiro框架,所以写了一个,供大家使用。
整合S2SH+Freemarker,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
适用于想拿freemarker练手的新手
该文档对于SpringMVC与Hibernate和Freemarker整合做了详细的阐述,通过范例使得大家对于三者整合框架的搭建有了更好的了解。
整了一个下午,整了一个SSHF框架,希望能帮助想要的人。实例比较简单,就是登陆和注册。
此案例实现了freemarker与spring框架的整合,并详细说明如何将视图层从jsp改为ftl模板
这是一个关于SSH框架整合 和用FreeMarker技术来将动态网页静态化的操作事例
Spring MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,这篇文章主要介绍了Spring MVC整合 freemarker及使用方法,需要的朋友可以参考下
该系统在技术上使用Spring+SpringMVC+MyBatis整合框架作为后台开发框架,AmazeUI作为前端开发框架。 并使用Ehcache作为项目的缓存,druid作为项目的数据库连接池,使用FreeMarker实现word的导出,使用Shiro完成项目...
SpringMVC+hibernate框架整合 项目中有包括用到的所有包,可以运行!前台用freemarker,开发人员只需要开发就行。
能生成静态的新闻页面可以供Struts+Spring+Hibernate+Freemarker框架的整合应用!
标准SSM框架源码,拿来即用,里面包含spring 整合springmvc,spring整合mybatis,ssm整合freemarker
3.freemarker-2.3.15.jar(支持freemarker的,在webwork中也有) 4.javassist.jar(一个开源的分析、编辑和创建Java字节码的类库,hibernate中也需要,引入其中一个即可) 5.ognl-2.7.3.jar(支持ognl语言) 6....
- 该系统在技术上使用Spring+SpringMVC+MyBatis整合框架作为后 台开发框架,AmazeUI作为前端开发框架。 并使用Ehcache作为项目的缓存,druid作为项目的数据库连接池, 使用FreeMarker实现word的导出,使用Shiro...