标签:序号 int 工具 except demo 结果 ons tle pos
使用步骤:
加载模板
创建数据
产生输出
FreemarkerDemo1.java
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo1.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo2.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
语法:
${属性} ${name}
${student.name}
${属性 ! 默认值} ${name ! "不存在该属性"}
${属性 ?String()} ${salary ? String("0.00")}
Student.java
package entity;
public class Student {
private String sno;
private String name;
private Integer age;
public Student(String sno, String name, Integer age) {
this.sno = sno;
this.name = name;
this.age = age;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
FreemarkerDemo2.java
package freemarker;
import entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class FreemarkerDemo2 {
public static void main(String[] args) throws IOException, TemplateException {
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo2.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo2.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
}
}
demo2.ftl
<#list students as s>
序号:${s_index+1}
学号:${s.sno}
姓名:${s.name}
年龄:${s.age}
-------------------------------
</#list>
FreemarkerDemo3.java
package freemarker;
import entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
public class FreemarkerDemo3 {
public static void main(String[] args) throws IOException, TemplateException {
//创建核心配置对象
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
//设置加载目录
configuration.setClassForTemplateLoading(FreemarkerDemo3.class,"");
//得到模板对象
Template t = configuration.getTemplate("demo3.ftl");
//创建数据
Map<String,Object> data = new HashMap<String,Object>();
List<Student> students=new ArrayList<>();
students.add(new Student("1001","Jack",18));
students.add(new Student("1002","Lucy",17));
students.add(new Student("1003","Tom",19));
students.add(new Student("1004","Tim",20));
students.add(new Student("1005","Marry",18));
data.put("students",students);
Map studentMap=new LinkedHashMap();
for(Student s:students){
studentMap.put(s.getSno(),s);
}
data.put("studentMap",studentMap);
//产生输出
t.process(data, new OutputStreamWriter(System.out));
}
}
Demo03.ftl
<#list studentMap?keys as k>
${k}-${studentMap[k].name}
</#list>
参考手册:http://freemarker.foofun.cn/
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>
<!--映射路径--><!--所有扩展名为ftl文件被freemarker加载-->
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
</web-app>
package Freemarker;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/fre_servlet")
public class FreemarkerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("----------------------------");
List<Map> students= new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> student = new HashMap<>();
student.put("sno", "S" + i);
student.put("name", "Jack" + i);
students.add(student);
}
request.setAttribute("students", students);
request.getRequestDispatcher("student.ftl").forward(request, response);
}
}
<#list students as student>
学号:${student.sno}-----姓名:${student.name}<br>
</#list>
标签:序号 int 工具 except demo 结果 ons tle pos
原文地址:https://www.cnblogs.com/jascen/p/11305715.html