Java 模板引擎FreeMarker实战
2016-07-18 15:18
363 查看
FreeMarkerTest.java
UserInfo.java
src/test
src/test/java
src/test/resources
src/test/templates
src/test/templates/index.ftl
src/test/templates/macro.ftl
src/test/templates/useif.ftl
index.ftl
useif.ftl
macro.ftl
import com.google.common.collect.Lists; import com.google.common.collect.Maps; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import org.junit.Test; import java.io.File; import java.io.OutputStreamWriter; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Random; /** * Created by Lovell on 7/14/16. */ public class FreeMakerTest { /** * 基本加载流程 * @throws Exception */ @Test public void test1() throws Exception { // 创建freemarker配置实例 Configuration conf = new Configuration(Configuration.VERSION_2_3_22); // 创建数据模型 conf.setDirectoryForTemplateLoading(new File("src/test/templates")); conf.setDefaultEncoding("UTF-8"); Map<String, String> m = Maps.newHashMap(); m.put("name", "Lovell"); conf.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); // 加载模板文件 Template t = conf.getTemplate("index.ftl"); // 显示生成后的数据 OutputStreamWriter w = new OutputStreamWriter(System.out); t.process(m, w); w.close(); } /** * if的使用 * @throws Exception */ @Test public void test2() throws Exception { // 创建freemarker配置实例 Configuration conf = new Configuration(Configuration.VERSION_2_3_23); conf.setDirectoryForTemplateLoading(new File("src/test/templates")); // 创建数据模型 Map<String, Object> m = Maps.newHashMap(); m.put("score", new Random().nextInt(100)); UserInfo u1 = new UserInfo(); u1.setName("Lovell"); u1.setPassword("123456"); UserInfo u2 = new UserInfo(); u2.setName("柯南"); u2.setPassword("654321"); List<UserInfo> all = Lists.newArrayList(); all.add(u2); all.add(u1); m.put("userList", all); m.put("time", new Date()); m.put("str", "北京,上海,杭州"); // 加载模板文件f Template t = conf.getTemplate("useif.ftl"); // 显示生成后的数据 OutputStreamWriter w = new OutputStreamWriter(System.out); t.process(m, w); w.close(); } /** * 宏指令的使用 * @throws Exception */ @Test public void test3() throws Exception { //创建freemarker配置实例 Configuration conf = new Configuration(Configuration.VERSION_2_3_23); conf.setDirectoryForTemplateLoading(new File("src/test/templates")); //创建数据模型 Map<String, Object> m = Maps.newHashMap(); m.put("type","other"); m.put("num1",2); m.put("num2",3); //加载模板文件 Template t = conf.getTemplate("macro.ftl"); //显示生成后的数据 OutputStreamWriter w = new OutputStreamWriter(System.out); t.process(m,w); w.close(); } }
UserInfo.java
/** * Created by Lovell on 7/14/16. */ public class UserInfo { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
src/test
src/test/java
src/test/resources
src/test/templates
src/test/templates/index.ftl
src/test/templates/macro.ftl
src/test/templates/useif.ftl
index.ftl
hello,${name}
useif.ftl
--------------if语句的使用--------------- <#if score gte 60> 及格 <#elseif score gte 80&&score lte 90> 良好 <#else> 高材生 </#if> --------------空值判断、默认值------------ ${name!"未定义"} --------------判断值是否存在-------------- <#if name??> name存在 <#else> name不存在 </#if> -------------使用list遍历数据------------- <#list userList as user> <#if user_has_next> 最后一组: ${user.name}:{user.password} <#else>${user.name}:${user.password} </#if> </#list> -------------其他内建函数----------------- (1)日期格式化 ${time?string("yyyy-MM-dd")} (2)截取字符串 ${str?substring(0,2)} (3)indexof的使用 ${str?last_index_of(",")} (4)split的使用 <#list "12,13,14,15"?split(",") as item> ${item} </#list>
macro.ftl
----------------------宏指令的使用 m1可以看成是方法的名称,num1、num2为形参---------------------- <#macro m1 num1 num2> <#assign result=num1+num2> <h3>${result}</h3> </#macro> <@m1 5 6/> ----------------------宏指令(嵌入式)----------------- <#macro m2> <h3><#nested></h3> </#macro> <@m2>hello world</@m2>
相关文章推荐
- IDEA将web项目打成war包
- springMvc <form action="">提交跳转路径问题
- rxjava 操作符 just,from,scan,map
- SpringMVC 中的Interceptor拦截器
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- [JavaWeb]解决doGet()获取数据的乱码问题
- [JavaWeb]解决doGet()获取数据的乱码问题
- dos运行java类时遇到Could not find the main class问题
- Spring注解@Scope("prototype")
- SpringMVC结合ajaxfileupload.js实现异步上传文件
- Spring-----多环境中加载资源配置文件
- java学习之Dialog与FileDialog类
- 【奔跑的菜鸟】Java中的多态
- Java IO
- Eclipse中运行MapReduce程序控制台无调试信息-log4j
- java开发环境搭建
- Eclipse快捷键
- Maven(二)——eclipse使用Maven创建(Java和Android)项目
- 在JSP中使用JavaBean
- JavaBean