枚举被解释为字符串 [英] enums are being interpreted as Strings

查看:198
本文介绍了枚举被解释为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登录时,我正在设置一个sessionScope对象,该bean对象由几个其他bean组成。这些bean的一个属性是一个枚举,但是我发现EL无法获取枚举类的属性,并且它只能获取一个java bean对象的属性。所以我决定为枚举创建一个bean类,并将枚举嵌套在该bean类中。我用来替换枚举的java bean,以便我可以用EL获得它的值,看起来像这样:

I am setting a sessionScope object when a user logs in, and that bean object is composed of a couple of other beans. One of the properties for these beans was an enum, but I found out that EL cannot get the properties of an enum class, and that it can only get the properties of a java bean object. So I decided to make a bean class for the enum, and have the enum nested in that bean class. the java bean that I made to replace the enum so that I can get its values with EL looks something like this:

public class RankBean implements Serializable {

    private static final long serialVersionUID = -1;

    private String rankName;

    public RankBean(String rankName) {
        this.rankName= rankName;
    }

    public RankBean(Rank rank) {
        this.rankName = rank.getRankName();
    }

    public String getRankName() {
            return rankName;
        }

    public void setRankName(String rankName) {
        this.rankName = rankName;
    }

    public static enum Rank {
        RANK_1("some rank name"),
        RANK_2("some rank name"),
        RANK_3("some rank name"),
        RANK_4("some rank name"),
        RANK_5("some rank name"),
        RANK_6("some rank name"),
        RANK_7("some rank name"),
        RANK_8("some rank name");

        private String rankName;

        private Rank(String rankName) {
            this.rankName = rankName;
        }

        public String getRankName() {
            return rankName;
        }

        public static Rank getRank(String rankName) {
            for (Rank rank : Rank.values()) {
                if (rank.getRankName().equals(rankName)) {
                    return rank;
                }
            }
            return null;
        }

        @Override
        public String toString() {
            return rankName;
        }
    }
}

然而,当我尝试访问等级名称(或可能有什么属性),我仍然收到一个JSP错误,表示rank对象是一个String,并且名为rankName的属性在java.lang.String中不存在。当我试图直接获取枚举的属性时,这是同样的问题,但现在我不是。
以下是错误信息:

However, when I try to access the rank name (or what ever property there may be), I still get a JSP error saying that the rank object is a String and a property named rankName does not exist in java.lang.String. This is the same problem I had when I was trying to get the properties of the enum directly, but now I am not. Here is the error message:

javax.el.PropertyNotFoundException: Property 'rankName' not found on type java.lang.String

所以以下EL会导致错误,因为rankName显然不存在。

So the following EL would cause error because rankName apparently doesnt exist.

${sessionScope.account.player.rank}


推荐答案

我的问题是我将RankBean对象传递给一个自定义jsp标签文件,我使用RankBean属性,而定义的属性没有指定键入,默认为java.lang.String。

Well my problem was that I was passing the RankBean object to a custom jsp tag file where I used the RankBean properties, and the defined attribute did not specify a type so it defaulted to java.lang.String.

这篇关于枚举被解释为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