OGNL 调用枚举's 的静态属性's 方法 [英] OGNL invoke an enum's static property's method

查看:39
本文介绍了OGNL 调用枚举's 的静态属性's 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的枚举:

package languages;

public enum Language
{
  ENG, GER, FRA;

  public static final Language DEFAULT = ENG;
}

我想通过调用 name() 使用 OGNL 获取 JSPDEFAULT 静态变量的名称 方法就可以了.

I'd like to get the name of the DEFAULT static variable in JSP using OGNL by invoking the name() method on it.

我尝试了这些,但没有一个奏效:

I tried these but none of them worked:

但这有效:

推荐答案

你不能.至少不是没有一些解决方法.

You cannot. At least not w/o some workaround.

在访问枚举中的静态字段时 - OGNL 将调用 java.lang.Enum 类的静态 valueOf 方法.

When it comes to accessing static fields in enum - OGNL will call static valueOf method of java.lang.Enum class.

你的表情会是这样的:

Enum.valueOf(Language.class, "DEFAULT");

这会导致 java.lang.IllegalArgumentException 被抛出,因为指定的枚举类型没有具有指定名称的常量.

Which results in java.lang.IllegalArgumentException being thrown because the specified enum type has no constant with the specified name.

这篇关于OGNL 调用枚举's 的静态属性's 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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