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

1、设计思路

(1)封装学生类

(2)创建数据模型

(3)新建student.ftl

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

2、封装学生类

Student.java:

  1. /**
  2. * @Title:Student.java
  3. * @Package:com.you.freemarker.model
  4. * @Description:学生类
  5. * @author:Youhaidong(游海东)
  6. * @date:2014-5-26 下午11:41:05
  7. * @version V1.0
  8. */
  9. package com.you.freemarker.model;
  10.  
  11. import java.io.Serializable;
  12. import java.util.Date;
  13.  
  14. /**
  15. * 类功能说明
  16. * 类修改者 修改日期
  17. * 修改说明
  18. * <p>Title:Student.java</p>
  19. * <p>Description:游海东个人开发</p>
  20. * <p>Copyright:Copyright(c)2013</p>
  21. * @author:游海东
  22. * @date:2014-5-26 下午11:41:05
  23. * @version V1.0
  24. */
  25. public class Student implements Serializable
  26. {
  27. /**
  28. * @Fields serialVersionUID:序列化
  29. */
  30. private static final long serialVersionUID = 1L;
  31.  
  32. /**
  33. * 学生姓名
  34. */
  35. private String studentName;
  36.  
  37. /**
  38. * 学生性别
  39. */
  40. private String studentSex;
  41.  
  42. /**
  43. * 学生年龄
  44. */
  45. private int studentAge;
  46.  
  47. /**
  48. * 学生生日
  49. */
  50. private Date studentBirthday;
  51.  
  52. /**
  53. * 学生地址
  54. */
  55. private String studentAddr;
  56.  
  57. /**
  58. * QQ
  59. */
  60. private long studentQQ;
  61.  
  62. /**
  63. * @return the studentName
  64. */
  65. public String getStudentName() {
  66. return studentName;
  67. }
  68.  
  69. /**
  70. * @param studentName the studentName to set
  71. */
  72. public void setStudentName(String studentName) {
  73. this.studentName = studentName;
  74. }
  75.  
  76. /**
  77. * @return the studentSex
  78. */
  79. public String getStudentSex() {
  80. return studentSex;
  81. }
  82.  
  83. /**
  84. * @param studentSex the studentSex to set
  85. */
  86. public void setStudentSex(String studentSex) {
  87. this.studentSex = studentSex;
  88. }
  89.  
  90. /**
  91. * @return the studentAge
  92. */
  93. public int getStudentAge() {
  94. return studentAge;
  95. }
  96.  
  97. /**
  98. * @param studentAge the studentAge to set
  99. */
  100. public void setStudentAge(int studentAge) {
  101. this.studentAge = studentAge;
  102. }
  103.  
  104. /**
  105. * @return the studentBirthday
  106. */
  107. public Date getStudentBirthday() {
  108. return studentBirthday;
  109. }
  110.  
  111. /**
  112. * @param studentBirthday the studentBirthday to set
  113. */
  114. public void setStudentBirthday(Date studentBirthday) {
  115. this.studentBirthday = studentBirthday;
  116. }
  117.  
  118. /**
  119. * @return the studentAddr
  120. */
  121. public String getStudentAddr() {
  122. return studentAddr;
  123. }
  124.  
  125. /**
  126. * @param studentAddr the studentAddr to set
  127. */
  128. public void setStudentAddr(String studentAddr) {
  129. this.studentAddr = studentAddr;
  130. }
  131.  
  132. /**
  133. * @return the studentQQ
  134. */
  135. public long getStudentQQ() {
  136. return studentQQ;
  137. }
  138.  
  139. /**
  140. * @param studentQQ the studentQQ to set
  141. */
  142. public void setStudentQQ(long studentQQ) {
  143. this.studentQQ = studentQQ;
  144. }
  145.  
  146. /**
  147. * <p>Title:</p>
  148. * <p>Description:无参构造函数</p>
  149. */
  150. public Student() {
  151. super();
  152. }
  153.  
  154. /**
  155. * <p>Title:</p>
  156. * <p>Description:有参构造函数</p>
  157. * @param studentName
  158. * @param studentSex
  159. * @param studentAge
  160. * @param studentBirthday
  161. * @param studentAddr
  162. * @param studentQQ
  163. */
  164. public Student(String studentName, String studentSex, int studentAge,
  165. Date studentBirthday, String studentAddr, long studentQQ) {
  166. super();
  167. this.studentName = studentName;
  168. this.studentSex = studentSex;
  169. this.studentAge = studentAge;
  170. this.studentBirthday = studentBirthday;
  171. this.studentAddr = studentAddr;
  172. this.studentQQ = studentQQ;
  173. }
  174.  
  175. }

