java Freemarker页面静态化实例详解
Freemarker
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java等。
目前企业中:主要用Freemarker做静态页面或是页面展示
总结:freemarker模版引擎,可以使用Freemarker模版生成html页面。
Freemarker语法
/**
*freemark入门案例
*freemark三要素:
*1.freemarkAPI
*2.数据
*3.模板文件:ftl文件
*@throwsException
*/
@Test
publicvoidtest1()throwsException{
//创建freemarker核心配置对象,指定freemarker
Configurationcf=newConfiguration(Configuration.getVersion());
//指定服务器模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//指定模板文件编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下面获取模板文件对象
Templatetemplate=cf.getTemplate("hello.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap();
maps.put("hello","freemarker入门案例");
//创建一个输出对象,把数据输出daoHtml页面
Writerout=newFileWriter(newFile("F:\\template\\out\\quickstart.html"));
//生成Html页面
template.process(maps,out);
//关闭资源
out.close();
}
/**
*freemarker模板语法处理特殊数据格式
*例如:$0.2,20%
*模板语法:$0.2:${price?string.currency}
*20%:${price?string.percent}
*@throwsException
*/
@Test
publicvoidtest2()throwsException{
//创建freemark核心配置对象,指定freemark版本
Configurationcf=newConfiguration(Configuration.getVersion());
//指定模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//设置模板编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下面取模板文件对象
Templatetemplate=cf.getTemplate("num.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap<>();
maps.put("price",0.2);
//创建输出对象,把数据输出到Html页面
Writerout=newFileWriter(newFile("F:\\template\\out\\price.html"));
//生成Html页面
template.process(maps,out);
//关闭资源
out.close();
}
/**
*使用模板语法处理null值
*模板文件处理语法:
*1.?
*语法:${username?default("张三")}
*2.!
*语法:
*${username!}
*${username!"默认值"}
*3.if
*语法:
*<#ifusername??>
*${username}
*#if>
*@throwsException
*/
@Test
publicvoidtest3()throwsException{
//创建freemark核心配置对象,指定freemarker版本
Configurationcf=newConfiguration(Configuration.getVersion());
//指定模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//设置模板编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下获取模板文件对象
Templatetemplate=cf.getTemplate("null.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap<>();
maps.put("username",null);
//创建输出对象,把数据输出到html页面
Writerout=newFileWriter(newFile("F:\\template\\out\\username.html"));
//生成html页面
template.process(maps,out);
//关闭资源
out.close();
}
/**
*使用模板语法处理pojo数据
*el表达式获取数据:
*model.addAttribute("p",person);
*${p.username}
*${p.address}
*模板语法获取pojo数据
*model.addAttribute("p",person);
*${p.username}
*${p.address}
*@throwsException
*/
@Test
publicvoidtest4()throwsException{
//创建freemark核心配置对象,指定freemarker版本
Configurationcf=newConfiguration(Configuration.getVersion());
//指定模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//设置模板编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下获取模板文件对象
Templatetemplate=cf.getTemplate("person.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap<>();
//创建person对象
Personperson=newPerson();
person.setUsername("张三");
person.setAge(22);
maps.put("p",person);
//创建输出对象,把数据输出到html页面
Writerout=newFileWriter(newFile("F:\\template\\out\\person.html"));
//生成html页面
template.process(maps,out);
//关闭资源
out.close();
}
/**
*使用模板语法处理集合数据
*el表达式获取数据:
*model.addAttribute("pList",pList);
*
*${p.username}
*${p.age}
*
*模板语法获取list数据
*model.addAttribute("pList",pList);
*<#listpListasp>
*${p.username}
*${p.age}
*#list>
*角标语法:${别名_index}
*@throwsException
*/
@Test
publicvoidtest5()throwsException{
//创建freemark核心配置对象,指定freemarker版本
Configurationcf=newConfiguration(Configuration.getVersion());
//指定模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//设置模板编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下获取模板文件对象
Templatetemplate=cf.getTemplate("list.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap<>();
//创建list集合
ListpList=newArrayList<>();
//创建person对象
Personperson1=newPerson();
person1.setUsername("张三");
person1.setAge(22);
//创建person对象
Personperson2=newPerson();
person2.setUsername("李四");
person2.setAge(24);
pList.add(person1);
pList.add(person2);
maps.put("pList",pList);
//创建输出对象,把数据输出到html页面
Writerout=newFileWriter(newFile("F:\\template\\out\\list.html"));
//生成html页面
template.process(maps,out);
//关闭资源
out.close();
}
/**
*使用模板语法处理时间类型数据
*获取数据日期:${today?date}
*获取数据时间:${today?time}
*获取数据日期时间:${today?datetime}
*获取数据日期时间格式化:${today?string('yyyy-MM-dd')}
*@throwsException
*/
@Test
publicvoidtest6()throwsException{
//创建freemark核心配置对象,指定freemarker版本
Configurationcf=newConfiguration(Configuration.getVersion());
//指定模板文件所在路径
cf.setDirectoryForTemplateLoading(newFile("F:\\template"));
//设置模板编码
cf.setDefaultEncoding("utf-8");
//从模板文件路径下获取模板文件对象
Templatetemplate=cf.getTemplate("date.ftl");
//创建map对象,封装模板数据
Mapmaps=newHashMap<>();
maps.put("today",newDate());
//创建输出对象,把数据输出到html页面
Writerout=newFileWriter(newFile("F:\\template\\out\\date.html"));
//生成html页面
template.process(maps,out);
//关闭资源
out.close();
}
引入页面
将另一个页面引入本页面时可用以下命令完成
Jsp引入页面:
Ftl引入页面:<#include“/include/head.ftl”>
Freemarker整合spring
配置整合Freemarkerspring配置文件:
创建模版对象
Freemarker放入服务器:WEB-INF文件夹下面:访问资源文件,必须启动服务器。
在web.xml加载application的spring配置文件:
springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml,classpath:applicationContext-*.xml 1
Nginx访问
直接访问,加载不了样式资源,必须经过http服务器,才能加载静态资源。
此时nginx作为http服务器来访问静态资源。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!