我想这个使用值表达式的动态参数来实现:
<h:dataTable value="#{someBean.someValue}" var="field">
<h:column>#{anotherBean[field]}</h:column>
</h:dataTable>
其中field
是'user.name'
或'location.address.zip'
或...
可能吗?
请注意,这是一个简单的例子,我有兴趣ValueExpression
不在dataTable
组件。
UPDATE现在的问题是:如何替换标准BeanELResolver?
在寻找ELUtils:
...
composite.addRootELResolver(IMPLICIT_RESOLVER);
composite.add(FLASH_RESOLVER);
composite.addPropertyELResolver(COMPOSITE_COMPONENT_ATTRIBUTES_EL_RESOLVER);
addELResolvers(composite, associate.getELResolversFromFacesConfig());
addVariableResolvers(composite, FacesCompositeELResolver.ELResolverChainType.Faces,
associate);
addPropertyResolvers(composite, associate);
composite.add(associate.getApplicationELResolvers());
composite.addRootELResolver(MANAGED_BEAN_RESOLVER);
composite.addPropertyELResolver(RESOURCE_RESOLVER);
composite.addPropertyELResolver(BUNDLE_RESOLVER);
...
但我不完全理解解析器链尚未...所以我会去学习:)
更新2
此代码的工作;)
public class ExtendedBeanELResolver extends BeanELResolver
{
@Override
public Object getValue(ELContext context, Object base, Object property) throws NullPointerException, PropertyNotFoundException, ELException
{
try
{
return super.getValue(context, base, property);
}
catch(PropertyNotFoundException e)
{
try
{
Object value = base;
for(String part : property.toString().split("\\."))
{
value = super.getValue(context, value, part);
}
return value;
}
catch(PropertyNotFoundException e1)
{
context.setPropertyResolved(false);
}
}
return null;
}
}
这是在默认情况下不支持。 你需要一个定制ELResolver
这里。 最简单的是扩展现有BeanELResolver
。
这里有一个开球例如:
public class ExtendedBeanELResolver extends BeanELResolver {
@Override
public Object getValue(ELContext context, Object base, Object property)
throws NullPointerException, PropertyNotFoundException, ELException
{
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;
for (String propertyPart : propertyString.split("\\.")) {
value = super.getValue(context, value, propertyPart);
}
return value;
}
else {
return super.getValue(context, base, property);
}
}
}
为了得到它的运行,如在下面进行注册faces-config.xml
:
<application>
<el-resolver>com.example.ExtendedBeanELResolver</el-resolver>
</application>
关于这一主题所作的工作很有趣,但不完整。
如果你传递一个组合元素,这是在该组件设置值,这是行不通的。
例如:
<composite:interface>
<composite:attribute name="value" type="java.lang.Boolean" required="true" />
</composite:interface>
<composite:implementation>
<h:inputText id="inputValue" value="#{cc.attrs.value}"
</h:inputText>
</composite:implementation>
随着ExtendedBeanElResolver,它抛出当我设置值PropertyNotFoundException。
所以,我花了几个小时找到一个解决方案,而这里是工作的解决方案,以便能够使用ExtendedBeanElResolver与设置复合材料部件内值的能力:
public class ExtendedBeanELResolver extends BeanELResolver {
private static final String PRIMEFACES_RESOURCE_PREFIX = "primefaces:";
private static final String RESOURCES_HANDLER = "class org.omnifaces.resourcehandler.GraphicResourceHandler";
private static final String PRIMEFACES_EXT_RESOURCES_HANDLER = "class org.primefaces.extensions.application.PrimeFacesExtensionsResourceHandler";
@Override
public Object getValue(ELContext context, Object base, Object property) {
if (property == null || base == null || base instanceof ResourceBundle || base instanceof Map || base instanceof Collection
|| property.toString().startsWith(PRIMEFACES_RESOURCE_PREFIX) || base.getClass().toString().equals(RESOURCES_HANDLER)
|| base.getClass().toString().equals(PRIMEFACES_EXT_RESOURCES_HANDLER)) {
return null;
}
String propertyString = property.toString();
if (propertyString.contains(".")) {
Object value = base;
for (String propertyPart : propertyString.split("\\.")) {
value = super.getValue(context, value, propertyPart);
}
return value;
} else {
Object v = super.getValue(context, base, property);
return v;
}
}
@Override
public void setValue(ELContext context, Object base, Object property, Object val) {
if (base != null && !(base instanceof ResourceBundle) && !(base instanceof Map) && !(base instanceof Collection))
super.setValue(context, base, property, val);
}
}
(I添加了 “的setValue” 的一部分)。
现在,它的工作原理。 不要犹豫,给我您的反馈意见,因为这是我在这个网站上的第一篇文章!