Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access enum properties in EL?

Given the following enum.

public enum Constants
{
    PAGE_LINKS(10);
    //Other constants as and when required.

    private final int value;

    private Constants(int value){
        this.value = value;
    }

    public int getValue(){
        value;
    }    
}

This enum is placed under an application scoped bean like so,

@ManagedBean
@ApplicationScoped
public final class ConstantsBean
{
    private Constants constants;

    public ConstantsBean() {}

    public Constants getConstants() {
        return constants;
    }
}

How to access the value of PAGE_LINKS in EL?

<p:dataGrid pageLinks="#{}".../>

What should be written in #{}? Is it possible?


EDIT:

Modifying the bean in the following way,

@ManagedBean
@ApplicationScoped
public final class ConstantsBean
{
    public ConstantsBean() {}

    public int getValue(Constants constants) {
        return constants.getValue();
    }
}

and then accessing in EL like so,

<p:dataGrid pageLinks="#{constantsBean.getValue('PAGE_LINKS')}".../>

somehow works but I don't believe in this ugly way.

like image 881
Tiny Avatar asked Apr 17 '14 03:04

Tiny


People also ask

Can enums have attributes?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).

How do you find the index of an element in an enum?

Use the Object. values() method to get an array containing the enum's values. Use square brackets to access the array at the specific index and get the value.

How do you access methods inside an enum?

Here is a simple example I write to show how to access to Enum's method. In order to call Enum's method, we can not just directly call as usual class's method “myClass. doSomething()”. Instead, we must assign a value to the Enum before we can invoke the method.

How do you use enums correctly?

You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values. Examples would be things like type constants (contract status: “permanent”, “temp”, “apprentice”), or flags (“execute now”, “defer execution”).


1 Answers

This, as commented by Sazzadur,

#{constantsBean.constants.value}

should work. Your enum has a proper public getter for its value property. However, you should also make sure that you set the constants property of the managed bean to the desired enum value. You namely didn't do that in the code snippet posted so far and thus it remains null. EL does by design not print anything when a (base) property is null.

Here's how you could set it:

@ManagedBean
@ApplicationScoped
public final class ConstantsBean {
    private Constants constants = Constants.PAGE_LINKS;

    public Constants getConstants() {
        return constants;
    }
}

I'd however rename the property (and getter) to pageLinks for better self-documentability.

#{constantsBean.pageLinks.value}

An alternative is to use OmniFaces <o:importConstants>, based on your question history you're already familiar with OmniFaces and probably also already using it in your current project.

<o:importConstants type="com.example.Constants" />
...
#{Constants.PAGE_LINKS.value}

This way you don't need to wrap the thing in an application scoped bean.

like image 141
BalusC Avatar answered Sep 17 '22 17:09

BalusC