freemarker demo
<!--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的更多相关文章
- 网页静态化解决方案-Freemarker demo+语法
1.网页静态化技术Freemarker 1.1为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说 ...
- DEMO: springboot 与 freemarker 集成
直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...
- Maven+SpringMVC+Freemarker入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为mavenspringmvcfreemarker的 ...
- Freemarker入门Demo
1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- Freemarker的简单demo
第一步.导入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- SpringMVC整合Freemarker(含Demo源码)(转)
转自:http://blog.csdn.net/sinat_27535209/article/details/61199452 整合过程如下: 1.新建一个maven web工程,使用maven依赖s ...
- 页面静态化技术Freemarker技术的介绍及使用实例.
一.FreeMarker简介 1.动态网页和静态网页差异 在进入主题之前我先介绍一下什么是动态网页,动态网页是指跟静态网页相对应的一种网页编程技术.静态网页,随着HTML代码的生成,页面的内容和显示效 ...
- SpringMVC+FreeMarker
前言: 最近在学习SpringMVC,模板引擎用的是FreeMarker,之前没有接触过.利用SpringMVC开发其实还有许多的步骤,比如控制层,服务层,持久化层,实体等等,先弄了一个小demo来总 ...
- springmvc使用freemarker
首先需要添加freemarker.jar到项目,如果项目中有spring或者spirngmvc,需要整合,首先配置freemarkerConfig,代码结构如下 <!-- 设置freeMarke ...
随机推荐
- 关于Ubuntu使用笔记
Ubuntu vm tools 安装 sudo apt install open-vm-tools-desktop 在安装程序时Ubuntu会将安装目录锁定,安装结束后会解除锁定,中断安装后无法再安装 ...
- Judy Beta Postmortem
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 与Alpha阶段相同,我们的软件要解决的Julia程序的dubug功能.问题定义得比较清楚,具体分 ...
- Appium简介及工作原理
一.什么是Appium Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS.Android及FirefoxOS平台.Appium使用WebDriver ...
- Windows下使用service.bat安装tomcat服务, 启动停止tomcat服务
在项目开发过程中,以前只是在Eclipse中配置.启动.停止tomcat服务器 如果只想在机器中使用tomcat服务器,而不想安装MyEclipse,可以使用service.bat 将tomcat安装 ...
- 幽灵自建的html5 的模板文件!
<!-- ,s555SB@@& :9H####@@@@@Xi 1@@@@@@@@@@@@@@8 ,8@@@@@@@@@B@@@@@@8 :B@@@@X3hi8Bs;B@@@@@Ah, , ...
- js事件、事件流以及target、currentTarget、this那些事
你是如此简单我却将你给遗忘 前面面试被问到js的事件机制 target.currentTarget.碰巧今天有时间来拔一拔,顺便记下.
- 新版ios证书的申请
现在IOS不再开放提供测试证书了,整理一个申请证书的流程. 1. 申请应用的id 链接地址 https://developer.apple.com/登陆开发者中心,在account界面点击红框里面得区 ...
- 2018-2019-2 学号20175223 实验二《Java面向对象程序设计》实验报告
目录 北京电子科技学院(BESTI)实验报告 实验名称:实验二 面向对象程序设计 实验内容.步骤与体会: 一.实验二 面向对象程序设计-1 二.实验二 面向对象程序设计-2 三.实验二 面向对象程序设 ...
- vue生命周期和钩子函数
new Vue 创建vue实例 init events & liftcycle 开始初始化 beforeCreate 组件刚被创建,组件属性计算之前,如data属性等 init injecti ...
- vue created中初始化属性
created:在模板渲染成html前调用,即通常初始化某些属性值,然后再渲染成视图. mounted:在模板渲染成html后调用,通常是初始化页面完成后,再对html的dom节点进行一些需要的操作