freemarker获取封装类中对象的属性
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获取封装类中对象的属性的更多相关文章
- freemarker获取封装类中对象的属性(六)
freemarker获取封装类中对象的属性 1.设计思路 (1)封装学生类 (2)创建数据模型 (3)新建student.ftl (4)运行Junit测试文件,生成HTML文件 2.封装学生类 Stu ...
- 利用KVC的方式更方便地获取数组中对象的属性的最值平均值等
直接上代码 输出结果也在相应的代码里标注出来了 //main.m文件 #import <Foundation/Foundation.h> #import "Student.h&q ...
- Java获取未知类型对象的属性
获取未知类型对象的属性通常有两种方式: 一是通过自定义注解的方式,通过获取被注解的属性从而获取属性的值,这种方式也是Spring参数注入的重要实现手段 二是通过反射获取属性的名称,通过属性名从而获取属 ...
- JavaScript中对象的属性
在JavaScript中,属性决定了一个对象的状态,本文详细的研究了它们是如何工作的. 属性类型 JavaScript中有三种不同类型的属性:命名数据属性(named data properties) ...
- java 对list中对象按属性排序
实体对象类 --略 排序类----实现Comparator接口,重写compare方法 package com.tang.list; import java.util.Comparator; publ ...
- springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象
1. 引入多个properties文件 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据, ...
- Vue 改变数组中对象的属性不重新渲染View的解决方案
Vue 改变数组中对象的属性不重新渲染View的解决方案 在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图.受到ja ...
- array排序(按数组中对象的属性进行排序)
使用array.sort()对数组中对象的属性进行排序 <template> <div> <a @click="sortArray()">降序& ...
- JS 取Json数据中对象特定属性值
解析JSON JSON 数据 var str = '[{"a": "1","b": "2"}, {"a&quo ...
随机推荐
- 基于tomcat+springMVC搭建基本的前后台交互系统
一.摘要 1.所需软件列表: 1) tomcat : apache-tomcat-7.0.54 服务端容器 2) Intellij: Intellij IDEA 14.0.3 开发 ...
- MongoDB Driver 简单的CURD
c#中我们可以使用MongoDB.Driver驱动进行对MongoDB数据库的增删改查. 首先需要在NuGet中安装驱动 安装完毕后会发现会有三个引用 其中 MongoDB.Driver和MongoD ...
- WPF 自定义图表(柱状图,曲线图)
1.功能 实现图表的数据绑定,动态绑定,属性更改绑定. 本文提供了一种思路. 2.原理 使用canvas绘制,使用反射来确定属性的绑定. 3.效果: 柱状图效果: 曲线图效果: 感谢阅读. 源码地址: ...
- spring boot + vue + element-ui全栈开发入门——windows开发环境
一.node.js开发环境 windows系统,去网站https://nodejs.org/en/download/,下载对应的安装程序,并安装Windows Installer (.msi) 接下 ...
- Python基础篇(三)
元组是序列的一种,与列表的区别是,元组是不能修改的. 元组一般是用圆括号括起来进行定义,如下: >>> (1,2,3)[1:2] (2,) 如果元组中只有一个元素,元组的表示 ...
- Python基础篇(二)
Python最基本的数据结构是序列(sequence),序列中的每个元素被分以以0开头的唯一的一个id号. Python中有6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和 ...
- 四、正则表达式re模块
什么是正则表达式 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一 ...
- 自兴人工智能-------------Python入门基础(1)
Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构, 并且能够用简单而又高效的方式进行面向对象编程. Python 优雅的语法和动态 类型,再结合它的解释性,使其在大多数平台 ...
- js 前端图片压缩+ios图片角度旋转
step1:读取选择的图片,并转为base64: function ImgToBase64 (e, fn) { // 图片方向角 //fn为传入的方法函数,在图片操作完成之后执行 var Orient ...
- Properties工具类
/** * 加载Properties文件 * @param path Properties文件路径 * @return */ private static Properties getClasspat ...