简体   繁体   English

EL枚举字符串处理

[英]EL enum string processing

I have a variable being passed to my JSP view from a spring controller that maps to an enum. 我有一个变量从一个映射到枚举的spring控制器传递给我的JSP视图。 It is being printed out at 'ENUM_VALUE', not very user friendly. 它打印在'ENUM_VALUE',不是非常用户友好。

What is the best way to convert this to a more readable form like 'Enum value'. 将此转换为更易读的形式(如“枚举值”)的最佳方法是什么。

I'd rather a pure EL solution so as to avoid writting more code in the controller to parse this, but all comments are appreciated. 我宁愿使用纯粹的EL解决方案,以避免在控制器中写入更多代码来解析它,但所有评论都表示赞赏。

That value is coming from Enum#name() method. 该值来自Enum#name()方法。 Just add a getter to your enum which returns the friendly name. 只需在枚举中添加一个getter,它返回友好名称。 Eg 例如

public String getFriendlyName() {
    return name().toLowerCase().replace("_", " ");
}

You can use it in EL like ${bean.someEnum.friendlyName} . 您可以在EL中使用它,如${bean.someEnum.friendlyName}

I'd add for every enum a description text when you define them. 当你定义它们时,我会为每个枚举添加一个描述文本。 Something like this. 像这样的东西。

public enum MyEnum {

    ENUM_VALUE("your friendly enum value");

    private String description;

    //constructor
    private MyEnum(String description) {
        this.description = description;
    }

    //add a getter for description
}

your EL would look like ${yourenum.description} 你的EL看起来像$ {yourenum.description}

Like Kacper86 said, the 2.1 specification says that Enum-to-String coercion should call .name() instead of toString() . 就像Kacper86所说的那样,2.1规范说Enum-to-String强制应该调用.name()而不是toString() I was unable to get BalusC's answer to work. 我无法得到BalusC的工作答案。

Personally, I prefer using an EL function instead of tacking on a getDescription method to my enum. 就个人而言,我更喜欢使用EL函数而不是将getDescription方法添加到我的枚举中。 I find that I usually always override the toString() method, so adding another method to get the same value seems like a bad design. 我发现我通常总是覆盖toString()方法,所以添加另一个方法来获得相同的值似乎是一个糟糕的设计。 If I'm working in Java, I just call toString() , so my problem is solely in the EL realm. 如果我在Java中工作,我只需要调用toString() ,所以我的问题仅在于EL领域。 Also, an EL function makes it so that you don't have to modify your enum class every time you want to print out a new enum on your web page; 此外,EL功能使您无需在每次要在网页上打印新的枚举时修改枚举类; you just reuse your EL function for any enum. 你只需要为任何枚举重用你的EL函数。

Here's some simple code to create an EL function: 这是创建EL函数的一些简单代码:

public final class JSTLUtilityFunctions 
{
    public static String enumToStr(Enum<?> enumInst) {
        return enumInst.toString();
    }
}

In your TLD: 在您的TLD中:

<taglib>
    <!-- Other stuff... -->
    <function>
        <name>enumToStr</name>
        <function-class>yourpackage.JSTLUtilityFunctions</function-class>
        <function-signature>java.lang.String enumToStr(java.lang.Enum)</function-signature>
    </function>
</taglib>

Usage: 用法:

<%@taglib prefix="util" uri="your-TLD-uri.tld" %>
<span>${ util:enumToStr(myEnumInstance) }</span>

Obviously, you could change the method signature to accept any instance of Object if you wanted. 显然,如果需要,您可以更改方法签名以接受任何Object实例。

你可以在jsp中做这样的事情:

<span>${myenum.toString()}</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM