<!--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. linux安装anaconda3 conda: command not found

    在使用Anaconda3时出现: conda:未找到命令 最后发现每次开机后都要运行一个命令才行:export PATH=~/anaconda3/bin:$PATH 如果要永久保存路径: 1.在终端输 ...

  2. centos源码安装mysql5.7.25-boost

    首先在CentOS6.5的版本安装mysql 创建安装目录,并解压 安装mysql的依赖包.这里需要用到阿里源,去吧阿里源宕到本机 安装依赖包,gcc.gcc-c++.cmake.ncurses-de ...

  3. Mvc请求的生命周期

    ASP.NET Core : Mvc请求的生命周期 translation from http://www.techbloginterview.com/asp-net-core-the-mvc-req ...

  4. Tikhonov regularization 吉洪诺夫 正则化

    这个知识点很重要,但是,我不懂. 第一个问题:为什么要做正则化? In mathematics, statistics, and computer science, particularly in t ...

  5. net core 随笔

    UseApplicationInsights  这个有用到azure 才有用, 平时没用的话可以去掉. 遥测. 上下文指的是 进程间占有的资源空间. 当一个进程时间片到了或者资缺的时候就会让出CPU ...

  6. react native 打包上架

    https://www.jianshu.com/p/ce71b4a8a246 react-native bundle --entry-file index.ios.js --platform ios ...

  7. linux小计

    一.查看远程主机开放端口命令 nc -zv 10.10.79.89 5151 二.远程登录ssh/scp yum install sshpass sshpass -p youpassword scp ...

  8. WeX5学习笔记 - 01

    了解WeX5,有朋友突然自己开始学习WeX5,我了解后觉得挺不错的实用范围广,现在手机上的主流软件基本都可以采用WeX5进行开发,如淘宝,美团.手机银行等,所以自己也开始学习WeX5,WeX5是Ecl ...

  9. L2-001 紧急救援 (25 分)

    L2-001 紧急救援 (25 分)   作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...

  10. 跟技术胖学vue+koa

    首页热卖商品组件 技术点:1热卖商品封装成单独的组件  2路由和参数的传递  3详情页面路由参数的接收 //首页 <template> <div> <div class= ...