我想使用值表达式的动态参数来实现此目的:
<h:dataTable value="#{someBean.someValue}" var="field">
<h:column>#{anotherBean[field]}</h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)
这里field是'user.name'或'location.address.zip'或...
可能吗?
请注意,这是一个简单的例子,我对组件ValueExpression不感兴趣dataTable.
现在更新的问题是:如何替换标准的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);
...
Run Code Online (Sandbox Code Playgroud)
但我还没有完全理解旋转变压器链......所以我会去学习:)
更新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;
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下不支持此操作.你需要一个自定义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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要使其运行,请按以下方式注册faces-config.xml:
<application>
<el-resolver>com.example.ExtendedBeanELResolver</el-resolver>
</application>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |