freemarker 用template快速构造XML
来源:互联网 发布:数据分析项目案例 编辑:程序博客网 时间:2024/04/30 12:53
freemarker 用template快速构造XML
1. 需要jar freemarker-2.3.8.jar
2. demo 如下:
import java.io.File;import java.io.IOException;import java.io.StringWriter;import java.util.HashMap;import java.util.Map;import freemarker.core.Environment;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateExceptionHandler;public class Test {/** * <一句话功能简述> * <功能详细描述> * @param map map * @param fileName fileName * @return 字符串流 * @see [类、类#方法、类#成员] */ @SuppressWarnings("unchecked") public static String buil(Map map, String fileName) { String url = Test.class.getResource("").getPath().replaceAll("%20", " "); String path = url; StringWriter out = new StringWriter(); try { Configuration configuration = new Configuration(); configuration.setDirectoryForTemplateLoading(new File(path)); Template template = configuration.getTemplate(fileName); template.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); template.setEncoding("UTF-8"); template.setOutputEncoding("UTF-8"); Environment env = template.createProcessingEnvironment(map, out); env.process(); out.flush(); } catch (Exception e) { } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return out.toString(); }public static void main(String[]arrgs){Map<String,String> map= new HashMap<String,String>();map.put("id", "test123456");System.out.println(buil(map,"test.ftl"));}}
test.ftl
<xml id ="${id}"></xml>参考网站如下:
http://freemarker.org/
0 0
- freemarker 用template快速构造XML
- Freemarker template
- FreeMarker template error!
- Struts2 freemarker.template.TemplateModelException
- freemarker template error
- FreeMarker template error
- FreeMarker template error! struts不识别--model类构造函数问题
- 用freemarker+xml生成word
- freemarker.template.TemplateException:Error parsing including template
- 用XMLDOM构造XML
- 快速幂相关(template)
- Struts2中遇到FreeMarker template error!怎么办?
- freemarker.template.TemplateException:Error executing macro:mainSelect
- freemarker.template.TemplateException: Expected collection or sequence
- FreeMarker快速入门1
- FreeMarker快速上手
- FreeMarker 快速入门
- FreeMarker快速上手
- 10个惊艳的Swift单行代码
- JDBC笔记-Transaction
- gulp 组件化
- 关于Android中如何过滤HTML标签
- 轴承故障诊断
- freemarker 用template快速构造XML
- 调节音量时隐藏系统提示框(Hide device volume HUD when adjust the volume)
- JAVA语言之基数排序
- OC里面的类
- [算法导论] 快速排序
- Adobe Photoshop CC2015 下载-安装-注册机破解
- String abc=null;String abc="";String abc;三种写法有什么区别?
- 友元
- MATLAB与C/C++混合编程之MATLAB调用C程序