1 package freemarker; 2 3 import java.io.IOException; 4 import java.io.OutputStreamWriter; 5 import java.util.Date; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 //import freemarker.entity.Computer; 10 11 import freemarker.core.ParseException; 12 import freemarker.entity.Computer; 13 import freemarker.template.Configuration; 14 import freemarker.template.MalformedTemplateNameException; 15 import freemarker.template.Template; 16 import freemarker.template.TemplateException; 17 import freemarker.template.TemplateNotFoundException; 18 19 public class FreemarkerSample1 { 20 21 public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException { 22 //1. 加载模板 23 //创建核心配置对象 24 Configuration config = new Configuration(Configuration.VERSION_2_3_29); 25 //设置加载的目录 26 config.setClassForTemplateLoading(FreemarkerSample1.class, ""); 27 //得到模板对象 28 Template t = config.getTemplate("sample1.ftl"); 29 //2. 创建数据 30 Map<String,Object> data = new HashMap<String,Object>(); 31 data.put("site", "新浪"); 32 data.put("url", "http://www.sina.com"); 33 data.put("date", new Date()); 34 data.put("number", 837183.883217); 35 Map info = new HashMap(); 36 info.put("cpu", "i5"); 37 Computer c1 = new Computer("1234567" , "ThinkPad" , 1 , "李四" , new Date() , 12900f , info); 38 data.put("computer", c1); 39 //3. 产生输出 40 t.process(data, new OutputStreamWriter(System.out)); 41 } 42 }
<#-- Freemarker取值 --> ${site} ${url} <#-- !默认值 --> ${author!"不存在的属性"} <#-- ?string格式化输出 --> ${date?string("yyyy年MM月dd日 HH:mm:ss SSS")} ${number?string("0.00")} <#if computer.sn == "1234567">
<#if computer.sn == "1234567"> 重要设备 </#if> SN:${computer.sn} 型号:${computer.model} <#if computer.state == 1> 状态:正在使用 <#elseif computer.state == 2> 状态:闲置 <#elseif computer.state == 3> 状态:已作废 </#if>
<#switch computer.state> <#case 1> 状态:正在使用 <#break> <#case 2> 状态:闲置 <#break> <#case 3> 状态:已作废 <#break> <#default> 状态:无效状态 </#switch> <#-- ??代表判断对象是否为空,true不为空,false为空 --> <#if computer.user??> 用户:${computer.user} </#if>
package freemarker; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; //import freemarker.entity.Computer; import freemarker.core.ParseException; import freemarker.entity.Computer; import freemarker.template.Configuration; import freemarker.template.MalformedTemplateNameException; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateNotFoundException; public class FreemarkerSample2 { public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException { //1. 加载模板 //创建核心配置对象 Configuration config = new Configuration(Configuration.VERSION_2_3_29); //设置加载的目录 config.setClassForTemplateLoading(FreemarkerSample2.class, ""); //得到模板对象 Template t = config.getTemplate("sample2.ftl"); //2. 创建数据 Map<String,Object> data = new HashMap<String,Object>(); List<Computer> computers = new ArrayList(); computers.add(new Computer("1234567" , "ThinkPad X1" , 2 , null , new Date() , 12999f , new HashMap() )); computers.add(new Computer("1234568" , "HP XXX" , 1 , "张三" , new Date() , 7500f , new HashMap() )); computers.add(new Computer("1234569" , "DELL XXX" , 3 , "李四" , new Date() , 8500f , new HashMap() )); computers.add(new Computer("1234570" , "ACER XXX" , 1 , "王五" , new Date() , 6300f , new HashMap() )); computers.add(new Computer("1234571" , "MSI XXX" , 1 , "赵六" , new Date() , 9300f , new HashMap() )); data.put("computers", computers); //LinkedHashMap可以保证数据按存放顺序进行提取 Map computerMap = new LinkedHashMap(); for(Computer c : computers) { computerMap.put(c.getSn(), c); } data.put("computer_map", computerMap); //3. 产生输出 t.process(data, new OutputStreamWriter(System.out)); } }
<#list computers as c> 序号:${c_index + 1} <#-- 迭代变量_index保存了循环的索引,从0开始 --> SN:${c.sn} 型号:${c.model} <#switch c.state> <#case 1> 状态:使用中 <#break> <#case 2> 状态:闲置 <#break> <#case 3> 状态:已作废 <#break> </#switch> <#if c.user??> 用户:${c.user} </#if> 采购日期:${c.dop?string("yyyy-MM-dd")} 采购价格:${c.price?string("0.00")} ------------------------------------------- </#list>
<#list computer_map?keys as k > ${k}-${computer_map[k].model} ${computer_map[k].price?string("0.00")} </#list>
1 package freemarker; 2 3 import java.io.IOException; 4 import java.io.OutputStreamWriter; 5 import java.util.ArrayList; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.LinkedHashMap; 9 import java.util.List; 10 import java.util.Map; 11 12 //import freemarker.entity.Computer; 13 14 import freemarker.core.ParseException; 15 import freemarker.entity.Computer; 16 import freemarker.template.Configuration; 17 import freemarker.template.MalformedTemplateNameException; 18 import freemarker.template.Template; 19 import freemarker.template.TemplateException; 20 import freemarker.template.TemplateNotFoundException; 21 22 public class FreemarkerSample3 { 23 24 public static void main(String[] args) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException { 25 //1. 加载模板 26 //创建核心配置对象 27 Configuration config = new Configuration(Configuration.VERSION_2_3_29); 28 //设置加载的目录 29 config.setClassForTemplateLoading(FreemarkerSample3.class, ""); 30 //得到模板对象 31 Template t = config.getTemplate("sample3.ftl"); 32 //2. 创建数据 33 Map<String,Object> data = new HashMap<String,Object>(); 34 data.put("name", "jackson"); 35 data.put("brand", "bmw"); 36 data.put("words", "first blood"); 37 data.put("n", 37981.83); 38 data.put("date", new Date()); 39 List<Computer> computers = new ArrayList(); 40 computers.add(new Computer("1234567" , "ThinkPad X1" , 2 , null , new Date() , 12999f , new HashMap() )); 41 computers.add(new Computer("1234568" , "HP XXX" , 1 , "张三" , new Date() , 7500f , new HashMap() )); 42 computers.add(new Computer("1234569" , "DELL XXX" , 3 , "李四" , new Date() , 8500f , new HashMap() )); 43 computers.add(new Computer("1234570" , "ACER XXX" , 1 , "王五" , new Date() , 6300f , new HashMap() )); 44 computers.add(new Computer("1234571" , "MSI XXX" , 1 , "赵六" , new Date() , 9300f , new HashMap() )); 45 data.put("computers", computers); 46 47 //3. 产生输出 48 t.process(data, new OutputStreamWriter(System.out)); 49 } 50 }
${name?cap_first} ${brand?upper_case} ${brand?length} ${words?replace("blood" , "*****")} ${words?index_of("blood")} <#-- 利用?string实现三目运算符的操作 --> ${(words?index_of("blood") != -1)?string("包含敏感词汇","不包含敏感词汇")} ${n?round} ${n?floor} ${n?ceiling} 公司共有${computers?size}台电脑 第一台:${computers?first.model} 最后一台:${computers?last.model} <#list computers?sort_by("price")?reverse as c> ${c.sn}-${c.price} </#list>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>fm-web</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>freemarker</servlet-name> <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class> <init-param> <param-name>TemplatePath</param-name> <param-value>/WEB-INF/ftl</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> </web-app>
1 package freemarker; 2 3 public class Employee { 4 private int empno; 5 private String ename; 6 private String department; 7 private String job; 8 private Float salary; 9 10 public Employee(int empno, String ename, String department, String job, Float salary) { 11 super(); 12 this.empno = empno; 13 this.ename = ename; 14 this.department = department; 15 this.job = job; 16 this.salary = salary; 17 } 18 public Integer getEmpno() { 19 return empno; 20 } 21 public void setEmpno(Integer empno) { 22 this.empno = empno; 23 } 24 public String getEname() { 25 return ename; 26 } 27 public void setEname(String ename) { 28 this.ename = ename; 29 } 30 public String getDepartment() { 31 return department; 32 } 33 public void setDepartment(String department) { 34 this.department = department; 35 } 36 public String getJob() { 37 return job; 38 } 39 public void setJob(String job) { 40 this.job = job; 41 } 42 public Float getSalary() { 43 return salary; 44 } 45 public void setSalary(Float salary) { 46 this.salary = salary; 47 } 48 }
1 package freemarker; 2 3 import java.io.IOException; 4 import java.util.*; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 13 /** 14 * Servlet implementation class ListServlet 15 */ 16 @WebServlet("/list") 17 public class ListServlet extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public ListServlet() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 30 */ 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 List list=new ArrayList(); 33 list.add(new Employee(7731,"张三","市场部","客户代表",8000f)); 34 list.add(new Employee(8871,"李四","研发部","运维工程师",7000f)); 35 request.setAttribute("employee_list", list); 36 request.getRequestDispatcher("/employee.ftl").forward(request, response); 37 } 38 39 }
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width,initial-scale=1"> 6 <title>员工列表</title> 7 <link href="css/bootstrap.css" type="text/css" rel="stylesheet"></link> 8 9 <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script> 10 <script type="text/javascript" src="js/bootstrap.js"></script> 11 12 <style type="text/css"> 13 .pagination { 14 margin: 0px 15 } 16 17 .pagination > li > a, .pagination > li > span { 18 margin: 0 5px; 19 border: 1px solid #dddddd; 20 } 21 22 .glyphicon { 23 margin-right: 3px; 24 } 25 26 .form-control[readonly] { 27 cursor: pointer; 28 background-color: white; 29 } 30 #dlgPhoto .modal-body{ 31 text-align: center; 32 } 33 .preview{ 34 35 max-width: 500px; 36 } 37 </style> 38 </head> 39 <body> 40 41 <div class="container"> 42 <div class="row"> 43 <h1 style="text-align: center">IMOOC员工信息表</h1> 44 <div class="panel panel-default"> 45 <div class="clearfix panel-heading "> 46 <div class="input-group" style="width: 500px;"> 47 48 </div> 49 </div> 50 51 <table class="table table-bordered table-hover"> 52 <thead> 53 <tr> 54 <th>序号</th> 55 <th>员工编号</th> 56 <th>姓名</th> 57 <th>部门</th> 58 <th>职务</th> 59 <th>工资</th> 60 <th> </th> 61 </tr> 62 </thead> 63 <tbody> 64 <#list employee_list as emp> 65 <tr> 66 <td>${emp_index+1}</td> 67 <td>${emp.empno?string("0")}</td> 68 <td>${emp.ename}</td> 69 <td>${emp.department}</td> 70 <td>${emp.job}</td> 71 <td style="color: red;font-weight: bold">¥${emp.salary?string("0.00")}</td> 72 </tr> 73 </#list> 74 </tbody> 75 </table> 76 </div> 77 </div> 78 </div> 79 80 </body> 81 </html>
效果显示:
原文:https://www.cnblogs.com/qqfff/p/12359279.html