freemarker获取html模板进⾏渲染输出⽂章⽬录
freemarker获取html模板进⾏渲染输出
应⽤场景
1、获取html⽂件内容进⾏模板解析,返回到页⾯展⽰
2、获取html⽂件内容进⾏模板解析,进⾏邮件发送
3、获取xml⽂件内容进⾏模板接卸,⽣成office办公⽂件,达到apache poi的效果
⼀些class引⽤官⽅demo,当做测试。
maven⼯程配置引⼊依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
创建获取模板测试类
plate.Configuration;
plate.Template;
plate.TemplateException;
plate.TemplateExceptionHandler;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
/**
* @author yunfeng
* @version V.1.0
* @title
* @Desc
* @create 2018-05-14 22:49
**/
public class TestFm {
@Test
public void test()throws IOException {
/* ------------------------------------------------------------------------ */
/* You should do this ONLY ONCE in the whole application life-cycle: */
/* Create and adjust the configuration singleton */
Configuration cfg =new Configuration(Configuration.VERSION_2_3_27);
cfg.setDirectoryForTemplateLoading(new File("d:/freemarker/templates"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
/* ------------------------------------------------------------------------ */
/* You usually do these for MULTIPLE TIMES in the application life-cycle: */
/* Create a data-model */
Map root =new HashMap();
root.put("user","Big Joe");
Product latest =new Product();
latest.setUrl("products/greenmouse.html");
latest.setName("green mouse");
root.put("latestProduct", latest);
/* Get the template (uses cache internally) */
Template temp = Template("test.ftlh");
/* Merge data-model with template */
Writer out =new OutputStreamWriter(System.out);
try{
temp.process(root, out);
}catch(TemplateException e){
e.printStackTrace();
}
// Note: Depending on what `out` is, you may need to call `out.close()`.
// This is usually the case for file output, but not for servlet output.
}
}
Product类
/
**
* @author yunfeng
* @version V.1.0
* @title
* @Desc
* @create 2018-05-14 22:51
**/
public class Product {
private String url;
private String name;
// As per the JavaBeans spec., this defines the "url" bean property // It must be public!
public String getUrl(){java修改html文件
return url;
}
public void setUrl(String url){
this.url = url;
}
// As per the JavaBean spec., this defines the "name" bean property // It must be public!
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
执⾏输出
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome Big Joe!</h1>
<p>Our latest product:
<a href="products/greenmouse.html">green mouse</a>!
</body>
</html>
Freemarker相应⼯具类FreemarkerUtil
plate.Configuration;
plate.Template;
plate.TemplateException;
plate.TemplateExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
/**
* @author yunfeng
* @version V1.0
* @date 2018-05-24 22:55
*/
public class FreemarkerUtil {
private final static Logger logger = Logger(QuartzUtils.class);
/**
* 获取模板内容
*
* @param template 模板⽂件
* @param map 模板参数
* @return 渲染后的模板内容
* @throws IOException IOException
* @throws TemplateException TemplateException
*/
public static String getTemplate(String template, Map map)throws IOException, TemplateException {
Configuration cfg =new Configuration(Configuration.VERSION_2_3_27);
String templatePath = Resource("/").getPath();
logger.info("FreemarkerUtil template = "+ template);
logger.debug("templatePath = "+ templatePath);
cfg.setDirectoryForTemplateLoading(new File(templatePath));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
cfg.setWrapUncheckedExceptions(true);
Template temp = Template(template);
StringWriter stringWriter =new StringWriter();
temp.process(map, stringWriter);
String();
}
}
总结
然后项⽬中通过调⽤FreemarkerUtil .getTemplate(String template, Map map)即可返回对应渲染模板内容。也可以⽤其他模板框架去渲染页⾯jsonup
注意事项
Configuration缓存 Template实例,所以当你Template(“test.ftlh”)下次调⽤ 时,它可能不会再读取和解析模板⽂件,只是Template第⼀次返回相同的 实例。
不要不必要地重新创建Configuration 实例; 创建很消耗性能,⽽且会失去了模板缓存。Configuration实例应当作为应⽤程序级别的单例。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论