enums are being interpreted as Strings

别来无恙 提交于 2019-12-11 02:17:52

问题


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;
        }
    }
}

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

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

${sessionScope.account.player.rank}

回答1:


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.




回答2:


I can not see the setter method of rankname in the above code.

public void setankName(String rankName) {


 this.rankName = rankName;
    }

I think this would be setRankName



来源:https://stackoverflow.com/questions/19217285/enums-are-being-interpreted-as-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!