您的位置:首页 > 编程语言 > Java开发

通用的枚举类型

2013-10-09 16:16 369 查看
写一个接口

public interface DisplayEnum {

public String getDisplay();

}


接着写枚举

/**
* 性别
*/
public enum SexType implements DisplayEnum {

MEN("M"),
FELMEN("F");

private String value;

private SexType(String value) {
this.value = value;
}

public String getValue() {
return this.value;
}

private static Map<String, String> labelMap = new LinkedHashMap<String, String>();

static {
SexType.labelMap.put(SexType.MEN.getValue(), "男");
SexType.labelMap.put(SexType.FELMEN.getValue(), "女");
}

public static Map<String, String> getLabelMap() {
return SexType.labelMap;
}

@Override
public String getDisplay() {
return SexType.labelMap.get(this.getValue());
}

}


jsp中用spring标签来引入枚举类型

<spring:eval
expression="T(com.overallsituation.SexType).getLabelMap()"
var="sexType"></spring:eval>

<select name="sexType" class="select" reg="\S">
<c:forEach var="map" items="${sexType}">
<option value="${map.key }" ${command.sexType==map.key?'selected':''}>
${map.value }
</option>
</c:forEach>
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息