ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Freemarker的简单使用

2019-04-02 10:50:21  阅读:238  来源: 互联网

标签:student Freemarker 简单 studentList nbsp Student 使用 new public


FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。

主要用Freemarker做静态页面或是页面展示

 使用(依赖):

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

pojo;

public class Student {

	private int id;
	private String name;
	private int age;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Student(int id, String name, int age, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
	public Student(){}
}

student.flt模板1:

<html>
<head>
	<title>student</title>
</head>
<body>
	学生信息:<br>
	学号:${student.id}&nbsp;&nbsp;&nbsp;&nbsp;
	姓名:${student.name}&nbsp;&nbsp;&nbsp;&nbsp;
	年龄:${student.age}&nbsp;&nbsp;&nbsp;&nbsp;
	家庭住址:${student.address}<br>
	学生列表:
	<table border="1">
		<tr>
			<th>序号</th>
			<th>学号</th>
			<th>姓名</th>
			<th>年龄</th>
			<th>家庭住址</th>
		</tr>
		<#list studentList as stu>
		<#if stu_index % 2 == 0>
		<tr bgcolor="red">
		<#else>
		<tr bgcolor="green">
		</#if>
			<td>${stu_index}</td>
			<td>${stu.id}</td>
			<td>${stu.name}</td>
			<td>${stu.age}</td>
			<td>${stu.address}</td>
		</tr>
		</#list>
	</table>
	<br>
	<!-- 可以使用?date,?time,?datetime,?string(parten)-->
	当前日期1:${date?time}<br>
	当前日期2(如果是4月,不会显示04):${date?datetime}<br>
	当前日期3(自己的格式):${date?string("yyyy/MM/dd HH:mm:ss")}<br>
	val1值:${val1!"val1的值为null"}<br>
	val2值:${val2!"val2的值为null"}<br>
	val3值:${val3!"val3的值为null"}<br>
	(判断val1的值是否为null:)<br>
	<#if val1??>
	val1中有内容
	<#else>
	val1的值为null
	</#if><br>
		(判断val2的值是否为null:)<br>
	<#if val2??>
	val2中有内容
	<#else>
	val2的值为null
	</#if><br>
	引用模板:<br>
	<#include "hello.ftl">
</body>
</html>

hello.flt模板2:

${hello}

测试方法:

@Test
	public void testFreeMarker() throws Exception {
		//创建一个模板文件
		//创建一个Configuration对象
		Configuration configuration = new Configuration(Configuration.getVersion());
		//设置模板文件保存的目录
		configuration.setDirectoryForTemplateLoading(new File("E:/代码/jackson/freemarkertest/src/main/webapp/WEB-INF/ftl"));
		//模板文件的编码格式,一般就是utf-8
		configuration.setDefaultEncoding("utf-8");
		//加载一个模板文件,创建一个模板对象。
		Template template = configuration.getTemplate("student.ftl");
		//创建一个数据集。使用map
		Map data = new HashMap<>();
		data.put("hello", "FreemarkerTestHello");
		//创建一个student对象,测试pojo取值
		Student student = new Student(1, "jackson", 20, "天津市");
		data.put("student", student);
		//添加一个list,测试List取值
		List<Student> studentList = new ArrayList<>();
		studentList.add(new Student(1, "jackson1", 1, "天津市"));
		studentList.add(new Student(2, "jackson2", 2, "天津市"));
		studentList.add(new Student(3, "jackson3", 3, "天津市"));
		studentList.add(new Student(4, "jackson4", 4, "天津市"));
		studentList.add(new Student(5, "jackson5", 5, "天津市"));
		studentList.add(new Student(6, "jackson6", 6, "天津市"));
		studentList.add(new Student(7, "jackson7", 7, "天津市"));
		studentList.add(new Student(8, "jackson8", 8, "天津市"));
		studentList.add(new Student(9, "jackson9", 9, "天津市"));
		data.put("studentList", studentList);
		//添加日期类型,取时间
		data.put("date", new Date());
		//null值的测试
		data.put("val1", null);
		data.put("val2", "222");
		data.put("val3", "333");
		//创建一个Writer对象,指定输出文件的路径及文件名。
		Writer out = new FileWriter(new File("D:/freemarker/student.html"));
		//生成静态页面
		template.process(data, out);
		//关闭流
		out.close();
	}

效果:

标签:student,Freemarker,简单,studentList,nbsp,Student,使用,new,public
来源: https://blog.csdn.net/qq_41566772/article/details/88964750

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有