一,讲解一
1、宏定义
<#macro select id datas> <select id="${id}" name="${id}"> <option>---请选择---</option> <#list datas as data> <option value="${data}">${data}</option> </#list> </select> </#macro>
2、示例引入
<#import "/inc/select.ftl" as items/> <@items.select id="city" datas=["武汉市","黄石市","黄冈市","荆州市"]/>
3、示例结果
(1)控制台打印结果
<select id="city" name="city"> <option>---请选择---</option> <option value="武汉市">武汉市</option> <option value="黄石市">黄石市</option> <option value="黄冈市">黄冈市</option> <option value="荆州市">荆州市</option> </select>
(2)页面显示结果
二,讲解二
1、宏定义
<#macro select id datas value=""> <select id="${id}" name="${id}"> <option>---请选择---</option> <#list datas as data> <#if value == data> <option value="${data}" selected>${data}</option> <#else> <option value="${data}">${data}</option> </#if> </#list> </select> </#macro>
2、页面引入
<@items.select id="zhuanye" datas=["请选择专业","计算机科学与技术","物流工程","英语专业"] value="计算机科学与技术"/>
3、运行结果
(1)控制台
<select id="zhuanye" name="zhuanye"> <option>---请选择---</option> <option value="请选择专业">请选择专业</option> <option value="计算机科学与技术" selected>计算机科学与技术</option> <option value="物流工程">物流工程</option> <option value="英语专业">英语专业</option> </select>
(2)页面显示
三,讲解三
1、宏定义
<#macro select id datas value="" key="" text=""> <select id="${id}" name="${id}"> <option>---请选择---</option> <#list datas as data> <#if key!=""> <#if value == data[key]?string> <option value="${data[key]}" selected>${data[text]}</option> <#else> <option value="${data[key]}">${data[text]}</option> </#if> <#else> <#if value == data> <option value="${data}" selected>${data}</option> <#else> <option value="${data}">${data}</option> </#if> </#if> </#list> </select> </#macro>
2、封装类
/** * @Title:User.java * @Package:com.you.freemarker.model * @Description: * @author:Youhaidong(游海东) * @date:2014-6-26 下午10:28:01 * @version V1.0 */ package com.you.freemarker.model; /** * 类功能说明 * 类修改者 修改日期 * 修改说明 * <p>Title:User.java</p> * <p>Description:游海东个人开发</p> * <p>Copyright:Copyright(c)2013</p> * @author:游海东 * @date:2014-6-26 下午10:28:01 * @version V1.0 */ public class User { /** * 用户号 */ private int userNo; /** * 用户姓名 */ private String userName; /** * 用户年龄 */ private int userAge; /** * @return the userNo */ public int getUserNo() { return userNo; } /** * @param userNo the userNo to set */ public void setUserNo(int userNo) { this.userNo = userNo; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; } /** * @return the userAge */ public int getUserAge() { return userAge; } /** * @param userAge the userAge to set */ public void setUserAge(int userAge) { this.userAge = userAge; } /** * <p>Title:</p> * <p>Description:</p> * @param userNo * @param userName * @param userAge */ public User(int userNo, String userName, int userAge) { super(); this.userNo = userNo; this.userName = userName; this.userAge = userAge; } /** * <p>Title:</p> * <p>Description:</p> */ public User() { super(); } }
3、页面引入
<@items.select id="user" datas=users key="userNo" text="userName"/>
4、Junit测试方法
/** * * @Title:testSelect * @Description: * @param: * @return: void * @throws */ @Test public void testSelect() { List<User> users = Arrays.asList(new User(1,"张三丰",25), new User(2,"李思思",22), new User(3,"赵武",23), new User(4,"刘雪",20), new User(5,"朱顾",21), new User(6,"赵武",23), new User(7,"吴语",24), new User(8,"血玉",21)); root.put("users", users); studentPrint("address.ftl"); studentFile("address.ftl","address.html"); }
5、测试结果
(1)控制台打印
<select id="user" name="user"> <option>---请选择---</option> <option value="1">张三丰</option> <option value="2">李思思</option> <option value="3">赵武</option> <option value="4">刘雪</option> <option value="5">朱顾</option> <option value="6">赵武</option> <option value="7">吴语</option> <option value="8">血玉</option> </select>
(2)页面显示
四,讲解四
1、宏定义
<#macro select id datas value="" key="" text="" headKey="" headValue=""> <select id="${id}" name="${id}"> <option>---请选择---</option> <#if headKey!=""> <option value="${headKey}">${headValue}</option> </#if> <#list datas as data> <#if key!=""> <#if value == data[key]?string> <option value="${data[key]}" selected>${data[text]}</option> <#else> <option value="${data[key]}">${data[text]}</option> </#if> <#else> <#if value == data> <option value="${data}" selected>${data}</option> <#else> <option value="${data}">${data}</option> </#if> </#if> </#list> </select> </#macro>
2、引入宏
<@items.select id="username" datas=users key="userNo" text="userName" headKey="0" headValue="其他"/>
3、示例结果
<select id="username" name="username"> <option>---请选择---</option> <option value="0">其他</option> <option value="1">张三丰</option> <option value="2">李思思</option> <option value="3">赵武</option> <option value="4">刘雪</option> <option value="5">朱顾</option> <option value="6">赵武</option> <option value="7">吴语</option> <option value="8">血玉</option> </select>
五,讲解五
1、宏定义
<#macro select id datas value="" key="" text="" headKey="" headValue=""> <select id="${id}" name="${id}"> <option>---请选择---</option> <#if headKey!=""> <option value="${headKey}">${headValue}</option> </#if> <#if datas?is_hash_ex> <#local keys=datas?keys/> <#list keys as key> <#if key==value> <option value="${key}" selected>${datas[key]}</option> <#else> <option value="${key}">${datas[key]}</option> </#if> </#list> <#else> <#list datas as data> <#if key!=""> <#if value == data[key]?string> <option value="${data[key]}" selected>${data[text]}</option> <#else> <option value="${data[key]}">${data[text]}</option> </#if> <#else> <#if value == data> <option value="${data}" selected>${data}</option> <#else> <option value="${data}">${data}</option> </#if> </#if> </#list> </#if> </select> </#macro>
2、宏引入
<@items.select id="sex" datas={"0":"请选择学历","1":"小学","2":"初中","3":"高中","4":"本科"} />
3、运行结果
<select id="sex" name="sex"> <option>---请选择---</option> <option value="0">请选择学历</option> <option value="1">小学</option> <option value="2">初中</option> <option value="3">高中</option> <option value="4">本科</option> </select>