freemarker
freemarker 是一个用 java 语言编写的模板引擎,它基于模板来生成文本输出。freemarker与 web 容器无关,即在 web 运行时,它并不知道 servlet 或 http。它不仅可以用作表现层的实现技术,而且还可以用于生成 xml,jsp 或 java 等。
目前企业中:主要用 freemarker 做静态页面或是页面展示
总结:freemarker 模版引擎,可以使用 freemarker 模版生成 html 页面。
freemarker 语法
/**
* freemark入门案例
* freemark三要素:
* 1.freemark api
* 2.数据
* 3.模板文件:ftl文件
* @throws exception
*/
@test
public void test1() throws exception{
//创建freemarker核心配置对象,指定freemarker
configuration cf = new configuration(configuration.getversion());
//指定服务器模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//指定模板文件编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下面获取模板文件对象
template template = cf.gettemplate("hello.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<string,object>();
maps.put("hello", "freemarker入门案例");
//创建一个输出对象,把数据输出daohtml页面
writer out = new filewriter(new file("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}
* @throws exception
*/
@test
public void test2() throws exception{
//创建freemark核心配置对象,指定freemark版本
configuration cf = new configuration(configuration.getversion());
//指定模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//设置模板编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下面取模板文件对象
template template = cf.gettemplate("num.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<>();
maps.put("price", 0.2);
//创建输出对象,把数据输出到html页面
writer out = new filewriter(new file("f:\template\out\price.html"));
//生成html页面
template.process(maps, out);
//关闭资源
out.close();
}
/**
* 使用模板语法处理null值
* 模板文件处理语法:
* 1.?
* 语法:${username?default("张三")}
* 2.!
* 语法:
* ${username!}
* ${username!"默认值"}
* 3.if
* 语法:
* <#if username??>
* ${username}
* </#if>
* @throws exception
*/
@test
public void test3() throws exception{
//创建freemark核心配置对象,指定freemarker版本
configuration cf = new configuration(configuration.getversion());
//指定模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//设置模板编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下获取模板文件对象
template template = cf.gettemplate("null.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<>();
maps.put("username", null);
//创建输出对象,把数据输出到html页面
writer out = new filewriter(new file("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}
* @throws exception
*/
@test
public void test4() throws exception{
//创建freemark核心配置对象,指定freemarker版本
configuration cf = new configuration(configuration.getversion());
//指定模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//设置模板编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下获取模板文件对象
template template = cf.gettemplate("person.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<>();
//创建person对象
person person = new person();
person.setusername("张三");
person.setage(22);
maps.put("p", person);
//创建输出对象,把数据输出到html页面
writer out = new filewriter(new file("f:\template\out\person.html"));
//生成html页面
template.process(maps, out);
//关闭资源
out.close();
}
/**
* 使用模板语法处理集合数据
* el表达式获取数据:
* model.addattribute("plist",plist);
* <c:foreach item="plist" var="p">
* ${p.username}
* ${p.age}
* </c:foreach>
* 模板语法获取list数据
* model.addattribute("plist",plist);
* <#list plist as p>
* ${p.username}
* ${p.age}
* </#list>
* 角标语法:${别名_index}
* @throws exception
*/
@test
public void test5() throws exception{
//创建freemark核心配置对象,指定freemarker版本
configuration cf = new configuration(configuration.getversion());
//指定模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//设置模板编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下获取模板文件对象
template template = cf.gettemplate("list.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<>();
//创建list集合
list<person> plist = new arraylist<>();
//创建person对象
person person1 = new person();
person1.setusername("张三");
person1.setage(22);
//创建person对象
person person2 = new person();
person2.setusername("李四");
person2.setage(24);
plist.add(person1);
plist.add(person2);
maps.put("plist", plist);
//创建输出对象,把数据输出到html页面
writer out = new filewriter(new file("f:\template\out\list.html"));
//生成html页面
template.process(maps, out);
//关闭资源
out.close();
}
/**
* 使用模板语法处理时间类型数据
* 获取数据日期:${today?date}
* 获取数据时间:${today?time}
* 获取数据日期时间:${today?datetime}
* 获取数据日期时间格式化:${today?string('yyyy-mm-dd')}
* @throws exception
*/
@test
public void test6() throws exception{
//创建freemark核心配置对象,指定freemarker版本
configuration cf = new configuration(configuration.getversion());
//指定模板文件所在路径
cf.setdirectoryfortemplateloading(new file("f:\template"));
//设置模板编码
cf.setdefaultencoding("utf-8");
//从模板文件路径下获取模板文件对象
template template = cf.gettemplate("date.ftl");
//创建map对象,封装模板数据
map<string,object> maps = new hashmap<>();
maps.put("today", new date());
//创建输出对象,把数据输出到html页面
writer out = new filewriter(new file("f:\template\out\date.html"));
//生成html页面
template.process(maps, out);
//关闭资源
out.close();
}
引入页面
将另一个页面引入本页面时可用以下命令完成
jsp 引入页面:
ftl 引入页面:<#include “/include/head.ftl”>
freemarker 整合 spring
配置整合 freemarker spring 配置文件:
<!-- freemarker交给spring管理 --> <!-- 使用spring提供模板管理配置对象 --> <bean class="org.springframework.web.servlet.view.freemarker.freemarkerconfigurer"> <!-- 模板路径 --> <property name="templateloaderpath" value="/web-inf/fm/" /> <!-- 模板编码 --> <property name="defaultencoding" value="utf-8" /> </bean>
创建模版对象
freemarker 放入服务器:web-inf 文件夹下面:访问资源文件,必须启动服务器。
在 web.xml 加载 application 的 spring 配置文件:
<!-- 加载springmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc.xml,classpath:applicationcontext-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
nginx访问
直接访问,加载不了样式资源,必须经过 http 服务器,才能加载静态资源。
此时 nginx 作为 http 服务器来访问静态资源。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!