With EL, is it possible to do field access on beans rather than getters/setters?
Asked Answered
C

2

7

EDIT

Take this answer with a grain of salt. Much has changed since I asked this question years ago. I recommend now using Lombok instead of my EL solution. Leaving the original question for historical reasons.


I'm serious tired of getters/setters clogging my code, when I don't need to control access to the internal state of an object. The only real reason I have to still generate getters/setters is because EL works by locating methods, not fields: ${myBean.fieldName}. where fieldName refers to the method getFieldName(). Would it be possible to extend an EL Resolver to just return the public field value unless a getter was found?


Update based on answer of Steve Atkinson: I hope this helps someone else. Notice how I explicitly check that I only use this elresolver on Form or Lead objects, which are my domain objects.

public class PublicFieldSupportingELResolver extends ELResolver {
    @Override
    public Class<?> getCommonPropertyType(ELContext context, Object base) {
        if (base instanceof Form || base instanceof Lead) {
            try {
                context.setPropertyResolved(true);
                return base.getClass();
            } catch (Exception e) {
                context.setPropertyResolved(false);
                return null;
            }
        } else {
            context.setPropertyResolved(false);
            return null;
        }
    }

    @Override
    public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
        return null;
    }

    @Override
    public Class<?> getType(ELContext context, Object base, Object property) {
        if (base instanceof Form || base instanceof Lead) {
            try {
                Field field = base.getClass().getField((String) property);
                context.setPropertyResolved(true);
                return field.getType();
            } catch (Exception e) {
                context.setPropertyResolved(false);
                return null;
            }
        } else {
            context.setPropertyResolved(false);
            return null;
        }
    }

    @Override
    public Object getValue(ELContext context, Object base, Object property) {
        if (base instanceof Form || base instanceof Lead) {
            try {
                Field field = base.getClass().getField((String) property);
                Object value = field.get(base);
                context.setPropertyResolved(true);
                return value;
            } catch (Exception e) {
                context.setPropertyResolved(false);
                return null;
            }
        } else {
            context.setPropertyResolved(false);
            return null;
        }
    }

    @Override
    public boolean isReadOnly(ELContext context, Object base, Object property) {
        if (base instanceof Form || base instanceof Lead) {
            try {
                base.getClass().getField((String) property);
                context.setPropertyResolved(true);
                return true;
            } catch (Exception e) {
                context.setPropertyResolved(false);
                return false;
            }
        } else {
            context.setPropertyResolved(false);
            return false;
        }
    }

    @Override
    public void setValue(ELContext context, Object base, Object property, Object value) {
        if (base instanceof Form || base instanceof Lead) {
            try {
                Field field = base.getClass().getField((String) property);
                field.set(base, value);
                context.setPropertyResolved(true);
            } catch (Exception e) {
                context.setPropertyResolved(false);
            }
        } else {
            context.setPropertyResolved(false);
        }
    }
}
Coexecutor answered 30/1, 2013 at 17:52 Comment(2)
Warning: this won't necessarily work for proxied beans (e.g. CDI).Nosey
Yeah, this really shouldn't be done anymoreCoexecutor
M
6

You can use @Getter/@Setter annotations from Project Lombok if you don't want to write/generate getter and setter method(s) on your Java Bean.

Marinelli answered 30/1, 2013 at 17:58 Comment(0)
A
3

Yes. Jsf is very extensible, pretty much everything can be extended or replaced.

A great example of custom resolvers is here

The guy was using it to populate dropdown lists from database queries. I'm certain you could use similar techniques to resolve based on public fields rather than public get/set methods.

At answered 30/1, 2013 at 18:0 Comment(3)
If I replace the el-resolver element in my faces-config, I can't use the default one. Any way to just tack an additional resolver on without replacing the default one?Coexecutor
@JonathanS.Fisher yeah well an answer to that would have been much appreciatedCayuga
See the Edit on the first post :)Coexecutor

© 2022 - 2024 — McMap. All rights reserved.