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>

freemarker处理空值的更多相关文章

  1. FreeMarker的空值运算符和逻辑运算符

    1.空值处理运算符 如果你在模板中使用了变量但是在代码中没有对变量赋值,那么运行生成时会抛出异常.但是有些时候,有的变量确实是null,怎么解决这个问题呢? 判断某变量是否存在:“??” 用法为:va ...

  2. freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)

    exists:由空值测试运算符的引入,它被废弃了. exp1?exists 和 exp1??是一样的, ( exp1)?exists 和(exp1)??也是一样的. if_exists:由默认值运算符 ...

  3. FreeMarker 的空值处理 , 简单理解 , 不用TMD就会忘记

    NO.1 而对于FreeMarker来说,null值和不存在的变量是完全一样的 NO.2 ! 指定缺失变量的默认值 返回String NO.3 ?? 判断变量是否存在 返回boolean NO.4 $ ...

  4. 默认值操作符(Freemarker的空值处理)

    默认值操作符: 使用形式例如: userName!default_expr 或 userName! 或 (userName)!default_expr 或 (userName)! 这个操作符允许你为可 ...

  5. Freemarker中空值 null的处理++++定义数组

    http://blog.java-zone.org/archives/800.html <#list listBlogPost as blogPost> </#list> 如果 ...

  6. freemarker中空值“”,null值的判断

    原文:http://zhousheng193.iteye.com/blog/1319772 <#if letVo.manageScore!=""> ${html('${ ...

  7. FreeMarker标签与使用

    模板技术在现代的软件开发中有着重要的地位,而目前最流行的两种模板技术恐怕要算freemarker和velocity了,webwork2.2对两者都有不错的支持,也就是说在webwork2中你可以随意选 ...

  8. Spring MVC 学习总结(七)——FreeMarker模板引擎与动态页面静态化

    模板引擎可以让程序实现界面与数据分离,业务代码与逻辑代码的分离,这就提升了开发效率,良好的设计也使得代码复用变得更加容易.一般的模板引擎都包含一个模板解析器和一套标记语言,好的模板引擎有简洁的语法规则 ...

  9. [转]一篇很全面的freemarker教程

    copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...

随机推荐

  1. 论事件驱动与异步IO

    通常我们写服务器模型,有以下几种模型: 每收到一个请求,创建一个新的进程,来处理该请求 每收到一个请求,创建一个新的线程,来处理该请求 每收到一个请求,放入到一个事件中,让主程序通过非阻塞I/0方式来 ...

  2. LANMP系列教程之php编译安装CentOS7环境

    前提:必须先安装好MySQL以及Apache   1.准备好源码包并配置好yum源,需要的源码包包括: libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel ...

  3. css模板

    最近好多人问我博客的css模板.... 现在是高三,没多少时间,趁放假赶紧更一下 主体就是把博客园的一个模板改动了一点 上面的图片特效,也是从别人那里得到的代码,大致就是下面那些,下面的三个图片换成自 ...

  4. 基于Mysql数据库的SSM分页查询

    前言: Hello,本Y又来了,"分页"在我们使用软件的过程中是一个很常见的场景,比如博客园对于每个博主的博客都进行了分页展示.可以简单清晰的展示数据,防止一下子将过多的数据展现给 ...

  5. shell编程之运算符(3)

    declare声明变量类型 declare[+/-][选项]变量名 选项: - : 给变量设定类型属性 + : 取消变量的类型属性 -a : 将变量声明为数组型 -i : 将变量声明为整数型(inte ...

  6. Redis 实践3-操作

    string常用操作 set key1  aminglinux get key1   set key1  aming //一个key对应一个value,多次赋值,会覆盖前面的value setnx k ...

  7. 利用Python爬取可用的代理IP

    前言 就以最近发现的一个免费代理IP网站为例:http://www.xicidaili.com/nn/.在使用的时候发现很多IP都用不了. 所以用Python写了个脚本,该脚本可以把能用的代理IP检测 ...

  8. url路径去掉两个opencms

    采用刚刚的方法安装OpenCMS之后,站点url中会存在两个opencms,造成访问url路径过长,下面讲解一种去掉两个opencms的方法. 1.去掉第一个opencms 安装时采用ROOT安装,即 ...

  9. Asp.Net Core 基于QuartzNet任务管理系统

    之前一直想搞个后台任务管理系统,零零散散的搞到现在,也算完成了. 这里发布出来,请园里的dalao批评指导! 废话不多说,进入正题. github地址:https://github.com/YANGK ...

  10. ASP.NET Core的身份认证框架IdentityServer4--(3)令牌服务配置访问控制跟UI添加

    使用密码保护API OAuth 2.0 资源所有者密码授权允许一个客户端发送用户名和密码到IdentityServer并获得一个表示该用户的可以用于访问api的Token. 该规范建议仅对" ...