JAVA中如何使用枚举

发布时间:2021-06-30 17:54:37 作者:Leah
来源:亿速云 阅读:97

这篇文章给大家介绍JAVA中如何使用枚举,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1. 定义枚举

`public enum DiscountTypeEnum { expressfee("减免快递费", 1), days("减免天数", 2), price("减免金额", 3);

    String displayName;
    Integer index;

    DiscountTypeEnum(String displayName, Integer index) {
        this.displayName = displayName;
        this.index = index;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    /**
     * 枚举集合
     * 供前端展示使用
     */
    public static List<DiscountTypeEnum> checkDetailFields() {
        List<DiscountTypeEnum> list = new ArrayList<>();
        for (DiscountTypeEnum o : DiscountTypeEnum.values()) {
            list.add(o);
        }
        return list;
    }

    public static List<Pair<String, String>> detailPairList() {
        List<Pair<String, String>> pairs = new ArrayList<>();
        checkDetailFields().forEach(o -> {
            pairs.add(Pair.of(o.toString(), o.getDisplayName()));
        });
        return pairs;
    }

}`

2. JAVA实体中使用 private DiscountTypeEnum type; //优惠码类型 @Enumerated(EnumType.STRING) @Column(name = "type") public DiscountTypeEnum getType() { return type; } public void setType(DiscountTypeEnum type) { this.type = type; }

3. Action中存放供JSP使用 ActionContext.getContext().put("types", DiscountTypeEnum.checkDetailFields());

4. JSP页面中使用 <span> <i>优惠码类型:</i> <select id="sel_type" name="entitydata.type" onchange="disabledByType();"> <option value="">请选择类型</option> <s:iterator value="types" id="entitydata_type" status="index"> <option value="${entitydata_type}" <s:if test="entitydata.type.index==index">selected="selected"</s:if>> <s:property value="displayName"/></option> </s:iterator> </select> </span>

<span> <i>优惠码类型</i><samp>:</samp> <select name="entitydata.type"> <option value="">全部</option> <s:iterator value="types" id="entitydata_type" status="index"> <option value="${entitydata_type}" <s:if test="entitydata.type==index">selected="selected"</s:if>> <s:property value="displayName"/></option> </s:iterator> </select> </span>

关于JAVA中如何使用枚举就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. java枚举与.net中的枚举区别
  2. java中的枚举如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java中如何实现悲观锁与乐观锁

下一篇:JDK8中新增的StampedLock有什么作用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》