您可以根据需要修改此 BalusC 答案。
基本上,您可以在将SpringBeanFacesELResolver
其用于 EL 解析器时进行扩展。但是,EL 解析器正在 Spring 上下文中寻找 Spring Bean。源代码很好地理解了 SpringBenFacesELResolver 的作用。
其次,您需要javax.el.BeanELResolver
访问 BalusC 答案中描述的托管 bean 值。为此,我使用 Java 反射。javax.el.BeanELResolver
可以SpringBeanFacesELResolver
在运行时动态加载,然后调用SpringBeanFacesELResolver#getValue
嵌套属性,就像在引用的答案中一样。
这是代码:
public class ExtendedSpringELResolver extends SpringBeanFacesELResolver {
@Override
public Object getValue(ELContext context, Object base, Object property)
{
if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection) {
return null;
}
String propertyString = property.toString();
if (propertyString.contains(".")) {
Object value = base;
Class []params= new Class[]{ELContext.class,Object.class,Object.class};
for (String propertyPart : propertyString.split("\\.")) {
Class aClass = BeanELResolver.class;
try {
Method getValueMethod = aClass.getDeclaredMethod("getValue",params );
value = getValueMethod.invoke(aClass.newInstance(), context, value, propertyPart);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return value;
}
else {
return super.getValue(context, base, property);
}
}
}
PS我用PrimeFaces 展示中的示例尝试了代码。我更改String color
为Color color
where Color
is a user defined class for my case,它只有一个 String 比例。我通过添加color.color
到列列表来访问这些值。
private String columnTemplate = "model manufacturer year color.color";