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. Spring学习——从入门到精通

    本文章是博主原创,转载需注明出处. 第一篇先简单入个门--通过Spring创建对象 开发环境为Myeclipse2013,JDK版本为1.6,不要嫌它老,新知识都是在旧知识的基础上建立起来的,所谓基础 ...

  2. 多路复用select

    多路复用I/O:一个执行体监视多个文件描述符对象的状态是否改变,一旦改变通知其他执行体来实现. 基本思想: 1. 先构造一张有关描述符的表,然后调用一个函数,当这些文件描述符中的一个或者多个已准备好进 ...

  3. 读书共享 Primer Plus C-part 9

    第十二章 存储类.链接和内存管理                                                       针对代码块中的static变量做如下范本 #include ...

  4. 在CentOS 7中安装Jetty服务器

    Jetty 是一款纯Java的HTTP (Web) 服务器和Java Servlet容器. 通常在更大的网络框架中,Jetty经常用于设备间的通信,而其他Web服务器通常给"人类" ...

  5. MTU介绍以及在windows和linux下怎么设置MTU值

    最大传输单元MTU(Maximum Transmission Unit,MTU)是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位).最大传输单元这个参数通常与通信接口有关(网络接口卡 ...

  6. CENTOS6.6下mysql5.6的源码安装

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 1.下载:当前mysql版本到了5.6.28 http://dev. ...

  7. prop&attr区别和用法,以多选框为例

    1.比较 相同点 : prop和attr作为jquery的方法都可以获取属性值; 不同点 : (1) 对于HTML元素本身就带有的固有属性,使用prop方法, attr获取checkbox的check ...

  8. 利用Azure嵌套虚拟化,解决公有云上机器不能启动的问题

    很多时候我们都会碰到因为意外重启,机器硬盘被损坏导致无法启动,或者是因为各种原因Windows上的RDP服务启动不了,Linux上的SSH无法链接等等问题.碰到这种问题基本上很难解决以前都是将VHD下 ...

  9. 2018/3/2晚11点30分写的程序(C++)

    程序目标:输入一个字符串,竖向输出该字符串.使用string和动态分配内存机制.代码如下: #include<iostream>#include "stdafx.h"# ...

  10. wpf 如何让控件左右移动

    通过DoubleAnimation可以让控件进行左右移动. <Canvas x:Name="canvas_Shape" HorizontalAlignment="S ...