freemarker写select组件(三)

freemarker写select组件

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)页面显示

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveW91MjNoYWk0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

时间: 2024-10-13 14:02:14

freemarker写select组件(三)的相关文章

freemarker写select组件(二)

freemarker写select组件 1.宏定义 <#macro select id datas value=""> <select id="${id}" name="${id}"> <option>---请选择---</option> <#list datas as data> <#if value == data> <option value="${da

freemarker写select组件报错总结(五)

1.错误描述 六月 26, 2014 10:44:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "The only legal comparisons are between two numbers, two strings, or two dates.\nLeft hand operand is a freemarker.template.SimpleScalar

freemarker写select组件(四)

freemarker写select组件 1.宏定义 <#macro select id datas value="" key="" text="" headKey="" headValue=""> <select id="${id}" name="${id}"> <option>---请选择---</option> &l

freemarker写select组件报错总结(七)

1.错误描述 六月 26, 2014 11:26:27 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Expected collection or sequence. datas evaluated instead to freemarker.core.HashLiteral$SequenceHash on line 7, column 18 in inc/select

freemarker写select组件(五)

freemarker写select组件 1.宏定义 <#macro select id datas value="" key="" text="" headKey="" headValue=""> <select id="${id}" name="${id}"> <option>---请选择---</option> &l

freemarker写select组件报错总结(一)

1.具体错误如下 六月 25, 2014 11:26:29 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Expression name is undefined on line 2, column 33 in inc/select.ftl." Expression name is undefined on line 2, column 33 in inc/s

freemarker写select组件(一)

freemarker写select组件 1.宏定义 <#macro select id datas> <select id="${id}" name="${id}"> <option>---请选择---</option> <#list datas as data> <option value="${data}">${data}</option> </#lis

freemarker写select组件报错总结(四)

1.错误描述 <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker 下拉框</title> </head> <body> Error parsing imported template inc/select.ftl The problema

freemarker写select组件报错总结(二)

1.错误描写叙述 六月 25, 2014 11:32:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Macro select has no such argument: name" Macro select has no such argument: name The problematic instruction: ---------- ==>