`
lingshangwen
  • 浏览: 61160 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

使用freemarker

阅读更多

public class Student {

	private Name name;
	private String no;
	private String sex;
	private Integer age;
	private Date entranceDate;
	private boolean local;

	public boolean isLocal() {
		return local;
	}

	public void setLocal(boolean local) {
		this.local = local;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}


	public Date getEntranceDate() {
		return entranceDate;
	}

	public void setEntranceDate(Date entranceDate) {
		this.entranceDate = entranceDate;
	}

}
   
public class Name {
	private String firstName;
	private String lastName;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}
 
public class FreeMarkerApp {

	public static void main(String[] args) {
		Configuration cfg = new Configuration();
		Writer out = null;
		try {
			cfg
					.setDirectoryForTemplateLoading(new File(
							"/home/lingshangwen/repo/eclipse/freemarker/src/main/resources/templates"));
			cfg.setObjectWrapper(new DefaultObjectWrapper());

			Template tmp = cfg.getTemplate("simple.ftl");

			Map root = new HashMap();

			List<Student> studentList = new ArrayList<Student>();
			Student student = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date entranceDate = sdf.parse("2004-09-10");
			Name name = null;
			for (int i = 0; i < 10; i++) {
				name = new Name();
				name.setFirstName("eagle" + i);
				name.setLastName("paraidse" + i);
				
				student = new Student();
				student.setName(name);
				student.setAge(24);
				student.setNo(String.valueOf(20040900 + i));
				student.setEntranceDate(entranceDate);
				if (i < 5) {
					student.setLocal(true);
					student.setSex("男");
				} else {
					student.setLocal(false);
					student.setSex("女");
				}

				studentList.add(student);
			}
			root.put("studentList", studentList);
			out = new OutputStreamWriter(System.out);
			tmp.process(root, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

 

simple.ftl

<#if studentList?exists>
	<#list studentList as student>
${student.name.firstName}.${student.name.lastName} ${student.no} ${student.sex} ${student.age?c} ${student.local?string("本地学生", "外地学生")}
	</#list>
</#if>
 
输出

eagle0.paraidse0 20040900 男 24 本地学生
eagle1.paraidse1 20040901 男 24 本地学生
eagle2.paraidse2 20040902 男 24 本地学生
eagle3.paraidse3 20040903 男 24 本地学生
eagle4.paraidse4 20040904 男 24 本地学生
eagle5.paraidse5 20040905 女 24 外地学生
eagle6.paraidse6 20040906 女 24 外地学生
eagle7.paraidse7 20040907 女 24 外地学生
eagle8.paraidse8 20040908 女 24 外地学生
eagle9.paraidse9 20040909 女 24 外地学生

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics