Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2: Using enums in the rendered attribute

Tags:

enums

jsf

el

jsf-2

Is there any way to check declaratively whether an enum has a specified value. For example:

<h:graphicImage name="error.png" library="images" 
  rendered="#{viewController.current.status == Status.ERROR}" />

It's a little bit tedious to define a method in the managed beand that checks this for every enum value, e.g.

public boolean isStateIsError() {
  return current.getStatus() == Status.ERROR;
}

Is there a shorter/better way of doing this?

like image 852
Theo Avatar asked Jan 16 '11 17:01

Theo


2 Answers

Until EL 3.0 it's not possible to import enums in EL scope. You can however just treat and compare them like strings, i.e. the enum constant value must be quoted like below.

<h:graphicImage name="error.png" library="images" 
  rendered="#{viewController.current.status eq 'ERROR'}" />
like image 146
BalusC Avatar answered Oct 04 '22 00:10

BalusC


I know this question is a bit older now, but i had the same problem and found another solution, which i want to share :

Create a Custom EL-Resolver and use enums and java constants as objects in jsf el:

<h:graphicImage name="error.png" library="images"  
      rendered="#{viewController.current.status == Status.ERROR}" />

But before you can use enums this way you have to do 3 steps.

1. step - Copy this Class and replace "MY_ENUM" through your enumClass (in the example above it would be "Status")

public class EnumCache {
    private Map<String, Object>  propertCache = new HashMap<String, Object>();
    private Map<String, Class>  baseCache = new HashMap<String, Class>();
    private static EnumCache staticEnumCache = null;

    public static EnumCache instance() {
        if (staticEnumCache == null) { staticEnumCache = new EnumCache(); }
        return staticEnumCache;
    }
    private EnumCache() {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        classes.add(MY_ENUM.class);

        for(Class clazz : classes) {
            try {
                baseCache.put(clazz.getSimpleName(), clazz);
                Method m = clazz.getMethod("values", (Class[]) null);
                Enum<?>[] valueList = (Enum[]) m.invoke(null, (Object[]) null);
                for (Enum<?> en : valueList) {
                    propertCache.put(clazz.getSimpleName() + "." + en.name(), en);
                }
            } catch (Exception e) {
                System.err.println(clazz.getSimpleName(), e);
            }
        }
    }
    public Object getValueForKey(String key)  {
        return propertCache.get(key);
    }
    public Class getClassForKey(String key) {
        return baseCache.get(key);
    }
}

2. step - add this EnumResolver - This class will map your JSF expression to the enum in cache (step 1)

public class MyEnumResolver extends ELResolver {

    public Object getValue(ELContext context, Object base, Object property) {
        Object result = null;
        if (base == null) {
            result = EnumCache.instance().getClassForKey(property + "");
        } else if (base instanceof Class) {
            result = EnumCache.instance().getValueForKey(((Class) base).getSimpleName() + "." + property);
        }
        if (result != null) {
            context.setPropertyResolved(true);
        }
        return result;
    }

    public Class<?> getCommonPropertyType(ELContext context, Object base) {
        return null;
    }
    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
        return null;
    }
    public Class<?> getType(ELContext context, Object base, Object property) {
        return null;
    }
    public boolean isReadOnly(ELContext context, Object base, Object property) {
        return false;
    }
    public void setValue(ELContext context, Object base, Object property, Object arg3) {
    }
}

3. step - register the EnumResolver in faces-config.xml

<faces-config>
    <application>
        <el-resolver>com.asd.MyEnumResolver</el-resolver>
    </application>
</faces-config>

NOTE: If you want to access your java constants this way, you just have to extend the constructor of the enumCache class. This (untestet) example should work:

baseCache.put(CLASS_WITH_CONSTANTS.getSimpleName(), clazz);
for (Field field : CLASS_WITH_CONSTANTS.getDeclaredFields()) {
    try {
        propertCache.put(CLASS_WITH_CONSTANTS.getSimpleName() + "." 
                  + field.getName(), field.get(null));
    } catch (Exception e) { }
}

Hope this reduced but working code can help anybody.


Update

I see this benefits:

  1. If you use strings in jsf (viewController.current.status == 'ERROR_abcdefg'), you can misspell the value and wont recognise it so fast. With my solution you would get an error while loading the jsf file, because the enum could not be resolved.

  2. You can see in the sourcecode that "ERROR" is value of the enum "STATUS".

  3. When you compare two values in el, the class of the enums will be compared too. So for example PersonState.ACTIV is not the same like AccounState.ACTIV.

  4. When i have to change my enum value from PersonState.ACTIV to PersonState.ACTIVATED i can search for the String "PersonState.ACTIV" in my sourcecode. searching for "ACTIV" would have much more matches.

like image 31
Witali Janzen Avatar answered Oct 04 '22 01:10

Witali Janzen