首页 > 代码库 > freemarker处理空值
freemarker处理空值
freemarker处理空值
1、设计思路
(1)封装学生类和课程类
(2)新建学生课程页面ftl文件
(3)创建测试方法
2、封装课程类
Course.java:
/** * @Title:Course.java * @Package:com.you.freemarker.model * @Description:课程封装类 * @author:Youhaidong(游海东) * @date:2014-5-28 下午9:41:41 * @version V1.0 */ package com.you.freemarker.model; /** * 类功能说明 * 类修改者 修改日期 * 修改说明 * <p>Title:Course.java</p> * <p>Description:游海东个人开发</p> * <p>Copyright:Copyright(c)2013</p> * @author:游海东 * @date:2014-5-28 下午9:41:41 * @version V1.0 */ public class Course { /** * 课程名 */ private String courseName; /** * @return the courseName */ public String getCourseName() { return courseName; } /** * @param courseName the courseName to set */ public void setCourseName(String courseName) { this.courseName = courseName; } /** * <p>Title:</p> * <p>Description:有参构造函数</p> * @param courseName */ public Course(String courseName) { super(); this.courseName = courseName; } /** * <p>Title:</p> * <p>Description:无参构造函数</p> */ public Course() { super(); } }
3、封装学生类
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; /** * 课程 */ private Course course; /** * @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; } /** * @return the course */ public Course getCourse() { return course; } /** * @param course the course to set */ public void setCourse(Course course) { this.course = course; } } /** * @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; /** * 课程 */ private Course course; /** * @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; } /** * @return the course */ public Course getCourse() { return course; } /** * @param course the course to set */ public void setCourse(Course course) { this.course = course; } }
4、新建学生课程页面ftl文件
course.ftl:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>学生信息</title> </head> <body> 姓名:${student.studentName} 性别:${student.studentSex} 年龄:${student.studentAge} 生日:${(student.studentBirthday)?string("yyyy-MM-dd")} 地址:${student.studentAddr} QQ:${student.studentQQ} 课程:${student.course!} </body> </html>
5、创建测试方法
/** * 测试freemarker处理空值 * @Title:testCourse * @Description: * @param: * @return: void * @throws */ @Test public void testCourse() { //freemarker不会处理空值 root.put("student", new Student("张三丰","男",16,new Date(1988-12-12),"湖北省武汉市武昌洪山区",78451214)); studentPrint("course.ftl"); }
6、运行结果
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>学生信息</title> </head> <body> 姓名:张三丰 性别:男 年龄:16 生日:1970-01-01 地址:湖北省武汉市武昌洪山区 QQ:78,451,214 课程: </body> </html>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。