1. 程式人生 > >freemarker獲取封裝類中物件的屬性

freemarker獲取封裝類中物件的屬性

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

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>


           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述