<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
/**
* Freemarker实例Controller
*/
@RequestMapping("/freemarker")
@Controller
public class FreemarkerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/test1")
public String test1(Map<String,Object> map){
map.put("name","fly");
Student stu2 = new Student();
Student stu1 = new Student("小明",18,new Date(),1000.22f,null,stu2);
stu2.setName("小红");
stu2.setMoney(222.1f);
stu2.setAge(12);
//list数据
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
map.put("stus",stus);
//map数据
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
map.put("stu1",stu1);
map.put("stuMap",stuMap);
map.put("today",new Date());
return "test1";
}
}

templates/test1.ftl:

<#--
freemarker实例
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>hello ${name}!</h4>
<table border="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#--遍历list-->
<#list stus as stu>
<tr>
<td>${stu_index + 1}</td> <#-- _index,循环下标,从0开始-->
<td>${stu.name}</td>
<td>${stu.age}</td>
<td>${stu.money}</td>
</tr>
</#list>
</table>
<#--遍历map-->
stu1: <br>
姓名: ${stuMap['stu1'].name} <br>
年龄: ${stuMap['stu1'].age} <br>
stu1: <br>
姓名: ${stuMap.stu1.name} <br>
年龄: ${stuMap.stu1.age} <br>
遍历map
<table border="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#list stuMap?keys as k>
<tr>
<td>${k_index + 1}</td>
<#--if 指令-->
<td <#if stuMap[k].name=='小明'>style="background: red;"</#if>>
${stuMap[k].name}
</td>
<td>${stuMap[k].age}</td>
<td>${stuMap[k].money}</td>
</tr>
</#list>
</table>
<#--空值处理-->
<#if stu1??>
<p>${stu1.name}</p>
</#if>
<br>
<#--内建函数-->
<#--集合大小-->
集合大小${stus?size}
年月日${today?date}
时分秒${today?time}
年月日+时分秒${today?datetime}
自定义格式${today?string("yyyy年MM月")}
<#--内建函数c 三位分割-->
${1111112221?c}
<#--json转对象-->
<#assign text="{'bank':'工商银行','account':'101010101010001010'}"/>
<#assign data=text?eval/>
户行${data.bank} 账号${data.account}
</body>
</html>

/**
* freemarker静态化测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShuJuApplication.class)
public class FreemarkerTest {
@Test
public void testGenerateHtml() throws Exception {
Configuration configuration = new Configuration(Configuration.getVersion());
String path = this.getClass().getResource("/").getPath();
configuration.setDirectoryForTemplateLoading(new File(path+"/templates"));
configuration.setDefaultEncoding("utf-8");
Template template = configuration.getTemplate("test1.ftl");
Map<String,Object> map = new HashMap<>();
map.put("name","fly");
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content, "utf-8");
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\uploads\\"+"test1.html"));
IOUtils.copy(inputStream,fileOutputStream);
}
@Test
public void testGenerateHtml2() throws Exception {
Configuration configuration = new Configuration(Configuration.getVersion());
String templateString = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
"<h4>hello ${name}!</h4>\n" +
"</body>\n" +
"</html>";
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("template",templateString);
configuration.setTemplateLoader(stringTemplateLoader);
Template template = configuration.getTemplate("template","utf-8");
Map<String,Object> map = new HashMap<>();
map.put("name","fly");
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content, "utf-8");
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\uploads\\"+"test1.html"));
IOUtils.copy(inputStream,fileOutputStream);
}
}

freemarker demo的更多相关文章

  1. 网页静态化解决方案-Freemarker demo+语法

    1.网页静态化技术Freemarker 1.1为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说 ...

  2. DEMO: springboot 与 freemarker 集成

    直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...

  3. Maven+SpringMVC+Freemarker入门Demo

    1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为mavenspringmvcfreemarker的 ...

  4. Freemarker入门Demo

    1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...

  5. Freemarker的简单demo

    第一步.导入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...

  6. SpringMVC整合Freemarker(含Demo源码)(转)

    转自:http://blog.csdn.net/sinat_27535209/article/details/61199452 整合过程如下: 1.新建一个maven web工程,使用maven依赖s ...

  7. 页面静态化技术Freemarker技术的介绍及使用实例.

    一.FreeMarker简介 1.动态网页和静态网页差异 在进入主题之前我先介绍一下什么是动态网页,动态网页是指跟静态网页相对应的一种网页编程技术.静态网页,随着HTML代码的生成,页面的内容和显示效 ...

  8. SpringMVC+FreeMarker

    前言: 最近在学习SpringMVC,模板引擎用的是FreeMarker,之前没有接触过.利用SpringMVC开发其实还有许多的步骤,比如控制层,服务层,持久化层,实体等等,先弄了一个小demo来总 ...

  9. springmvc使用freemarker

    首先需要添加freemarker.jar到项目,如果项目中有spring或者spirngmvc,需要整合,首先配置freemarkerConfig,代码结构如下 <!-- 设置freeMarke ...

随机推荐

  1. java课堂笔记

  2. tf.contrib.slim

    https://blog.csdn.net/mao_xiao_feng/article/details/73409975

  3. python-数据类型练习题1

    1.有变量name = "aleX leNb" 完成如下操作:移除name变量对应的值两边的空格,并输出处理结果n1 = name.strip()print(n1) 结果:aleX ...

  4. react系列笔记:第一记-redux

    前言: 目前公司使用dva,对于前不久还是使用原生js的我来说,花了差不多一两周时间,基本掌握如何使用.虽然对于react有一点点基础,但很多地方未深入,很多概念也很模糊,故从本文开始,记录一下系统的 ...

  5. nginx配置ssl证书

    一:加装nginx的ssl模块 1.1:切换到源码包 cd /zz/nginx-1.14.2 1.2:查看已安装模块 /usr/local/nginx/sbin/nginx -V [root@game ...

  6. 通用c程序Makefile

    #一个通用的Makefile,可以在linux各大平台下通用编译c程序,这个版本在gcc平台下实现,如需要课更改第二部的gcc,也可以在第三部添加所需要的库,如有错误,欢迎即使纠正 #1.遵循可移植操 ...

  7. hadoop sentry错误记录

    1.报无法实例化metastore连接 hive> show tables; FAILED: SemanticException org.apache.hadoop.hive.ql.metada ...

  8. 修改hbuilder背景颜色为护眼模式

    复制以下代码,保存为.tmTheme文件导入HBuilder <?xml version="1.0" encoding="UTF-8"?> < ...

  9. 微信退款回调AES算法(AES-256-ECB)

    解密步骤如下: (1)对加密串A做base64解码,得到加密串B (2)对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)--> ...

  10. GitHub网站使用的基础入门

    github基本入门,首先需要掌握一些ssh非对称加密的知识和一些基本的git命令操作.下面是我推荐的两个比较好的网站,然后我再专门对GitHub网站使用进行步骤讲解. git 命令:http://b ...