使用Java EL 2.2 / JSF2是否可以进行字段访问而不是getter / setter? [英] With Java EL 2.2 / JSF2 Is it possible to do field access rather than getters/setters?
问题描述
当我不需要控制对对象内部状态的访问时,我非常厌倦了getter / setter堵塞我的代码。我必须仍然生成getter / setter的唯一真正原因是因为JSF2.0 / EL 2.2通过定位方法而不是字段来工作: $ {myBean.fieldName}
。其中 fieldName
是指函数 getFieldName()
。是否有可能扩展EL解析器只返回公共字段值,除非找到了getter?
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 JSF2.0 / EL 2.2 works by locating methods, not fields: ${myBean.fieldName}
. where fieldName
refers to the function getFieldName()
. Would it be possible to extend an EL Resolver to just return the public field value unless a getter was found?
编辑:
我希望这可以帮助其他人。注意我是如何明确地检查我只在Form或Lead对象上使用这个elresolver,它们是我的域对象。
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);
}
}
}
推荐答案
是。 Jsf是非常可扩展的,几乎所有东西都可以扩展或替换。
Yes. Jsf is very extensible, pretty much everything can be extended or replaced.
自定义解析器的一个很好的例子是这里
A great example of custom resolvers is here
这家伙用它来填充数据库查询的下拉列表。我确信您可以使用类似的技术来解析基于公共字段而不是公共get / set方法。
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.
这篇关于使用Java EL 2.2 / JSF2是否可以进行字段访问而不是getter / setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!