枚举的使用
//--------------------------------------------实体-------------------------------------
private CanCi name;
@Enumerated(EnumType.STRING)
@Column(name = "name",nullable = false, length = 10)
public CanCi getName() {
return name;
}
public void setName(CanCi name) {
this.name = name;
}
public enum CanCi
{
QT("全天"){},
ZC("早餐"){},
WC("午餐") {},
W_C("晚餐") {};
private String value;
private CanCi(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
//--------------------------------------------controller-------------------------------------
model.addAttribute("types", MealTime.CanCi.values());
//--------------------------------------------jsp-------------------------------------
<select style="width: 80%;" id="coursesTypeId" name="name">
<c:forEach items="${types}" var="type">
<option value="${type}" <c:if test="${pageModel.name eq type}">selected="selected"</c:if> >${type.value}</option>
</c:forEach>
</select>
-----------------------------------jsp页面取固定的key和value---------------
//----------------------实体---------------
private ProductPayWay payType;
@Enumerated(EnumType.STRING)
@Column(name = "pay_type",nullable = false, length = 10)
public ProductPayWay getPayType() {
return payType;
}
public enum ProductPayWay
{
HTZ("后台支付"){},
WX("微信"){},
ZFB("支付宝"){},
CK("餐卡"){};
private String value;
private ProductPayWay(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
//---------------controller------------------
model.addAttribute("productPayWay",ProductPayWay.values());
//-------------jsp----------------------
${productPayWay[0]} --得到HTZ ${productPayWay[0].value } --得到后台支
${productPayWay[1]} --得到WX ${productPayWay[1].value } --得到微信
${productPayWay[2]} --得到ZFB ${productPayWay[2].value } --得到支付宝
${productPayWay[3]} --得到CK ${productPayWay[2].value } --得到餐卡
----------------------------------controller页面取固定的key和value---------------
//----------------------实体---------------
private ProductPayWay payType;
@Enumerated(EnumType.STRING)
@Column(name = "pay_type",nullable = false, length = 10)
public ProductPayWay getPayType() {
return payType;
}
public enum ProductPayWay
{
HTZ("后台支付"){},
WX("微信"){},
ZFB("支付宝"){},
CK("餐卡"){};
private String value;
private ProductPayWay(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
取key:
Enum.valueOf(ProductPayWay.class, ProductPayWay.WX.toString())
ProductPayWay.WX.name()
取value:
ProductPayWay.values()[1].getValue()
获取枚举的value:
mealTime.setTypeStr(MealType.valueOf(mealTime.getMealType().toString()).getValue());
获取枚举的key: