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

Posted odejsjhshw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>


 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow









以上是关于freemarker获取封装类中对象的属性的主要内容,如果未能解决你的问题,请参考以下文章

java自学-面向对象三大特性

简述Java三大特性

python之路之前没搞明白4面向对象(封装)

面向对象的特征有哪些?

面向对象特征之封装性

JAVASE面向对象:封装性(特性之一)构造器属性关键字