/**
* FreeMarker 辅助类
* @author Rubekid
*
*/
public class FreeMarkerHelper { /**
* 模板文件存放目录
*/
private static final String TEMPLATE_PATH = "/templates/freemarker/"; /**
* 获取模板内容
* @param tpl
* @param rootMap
* @return
* @throws IOException
* @throws TemplateException
*/
public static String fetch(String tpl, Object rootMap) throws IOException, TemplateException{
Template template = getTemplate(tpl);
Writer out= new StringWriter();
template.process(rootMap, out);
String result = out.toString();
out.flush();
return result;
} /**
* 输出显示
* @param tpl
* @param rootMap
* @param out
* @throws IOException
* @throws TemplateException
*/
public static void display(String tpl, Object rootMap, Writer out) throws IOException, TemplateException{
Template template = getTemplate(tpl);
template.process(rootMap, out);
out.flush();
} /**
* 获取模板
* @param tpl
* @return
* @throws IOException
*/
private static Template getTemplate(String tpl) throws IOException{
Configuration config = new Configuration();
String path = WebUtils.getContextPath() + TEMPLATE_PATH;
String name = tpl;
int pos = tpl.lastIndexOf("/");
if(pos > -1){
path += tpl.substring( 0, pos+1 );
name += tpl.substring( pos+1 );
} config.setDirectoryForTemplateLoading(new File(path));
config.setObjectWrapper(new DefaultObjectWrapper());
return config.getTemplate(name ,"utf-8");
} /**
* 获取模板(通过字符串)
* @param source
* @throws IOException
*/
private static Template getTemplate(String name, String source) throws IOException{
Configuration config = new Configuration();
StringTemplateLoader loader = new StringTemplateLoader();
loader.putTemplate(name, source);
config.setTemplateLoader(loader);
return config.getTemplate(name, "utf-8");
}

FreeMarker辅助的更多相关文章

  1. 第一个spring、springmvc、mybatis、freemarker项目小知识(一)

    1.持久层开发(Mybatis)    1.1 添加,非空字段必须给值,不是非空加判断,有值添加无值不添    1.2 删除,注意外键关系的级联删除.(事务传播行为)    1.3 更新,每个字段单独 ...

  2. SpringMVC整合FreeMarker实例

    FreeMarker作为模板引擎,是比较常用的. FreeMarker官方文档地址为:https://freemarker.apache.org/ 现在浏览器或者翻译工具这么多,对于英文方面,我想大多 ...

  3. spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。

    Spring jar包的描述:针对3.2.2以上版本 org.springframework spring-aop ——Spring的面向切面编程,提供AOP(面向切面编程)实现 org.spring ...

  4. 6.DNS公司PC访问外网的设置 + 主DNS服务器和辅助DNS服务器的配置

    网站部署之~Windows Server | 本地部署 http://www.cnblogs.com/dunitian/p/4822808.html#iis DNS服务器部署不清楚的可以看上一篇:ht ...

  5. spring源码分析之freemarker整合

    FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个Java类库,是一款程 ...

  6. Maven 整合FreeMarker使用

    pom.xml <!-- freemarker jar --> <dependency> <groupId>org.freemarker</groupId&g ...

  7. JAVA FreeMarker工具类

    FreeMarkerUtil.java package pers.kangxu.datautils.utils; import java.io.File; import java.io.StringW ...

  8. FreeMarker:怎么使用

    第一个FreeMarker程序 1. 建立一个普通的java项目:testFreeMarker 2. 引入freemarker.jar包 3. 在项目目录下建立模板目录:templates 4. 在t ...

  9. FreeMarker的基础语法

    FreeMarker语言 FreeMarker语言概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写. FreeMarker被设计用来生成HTML Web ...

随机推荐

  1. 武汉科技大学ACM :1002: 零起点学算法38——求阶乘和

    Problem Description 输入一个正整数n(n<=10),计算 S=1!+2!+3!+...+n! Input 输入一个正整数n(n<=10)(多组数据) Output 输出 ...

  2. CURL请求接口

    private function  http_curl($url,$data=null){ //1.初始化,创建一个新cURL资源                 $ch = curl_init(); ...

  3. thinkphp M 和模板用法

    <?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller { pu ...

  4. jquery上传控件个人使用

    转了一篇jquery的上传控件使用博文,但是,经过测试貌似不行,自己研究了一下,效果实现.记下,以后使用. 下载“Uploadify”,官方版本为php的,很多文件不需要,删除带.php的文件. &l ...

  5. php global范例

    Example #1 $GLOBALS 范例 <?phpfunction test() {    $foo = "local variable"; echo '$foo in ...

  6. 基于.NET MVC的高性能IOC插件化架构(一)

    最近闲下来,整理了下最近写的代码,先写写架构,后面再分享几个我自己写的插件 最近经过反复对比,IOC框架选择了Autofac,原因很简单,性能出众,这篇博文是我的各大IOC框架的性能测试:http:/ ...

  7. arm linux上的第一个应用程序 BOA移植

    1. 首先, linux在开发板上能跑起来了. 包括网络驱动也有了, ifconfig之后, 能看到在rcS里面设置的IP, 也能ping通windows主机了, 当然, 也要window关掉防火墙才 ...

  8. Notepad++ Java开发环境配置

    1. 安装JDK 下载JDK 6下载 http://developers.sun.com.cn/download/java_se.html 运行安装程序,按照屏幕提示完成JDK 6的安装,下面为安装路 ...

  9. 编写 capture filters

    编写 capture filters 如有转载,请在转载前给我提一些建议.谢谢. 百度查不到资料,为无能的百度搜索增加点营养的料. 读 http://www.n-cg.net/CaptureFilte ...

  10. Linux学习——卸载Ubuntu,安装CentOS,第一次使用命令

    最近,看了相关的资料,发现Ubuntu不是很利于学习Linux操作系统,而CentOS比较有利于学习Linux操作系统,就卸载了. 当然,对于菜鸟,自然是很没有主见. 卸载Ubuntu感觉很奇怪,下来 ...