<!--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. 【转载】安装 gephi 软件

    作者:小小爽链接:https://www.zhihu.com/question/21268129/answer/354924066来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  2. flask-bootstrap学习笔记

    flask-bootstrap 是flask一个方便扩展使用bootstrap前台的扩展. 使用方法: 安装: pip instal flask-bootstrap4 加载: from flask_b ...

  3. MySQL拓展操作

    MySQL除了基本的增删该查功能,还有以下拓展功能: create table t1( id int ...., num int, xx int, unique 唯一索引名称 (列名,列名), con ...

  4. js jquery 正则去空字符

    1.正则去空字符串: var str1=" a b c "; var strtrim=str1.replace(/\s/g,""); 2.js去前后空字符串: ...

  5. 命令行运行Android Robotium自动化用例或单元测试用例

    本文目录 1.运行所有的测试用例 2.运行单个测试类或某个TestSuite 3.运行某个测试类里面的某个测试方法 4.运行两个不同的测试类或类中的方法 命令行运行Android Robotium自动 ...

  6. Ubuntu 18.04安装VNC远程登录

    reference: https://blog.csdn.net/bluewhalerobot/article/details/73649353 https://community.bwbot.org ...

  7. shell编程(二)

    第三十二次课 shell编程(二) 目录 十五.shell中的函数 十六.shell中的数组 十七.告警系统需求分析 十八.告警系统主脚本 十九.告警系统配置文件 二十.告警系统监控项目 二十一.告警 ...

  8. vue-resource 和 axios的区别

    vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必要引入jQuery.vue-resource是Vue.js的 ...

  9. 手机端flex、字体设置、快速点击

    ;(function flexible (window, document) { var docEl = document.documentElement ♥1 var dpr = window.de ...

  10. 微信小程序笑话小程序实践开发学习

    首先做出笑话展示页面 1.修改app.json文件,在"pages"中添加一条 "pages/joke/joke",然后ctrl+s就可以自动创建joke文件夹 ...