freemarker获取封装类中对象的属性

1、设计思路

(1)封装学生类

(2)创建数据模型

(3)新建student.ftl

(4)运行Junit测试文件,生成HTML文件

2、封装学生类

Student.java:

/**
 * @Title:Student.java
 * @Package:com.you.freemarker.model
 * @Description:学生类
 * @author:Youhaidong(游海东)
 * @date:2014-5-26 下午11:41:05
 * @version V1.0
 */
package com.you.freemarker.model;

import java.io.Serializable;
import java.util.Date;

/**
 * 类功能说明
 * 类修改者 修改日期
 * 修改说明
 * <p>Title:Student.java</p>
 * <p>Description:游海东个人开发</p>
 * <p>Copyright:Copyright(c)2013</p>
 * @author:游海东
 * @date:2014-5-26 下午11:41:05
 * @version V1.0
 */
public class Student implements Serializable
{
	/**
	 * @Fields  serialVersionUID:序列化
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 学生姓名
	 */
	private String studentName;

	/**
	 * 学生性别
	 */
	private String studentSex;

	/**
	 * 学生年龄
	 */
	private int studentAge;

	/**
	 * 学生生日
	 */
	private Date studentBirthday;

	/**
	 * 学生地址
	 */
	private String studentAddr;

	/**
	 * QQ
	 */
	private long studentQQ;

	/**
	 * @return the studentName
	 */
	public String getStudentName() {
		return studentName;
	}

	/**
	 * @param studentName the studentName to set
	 */
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	/**
	 * @return the studentSex
	 */
	public String getStudentSex() {
		return studentSex;
	}

	/**
	 * @param studentSex the studentSex to set
	 */
	public void setStudentSex(String studentSex) {
		this.studentSex = studentSex;
	}

	/**
	 * @return the studentAge
	 */
	public int getStudentAge() {
		return studentAge;
	}

	/**
	 * @param studentAge the studentAge to set
	 */
	public void setStudentAge(int studentAge) {
		this.studentAge = studentAge;
	}

	/**
	 * @return the studentBirthday
	 */
	public Date getStudentBirthday() {
		return studentBirthday;
	}

	/**
	 * @param studentBirthday the studentBirthday to set
	 */
	public void setStudentBirthday(Date studentBirthday) {
		this.studentBirthday = studentBirthday;
	}

	/**
	 * @return the studentAddr
	 */
	public String getStudentAddr() {
		return studentAddr;
	}

	/**
	 * @param studentAddr the studentAddr to set
	 */
	public void setStudentAddr(String studentAddr) {
		this.studentAddr = studentAddr;
	}

	/**
	 * @return the studentQQ
	 */
	public long getStudentQQ() {
		return studentQQ;
	}

	/**
	 * @param studentQQ the studentQQ to set
	 */
	public void setStudentQQ(long studentQQ) {
		this.studentQQ = studentQQ;
	}

	/**
	 * <p>Title:</p>
	 * <p>Description:无参构造函数</p>
	 */
	public Student() {
		super();
	}

	/**
	 * <p>Title:</p>
	 * <p>Description:有参构造函数</p>
	 * @param studentName
	 * @param studentSex
	 * @param studentAge
	 * @param studentBirthday
	 * @param studentAddr
	 * @param studentQQ
	 */
	public Student(String studentName, String studentSex, int studentAge,
			Date studentBirthday, String studentAddr, long studentQQ) {
		super();
		this.studentName = studentName;
		this.studentSex = studentSex;
		this.studentAge = studentAge;
		this.studentBirthday = studentBirthday;
		this.studentAddr = studentAddr;
		this.studentQQ = studentQQ;
	}

}

3、创建数据模型

Map<String,Object> root = null;

	/**
	 *
	 * @Title:testStudent
	 * @Description:
	 * @param:
	 * @return: void
	 * @throws
	 */
	@Test
	public void testStudent()
	{
		//创建数据模型
		root = new HashMap<String,Object>();
		root.put("student", new Student("张三丰","男",34,new Date(1988-12-12),"湖北省武汉市武昌洪山区",78451214));
		student("student.ftl");
		studentFile("student.ftl","student.html");
	}

	/**
	 *
	 * @Title:student
	 * @Description:
	 * @param:@param name
	 * @return: void
	 * @throws
	 */
	private void student(String name)
	{
		ft.printFtl(name,root);
	}

	/**
	 *
	 * @Title:studentFile
	 * @Description:
	 * @param:@param name
	 * @param:@param fileName
	 * @return: void
	 * @throws
	 */
	private void studentFile(String name,String fileName)
	{
		ft.printFile(name, root, fileName);
	}

4、新建student.ftl

