Freemarker handles null values

Source: Internet
Author: User

Freemarker handles null values


1. Design Ideas

(1) encapsulate student and course classes

(2) create an ftl file on the student course page

(3) create a test method


2. encapsulate courses

Course. java:

/*** @ Title: Course. java * @ Package: com. you. freemarker. model * @ Description: course encapsulation class * @ author: Youhaidong (Youhaidong) * @ date: 9:41:41 * @ version V1.0 */package com. you. freemarker. model;/*** class Function Description * class modifier modification date * modification Description *

Title: Course. java

*

Description: Personal Development of youhaidong

*

Copyright: Copyright (c) 2013

* @ Author: You Haidong * @ date: 9:41:41 * @ version V1.0 */public class Course {/*** Course name */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 ;}/***

Title:

*

Description: constructor with Parameters

* @ Param courseName */public Course (String courseName) {super (); this. courseName = courseName ;}/***

Title:

*

Description: No parameter Constructor

*/Public Course () {super ();}}

3. encapsulate student categories

Student. java:

/*** @ Title: Student. java * @ Package: com. you. freemarker. model * @ Description: Student Class * @ author: Youhaidong (Youhaidong) * @ date: 11:41:05 * @ version V1.0 */package com. you. freemarker. model; import java. io. serializable; import java. util. date;/*** class Function Description * class modifier modify Date * modification Description *

Title: Student. java

*

Description: Personal Development of youhaidong

*

Copyright: Copyright (c) 2013

* @ Author: You Haidong * @ date: 11:41:05 * @ version V1.0 */public class Student implements Serializable {/*** @ Fields serialVersionUID: serialize */private static final long serialVersionUID = 1L;/* Student name */private String studentName;/* Student gender */private String studentSex; /* ** student age */private int studentAge;/* student birthday */private Date studentBirthday;/* student address */private String studentAddr; /*** QQ */private long studentQQ;/*** Course */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 ;}/***

Title:

*

Description: No parameter Constructor

*/Public Student () {super ();}/***

Title:

*

Description: constructor with Parameters

* @ Param studentName * @ param studentSex * @ param studentAge * @ param Student * @ 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: Student Class * @ author: Youhaidong (Youhaidong) * @ date: 11:41:05 * @ version V1.0 */package com. you. freemarker. model; import java. io. serializable; import java. util. date;/*** class Function Description * class modifier modify Date * modification Description *

Title: Student. java

*

Description: Personal Development of youhaidong

*

Copyright: Copyright (c) 2013

* @ Author: You Haidong * @ date: 11:41:05 * @ version V1.0 */public class Student implements Serializable {/*** @ Fields serialVersionUID: serialize */private static final long serialVersionUID = 1L;/* Student name */private String studentName;/* Student gender */private String studentSex; /* ** student age */private int studentAge;/* student birthday */private Date studentBirthday;/* student address */private String studentAddr; /*** QQ */private long studentQQ;/*** Course */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 ;}/***

Title:

*

Description: No parameter Constructor

*/Public Student () {super ();}/***

Title:

*

Description: constructor with Parameters

* @ Param studentName * @ param studentSex * @ param studentAge * @ param Student * @ 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. Create an ftl file on the student course page

Course. ftl:

      
     Student Information      Name: $ {student. studentName} Gender: $ {student. studentSex} age: $ {student. studentAge} Birthday: $ {(student. studentBirthday )? String ("yyyy-MM-dd")} address: $ {student. studentAddr} QQ: $ {student. studentQQ} course: $ {student. course !}

5. Create a test method

/*** Test freemarker to process null values * @ Title: testCourse * @ Description: * @ param: * @ return: void * @ throws */@ Testpublic void testCourse () {// freemarker does not process null value root. put ("student", new Student ("Zhang Sanfeng", "male", 16, new Date (1988-12-12), "Hongshan district, Wuchang City, Hubei Province", 78451214 )); studentPrint ("course. ftl ");}

6. Running result

      
     Student Information      Name: Zhang Sanfeng Gender: male age: 16 Birthdate: Address: QQ: 78,451,214 course:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.