写一个接口
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>