使用EL,是否可以在bean上进行现场访问,而不是在getter / setter上进行访问? [英] With EL, is it possible to do field access on beans rather than getters/setters?

查看:95
本文介绍了使用EL,是否可以在bean上进行现场访问,而不是在getter / setter上进行访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用一粒盐来回答这个问题。自从几年前我问这个问题以来,发生了很多变化。我建议现在使用Lombok代替我的EL解决方案。出于历史原因,留下了最初的问题。

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.

当我不这样做时,我非常讨厌吸气剂/设置器阻塞我的代码无需控制对对象内部状态的访问。我仍然必须生成getter / setter的唯一真实原因是因为EL通过定位方法而不是字段来工作: $ {myBean.fieldName} 。其中 fieldName 引用方法 getFieldName()

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?

基于更新,是否有可能将EL解析器扩展为仅返回公共字段值?关于史蒂夫·阿特金森(Steve Atkinson)的回答:
我希望这对其他人有帮助。请注意,我如何显式检查我仅在Form或Lead对象(这是我的域对象)上使用此elresolver。

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);
        }
    }
}


推荐答案

您可以使用 @Getter / @如果您不想编写/生成龙目岛中的Setter 批注Java Bean上的getter和setter方法。

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.

这篇关于使用EL,是否可以在bean上进行现场访问,而不是在getter / setter上进行访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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