FreeMarker中的null处理
来源:互联网 发布:手机淘宝6.7.6版本 编辑:程序博客网 时间:2024/05/16 05:10
Let`s drink code
This is the pojo class----user.java and Group.java
package com.jadyer.model;public class User {private int id;private int age;private String name;private Group group;/*==四个属性的setter和getter略==*/public User() {}public User(int id, int age, String name) {this.id = id;this.age = age;this.name = name;}}public class Group {private int id;private String name;/*==二个属性的setter和getter略==*/public Group() {}public Group(int id, String name) {this.id = id;this.name = name;}}
This is FreeMarker Util Class----FreeMarkerUtil.java
package com.jadyer.util;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.Writer;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;public class FreeMarkerUtil {/** * 获取指定目录下的模板文件 * @param name 模板文件的名称 * @param pathPrefix 模板文件的目录 */public Template getTemplate(String name, String pathPrefix) throws IOException{Configuration cfg = new Configuration(); //通过FreeMarker的Configuration对象可以读取ftl文件cfg.setClassForTemplateLoading(this.getClass(), pathPrefix); //设置模板文件的目录cfg.setDefaultEncoding("UTF-8"); //Set the default charset of the template filesTemplate temp = cfg.getTemplate(name); //在模板文件目录中寻找名为"name"的模板文件return temp; //此时FreeMarker就会到类路径下的"pathPrefix"文件夹中寻找名为"name"的模板文件}/** * 根据模板文件输出内容到控制台 * @param name 模板文件的名称 * @param pathPrefix 模板文件的目录 * @param rootMap 模板的数据模型 */public void print(String name, String pathPrefix, Map<String,Object> rootMap) throws TemplateException, IOException{this.getTemplate(name, pathPrefix).process(rootMap, new PrintWriter(System.out));}/** * 根据模板文件输出内容到指定的文件中 * @param name 模板文件的名称 * @param pathPrefix 模板文件的目录 * @param rootMap 模板的数据模型 * @param file 内容的输出文件 */public void printFile(String name, String pathPrefix, Map<String,Object> rootMap, File file) throws TemplateException, IOException{Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));this.getTemplate(name, pathPrefix).process(rootMap, out); //将模板文件内容以UTF-8编码输出到相应的流中if(null != out){out.close();}}}
This is our freemarker file----theNull.ftl
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><h2>${user.id}----${user.name}----${(user.group.name)!"group.name是空值"}</h2><#-- ??用于判断变量是否存在 --><#if (person.pname)??>person.pname----存在该对象<#else>person.pname----不存在该对象</#if><#--FreeMarker中的空值处理写法:${(user.group.name)!"空值"}说明:1)括号会告诉FreeMarker去连续判断括号内的所有对象。否则${user.group.name!"空值"}则只会判断name对象 2)在括号后面加一个感叹号,就可以对里面的值进行判断了。若其值不存在,FreeMarker就会使用感叹号后面的值替之-->
This is the test class----FreeMarkerTest.java
package com.jadyer.test;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import org.junit.Test;import com.jadyer.model.User;import com.jadyer.util.FreeMarkerUtil;import freemarker.template.TemplateException;public class FreeMarkerTest {@Testpublic void print() throws TemplateException, IOException{Map<String,Object> rootMap = new HashMap<String,Object>();//FreeMarker不会自动处理空值:此时user对象并没有Group值,如果在ftl中输出${user.group},则直接编译报错rootMap.put("user", new User(1, 22, "张小凡"));new FreeMarkerUtil().print("theNull.ftl", "/ftl", rootMap);new FreeMarkerUtil().printFile("theNull.ftl", "/ftl", rootMap, new File("D:\\ftl\\my.html"));}}
- FreeMarker中的null处理
- freemarker 处理null值
- freemarker中的null异常处理以及!与??的使用
- freemarker中的null异常处理以及!与??的使用
- freemarker 对null 的处理
- Freemarker中的null判断,小坑一枚
- FreeMarker 对null值的处理技巧
- FreeMarker 对null值的处理
- FreeMarker对null值的处理
- 处理json中的null
- FreeMarker中的日期时间处理
- freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)
- freemarker中空值 null的处理 ?exists ?default("")
- freemarker中空值 null的处理 ?exists ?if_exists ?default(“”)
- 怎样处理数据库中的NULL
- SQL中的NULL的处理
- velocity中的null值处理
- Velocity中的null值处理
- shell 脚本查看 cpu 负载
- Java程序员从笨鸟到菜鸟之(七十六)细谈Hibernate(十八)悲观锁和乐观锁解决hibernate并发
- 【linux安全】移除不必要的服务
- 哑铃健身计划及图解说明
- Spring与iBATIS集成时,一对多表会报NullPointerException问题的解释
- FreeMarker中的null处理
- js调用谷歌地图V3
- copy_to_user,copy_from_user,get_user,put_user函数比较
- 清理系统垃圾
- cmd 命令切换盘符
- Xpath
- JavaScript传中文值给Action出现乱码问题解决和返回ajax 返回 输出 json 乱码
- SVN简介
- Delphi图像处理 -- 图像颜色混合