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 ...
随机推荐
- window.opener和window.open
window.open (URL,name,specs,replace)方法用于打开一个新的浏览器窗口或查找一个已命名的窗口. URL:可选.打开指定的页面的URL.如果没有指定URL,打开一个新的空 ...
- awk使用学习
awk使用中常用的几个方法: 一.在某一列中查询符合条件的值,并返回该行数据 [root@localhost bus_route]# cat 123.log one two three four1 2 ...
- Vscode下的Markdown的基本使用
1.Vscode默认支持Markdown语法,只需要安装相应的扩展插件,Markdown Preview enhanced. 2.安装完插件后,在Vscode上新建一个文件,然后将文件的语言模式设置为 ...
- layui在open弹出层回显,解决动态select数据回显问题
//监听数据表格工具条 table.on('tool(contentList)', function(obj){ //注:tool是工具条事件名,test是table原始容器的属性 l ...
- Linux常用shell命令
1.>>>>>刚安装好的ubuntu需要为root创建密码[解决]passwd root 2.>>>>>安装完ubuntu后需要更新[解决] ...
- Linux 下编程
关于Linux 下的C语言编译命令和编程要点! https://www.cnblogs.com/wfwenchao/p/3985153.html?utm_source=tuicool&utm_ ...
- python识别图片中的代码。
在看并发编程网的时候,有些示例代码是以图片的形式出现的,要是此时自己想复制下来的话,只能对着图片敲了,很不爽,于是搜了一下识别图片的网站,有! 把图片上传上去解析,下来txt文本,打开一看,大部分能解 ...
- Sql Server 获取存储过程或函数创建语句
通过该语句可以获取到sqlserver 所有的函数名或者存储过程名 SELECT name FROM sysobjects WHERE xtype='P'; 通过该语句可查询出函数或者存储过的的创建语 ...
- Python之字典方法
def clear(self): # 清除所有内容 """ D.clear() -> None. Remove all items from D. "&q ...
- Vmware网络不可达
1. ifconfig -a 查看当前的网卡 2. cd /etc/sysconfig/network-scripts/ 3. 打开对应网卡名称文件, 具体修改内容参考(https://www. ...