student.ftl:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>学生信息</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  </head>

  <body>
    	姓名:${student.studentName}
     	性别:${student.studentSex}
     	年龄:${student.studentAge}
     	生日:${(student.studentBirthday)?string("yyyy-MM-dd")}
    	地址:${student.studentAddr}
  		QQ:${student.studentQQ}
  </body>
</html>

5、运行Junit测试文件,生成HTML文件





6、控制台输出结果

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>学生信息</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  </head>

  <body>
    	姓名:张三丰
     	性别:男
     	年龄:34
     	生日:1970-01-01
    	地址:湖北省武汉市武昌洪山区
  		QQ:78,451,214
  </body>
</html>

freemarker获取封装类中对象的属性的更多相关文章

  1. freemarker获取封装类中对象的属性(六)

    freemarker获取封装类中对象的属性 1.设计思路 (1)封装学生类 (2)创建数据模型 (3)新建student.ftl (4)运行Junit测试文件,生成HTML文件 2.封装学生类 Stu ...

  2. 利用KVC的方式更方便地获取数组中对象的属性的最值平均值等

    直接上代码 输出结果也在相应的代码里标注出来了 //main.m文件 #import <Foundation/Foundation.h> #import "Student.h&q ...

  3. Java获取未知类型对象的属性

    获取未知类型对象的属性通常有两种方式: 一是通过自定义注解的方式,通过获取被注解的属性从而获取属性的值,这种方式也是Spring参数注入的重要实现手段 二是通过反射获取属性的名称,通过属性名从而获取属 ...

  4. JavaScript中对象的属性

    在JavaScript中,属性决定了一个对象的状态,本文详细的研究了它们是如何工作的. 属性类型 JavaScript中有三种不同类型的属性:命名数据属性(named data properties) ...

  5. java 对list中对象按属性排序

    实体对象类 --略 排序类----实现Comparator接口,重写compare方法 package com.tang.list; import java.util.Comparator; publ ...

  6. springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象

    1.    引入多个properties文件 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据, ...

  7. Vue 改变数组中对象的属性不重新渲染View的解决方案

    Vue 改变数组中对象的属性不重新渲染View的解决方案 在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图.受到ja ...

  8. array排序(按数组中对象的属性进行排序)

    使用array.sort()对数组中对象的属性进行排序 <template> <div> <a @click="sortArray()">降序& ...

  9. JS 取Json数据中对象特定属性值

    解析JSON JSON 数据 var str = '[{"a": "1","b": "2"}, {"a&quo ...

随机推荐

  1. php操作mongodb的常用函数

    连接mongodb: $mongoObj = new Mongo("127.0.0.1" , array( 'connect'=>true, 'persist'=>tr ...

  2. 洛谷 [P1801] 黑匣子

    这道题是一道splay裸题,然而身为蒟蒻的我并不会,所以这道题我维护的是一个大根堆与一个小根堆结合起来的类似沙漏的结构. 本题难点在于询问的不是最大最小值,而是第K小值,所以我们想到了维护这样两个堆, ...

  3. bzoj 2095: [Poi2010]Bridges [混合图欧拉回路]

    2095: [Poi2010]Bridges 二分答案,混合图欧拉路判定 一开始想了一个上下界网络流模型,然后发现不用上下界网络流也可以 对于无向边,强制从\(u \rightarrow v\),计算 ...

  4. SqlServer varchar数据中类似于1.1.1.1这种值的排序方法

    select * from 表名order by Convert(int,left(列名,charindex('.',列名+'.')-1)) asc, 列名asc charindex('.',列名)  ...

  5. 使用xUnit为.net core程序进行单元测试(4)

    第1部分: http://www.cnblogs.com/cgzl/p/8283610.html 第2部分: http://www.cnblogs.com/cgzl/p/8287588.html 第3 ...

  6. Linux压缩解压命令汇总

    01-.tar格式 解包:[*******]$ tar xvf FileName.tar 打包:[*******]$ tar cvf FileName.tar DirName (注:tar是打包,不是 ...

  7. Windows Azure Platform Introduction (14) 申请海外的Windows Azure账户

    <Windows Azure Platform 系列文章目录> 本文的最后更新时间为:2017-12-27 本文介绍国内用户,注册和使用海外Azure账户. 前提: 1.需要一个有效的Wi ...

  8. JS原型学习之旅(一)之一图了解原型链关系

    目前正在学JS的原型思想(准确的说是从昨天2018.1.29开始正式接触),琢磨了两天,在chrome的console不停的敲了好多代码测试__proto__和prototype的关系,有了些小收获( ...

  9. JSON工具类

    import java.sql.Timestamp; import java.util.Collection; import java.util.Date; import org.soul.util. ...

  10. 企业级docker仓库Harbor部署

    1.安装环境下载离线安装包地址https://github.com/vmware/harbor/releases/yum install -y dockerpip install -i https:/ ...