Freemarker_Thymeleaf_Velocity模板或者字符串渲染

前言

博主github
博主个人博客http://blog.healerjean.com

1、Freemarker解析模板

   <!--freemarker 模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

1.1、ftl模板

<?xml version="1.0" encoding="utf-8"?>
<root id="" comment="">
    <person>
        <person>${x.person}</person>
        <age>${x.age}</age>
        <name>${name}</name>
        <sex>${x.sex}</sex>
    </person>
</root>  
1.1.2、制作map
1.1.2.1、DTO
@Data
public class XmlEntry {
   
	
	public String person ;
	public Integer age;
	public Integer sex;
	public String name ;
	public double salary; 
	public Integer classPersonNum;

	
}
1.1.2.2、DTO
	private static Map demo(){
     
				XmlEntry p =new XmlEntry();  
				p.setAge(24);
				p.setPerson("张宇晋"); 
				p.setSex(1);
				Map map = new HashMap();  
				map.put("name", "zhangyujin");
				map.put("x", p);  
			return map;  
		}
1.2.3、根据不同模板目录生成文本

/**
设置FreeMarker的模版文件位置

1、类路径,当前类下面的文件
cfg.setClassForTemplateLoading(CreateXmlByVmFile.class,"");
	或者
TemplateLoader templateLoaderClass = new ClassTemplateLoader(CreateXmlByVmFile.class,"");
cfg.setTemplateLoader(templateLoaderClass);

2、文件系统 ,文件目录
cfg.setDirectoryForTemplateLoading(fileDirectory);
	或者
TemplateLoader templateLoaderFile=new FileTemplateLoader(new File("E://hlj/xml/"));
cfg.setTemplateLoader(templateLoaderFile);

3、Servlet Context。
setServletContextForTemplateLoading(Object servletContext, String path);

4、resource目录,观察下面的工具类

*/

public  static String createXmlFile(String TemplateName, Map dataMap) throws TemplateException, IOException     {
   


		  //得FreeMarker配置对象
		  // 创建Configuration对象
		   Configuration cfg = new Configuration();
		   cfg.setEncoding(Locale.CHINA, "UTF-8");

		   File file = new File(CreateXmlByVmFile.class.getResource("").getFile());
		   cfg.setDirectoryForTemplateLoading(file);
		   Template template = cfg.getTemplate(TemplateName,"utf-8");

		   StringWriter w =new 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值