Freemarker write select component (3)

Source: Internet
Author: User

Freemarker write select component


1. macro definition

<# Macro select id datas value = "" key = "" text = ""> <select id = "$ {id}" name = "$ {id}"> <option> --- Select --- </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> </# list> </select> </# macro>

2. Encapsulation

/*** @ Title: User. java * @ Package: com. you. freemarker. model * @ Description: * @ author: Youhaidong (Youhaidong) * @ date: 10:28:01 * @ version V1.0 */package com. you. freemarker. model;/*** class Function Description * class modifier modification date * modification Description * <p> Title: User. java </p> * <p> Description: youhaidong personal development </p> * <p> Copyright: Copyright (c) 2013 </p> * @ author: youhaidong * @ date: 10:28:01 * @ version V1.0 */public class User {/*** User number */private int userNo; /*** user name */private String userName;/*** user age */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. Page Introduction

 <@items.select id="user" datas=users key="userNo" text="userName"/>

4. Junit Testing Method

/***** @ Title: testSelect * @ Description: * @ param: * @ return: void * @ throws */@ Testpublic void testSelect () {List <User> users = Arrays. asList (new User (1, "Zhang Sanfeng", 25), new User (2, "Li Sisi", 22), new User (3, "Zhao Wu", 23 ), new User (4, "Liu Xue", 20), new User (5, "Zhu Gu", 21), new User (6, "Zhao Wu", 23 ), new User (7, "wuyu", 24), new User (8, "xuanyu", 21); root. put ("users", users); studentPrint ("address. ftl "); studentFile (" address. ftl "," address.html ");}

5. Test Results

(1) console Printing

<Select id = "user" name = "user"> <option> --- select --- </option> <option value = "1"> Zhang Sanfeng </option> <option value = "2"> Li Sisi </option> <option value = "3"> Zhao Wu </option> <option value = "4"> Liu Xue </option> <option value = "5"> Zhu Gu </option> <option value = "6"> Zhao Wu </option> <option value = "7"> Wu Yu </option> <option value = "8"> xuanyu </option> </select>

(2) page display


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.