---
title: "freemarker 遍历 enum"
author: "kabuka"
date: "2013-10-31T13:39:12Z"
tags: []
categories: ["编程语言"]
slug: "freemarker-enum"
draft: false
lastmod: "2013-10-31T13:39:12Z"
id: 1967584
url: "https://kabuka.iteye.com/blog/1967584"
---

freemarker遍历enum 示例：

 

enum

```
package com.xxx;

public enum BooleanEnum {
    TRUE(Boolean.TRUE, "是"), FALSE(Boolean.FALSE, "否");

    private final Boolean value;
    private final String info;

    private BooleanEnum(Boolean value, String info) {
        this.value = value;
        this.info = info;
    }

    public String getInfo() {
        return info;
    }

    public Boolean getValue() {
        return value;
    }
}
```

 

在controller或者interceptor中，将freemarker的包装类添加到model中：

```
model.addObject("enums", BeansWrapper.getDefaultInstance()
				.getEnumModels());
```

 

页面上即可进行遍历

```
<select>
	<#list enums["com.xxx.BooleanEnum"]?values as item>
		<option value="${item}">${item.info}</option>
	</#list>
	<option value="">全部</option>
</select>
```