3、创建数据模型

  1. Map<String,Object> root = null;
  2.  
  3. /**
  4. *
  5. * @Title:testStudent
  6. * @Description:
  7. * @param:
  8. * @return: void
  9. * @throws
  10. */
  11. @Test
  12. public void testStudent()
  13. {
  14. //创建数据模型
  15. root = new HashMap<String,Object>();
  16. root.put("student", new Student("张三丰","男",34,new Date(1988-12-12),"湖北省武汉市武昌洪山区",78451214));
  17. student("student.ftl");
  18. studentFile("student.ftl","student.html");
  19. }
  20.  
  21. /**
  22. *
  23. * @Title:student
  24. * @Description:
  25. * @param:@param name
  26. * @return: void
  27. * @throws
  28. */
  29. private void student(String name)
  30. {
  31. ft.printFtl(name,root);
  32. }
  33.  
  34. /**
  35. *
  36. * @Title:studentFile
  37. * @Description:
  38. * @param:@param name
  39. * @param:@param fileName
  40. * @return: void
  41. * @throws
  42. */
  43. private void studentFile(String name,String fileName)
  44. {
  45. ft.printFile(name, root, fileName);
  46. }

4、新建student.ftl

student.ftl:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>学生信息</title>
  5.  
  6. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7. <meta http-equiv="description" content="this is my page">
  8. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.  
  10. </head>
  11.  
  12. <body>
  13. 姓名:${student.studentName}
  14. 性别:${student.studentSex}
  15. 年龄:${student.studentAge}
  16. 生日:${(student.studentBirthday)?string("yyyy-MM-dd")}
  17. 地址:${student.studentAddr}
  18. QQ:${student.studentQQ}
  19. </body>
  20. </html>

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

6、控制台输出结果

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>学生信息</title>
  5.  
  6. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7. <meta http-equiv="description" content="this is my page">
  8. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.  
  10. </head>
  11.  
  12. <body>
  13. 姓名:张三丰
  14. 性别:男
  15. 年龄:34
  16. 生日:1970-01-01
  17. 地址:湖北省武汉市武昌洪山区
  18. QQ:78,451,214
  19. </body>
  20. </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. BZOJ 2342: [Shoi2011]双倍回文 [Manacher + set]

    题意: 求最长子串使得它有四个相同的回文串SSSS相连组成 枚举中间x 找右边的中间y满足 y-r[y]<=x y<=x+r[x]/2 用个set维护 注意中间只能是# #include ...

  2. Asp.Net Core MailKit 完美附件(中文名、长文件名)

    最近在使用MailKit组件发送邮件,看了一些博客其实还是蛮简单的,但是发送附件的时候却产生了不小的问题,附件的中文名字是乱码的,或者附件的名字过长就会无效,附件的名字在QQ邮箱中会变成类似 tcmi ...

  3. 定制化WinPE

    1 .首先挂载wim Dism /Mount-WIM /WimFile:D:\install.wim /Index: /MountDir:D:\wimmount 2. 如何要修改WinPE的启动项,可 ...

  4. mysql 修改默认字符集为utf8

    MySQL 5.5, all you need is: [mysqld] character_set_client=utf8 character_set_server=utf8 collation_s ...

  5. memcached 的实践操作

    memcached安装和使用   yum install -y libevent  memcached  libmemcached   启动命令:   /etc/init.d/memcached st ...

  6. 【echarts3】--1 简单入门

    echarts3 相信大家都了解吧,是百度研发的 ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8 ...

  7. pro asp.net mvc 5笔记

    1.Ninject条件绑定方法When(predicate)WhenClassHas<T>()WhenInj ectedInto<T>()例: kernel.Bind<I ...

  8. 用yii2给app写接口(上)

    Yii2如何实现RESTful风格的API 1.建立单独的应用程序 为了增加程序的可维护性,易操作性,我们选择新建一套应用程序,这也是为了和前台应用.后台应用区分开操作.有些人要嚷嚷了,为啥非得单独搞 ...

  9. PHP不使用array_merge函数实现一维数组合并

    function array_mer() { $arrays = func_get_args(); //获取当前函数中传递进来的所有参数,也就是所有一维数组 // echo '<pre>' ...

  10. 第二十一章 Django的分页与cookie

    第二十一章 Django的分页与cookie 第一课 模板 1.模板的继承 在Template目录下新建模板master.html <!DOCTYPE html> <html lan ...