FreeMarker编写一个用于实现<select/>效果的通用指令

完整版见https://jadyer.github.io/




Let`s drink code


首先是两个实体类User.java和Student.java

package com.jadyer.model;

public class User {
	private int id;
	private int age;
	private String name;
	/*== 两个属性的setter和getter略 ==*/
	public User() {}
	public User(int id, int age, String name) {
		this.id = id;
		this.age = age;
		this.name = name;
	}
}

public class Student {
	private String stuNo;
	private String stuName;
	/*== 两个属性的setter和getter略 ==*/
	public Student() {}
	public Student(String stuNo, String stuName) {
		this.stuNo = stuNo;
		this.stuName = stuName;
	}
}

接下来是自定义的工具类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 files
		Template 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();
		}
	}
}

接下来是位于//src//ftl//包中用于演示<select/>效果的selectUsage.ftl

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<#import "/inc/select.ftl" as my/>

<@my.genSelect id="address" datas=["黑龙江","哈尔滨","道里区"]/>

<@my.genSelect id="sex" datas=["选择性别","男","女"] defaultValue="男"/>

<@my.genSelect id="sex" datas={"1":"男", "0":"女"} defaultValue="0"/>

<@my.genSelect id="user" datas=userList key="id" text="name"/>

<@my.genSelect id="user" datas=userList key="id" text="name" defaultValue="22"/>

<@my.genSelect id="user" datas=userList key="id" text="name" headKey="-1" headText="选择用户"/>

<@my.genSelect id="stu" datas=stuList key="stuNo" text="stuName" headKey="-1" headText="选择学生"/>

下面是位于//src//ftl//inc//包中用于生成<select/>的通用指令的select.ftl

<#--
自定义指令genSelect的参数说明,如下
id           必填。用于指定<select>的id和name属性值
datas        必填。用于指定数据模型,可以是序列、哈希表、java.util.List等
key          选填。用于指定<option>的key和text
text         选填。同key
headKey      选填。用于指定所显示的第一个<option>,其具有提示信息的意义
headText     选填。同headKey
defaultValue 选填。用于指定默认的<option>
-->
<#macro genSelect id datas key="" text="" headKey="" headText="" defaultValue="">
	<select id="${id}" name="${id}">
	<#-- 判断是否需要显示提示性的<option> -->
	<#if headKey!="">
		<option value="${headKey}">${headText}</option>
	</#if>
	<#-- 判断传入的数据模型是否为哈希表 -->
	<#if datas?is_hash_ex>
		<#-- 获取哈希表的key集合 -->
		<#local keys=datas?keys/>
		<#list keys as key>
			<#if key==defaultValue>
				<option value="${key}" selected>${datas[key]}</option>
			<#else>
				<option value="${key}">${datas[key]}</option>
			</#if>
		</#list>
	<#-- 如果传入的数据模型不是哈希表,则可以当作序列或java.util.List来处理 -->
	<#else>
		<#list datas as data>
			<#-- 通常这个判断是针对数据模型为java.util.List的 -->
			<#if key!="">
				<#if defaultValue==data[key]?string>
					<option value="${data[key]}" selected>${data[text]}</option>
				<#else>
					<option value="${data[key]}">${data[text]}</option>
				</#if>
			<#-- 通常这个判断是针对数据模型为序列的 -->
			<#else>
				<#if defaultValue==data>
					<option value="${data}" selected>${data}</option>
				<#else>
					<option value="${data}">${data}</option>
				</#if>
			</#if>
		</#list>
	</#if>
	</select>
</#macro>

最后是JUnit4.x编写的测试类FreeMarkerTest.java

package com.jadyer.test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.jadyer.model.Student;
import com.jadyer.model.User;
import com.jadyer.util.FreeMarkerUtil;

import freemarker.template.TemplateException;

public class FreeMarkerTest {
	@Test
	public void print() throws TemplateException, IOException{
		List<User> userList = Arrays.asList(new User(11,36,"张起灵"),new User(22,32,"吴三省"),new User(33,29,"陈文锦"));
		List<Student> stuList = Arrays.asList(new Student("stu11","张小凡"),new Student("stu22","林惊羽"),new Student("stu33","曾书书"));
		Map<String,Object> rootMap = new HashMap<String,Object>();
		rootMap.put("userList", userList);
		rootMap.put("stuList", stuList);
		new FreeMarkerUtil().print("selectUsage.ftl", "/ftl", rootMap);
		new FreeMarkerUtil().printFile("selectUsage.ftl", "/ftl", rootMap, new File("D:\\ftl\\my.html"));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值