我是认真的累干将/ setter方法堵塞我的代码,当我并不需要控制访问对象的内部状态。 唯一的真正的原因,我不得不仍然产生getter / setter方法是因为JSF2.0 / EL 2.2作品定位方法,而不是字段: ${myBean.fieldName}
。 其中fieldName
指功能getFieldName()
是否有可能延长的EL解析器只返回public字段值,除非一个getter被发现?
编辑:我希望这可以帮助别人。 请注意如何我明确检查,我只能用这个elresolver表格或铅的对象,这是我的域对象。
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);
}
}
}