zoukankan      html  css  js  c++  java
  • 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>
    


  • 相关阅读:
    (连通图 模板题 无向图求桥)Critical Links -- UVA -- 796
    (连通图 模板题)迷宫城堡--hdu--1269
    (连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)
    (线段树 区间合并更新)Tunnel Warfare --hdu --1540
    (线段树 区间查询更新) Can you answer these queries? -- hdu--4027
    (线段树)Balanced Lineup --POJ --3264
    (线段树) Count the Colors --ZOJ --1610
    (线段树)Just a Hook -- hdu -- 1689
    All about <httpRuntime targetFramework>
    ASP.NET 4.0升级至ASP.NET 4.5需要注意的地方 【转】
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13315084.html
Copyright © 2011-2022 走看看