以编程方式获取facelets参数的表达式值(变量)
以下Java代码允许从Faces上下文访问任何对象或变量:
Following java code allows to access any object or variable from faces context:
ELContext elCtx = facesContext.getELContext();
ExpressionFactory exprFac = facesContext.getApplication().getExpressionFactory();
MyProperty myProperty = (MyProperty) exprFac.createValueExpression(elCtx, "#{somebean.someattr.someproperty}", MyProperty.class).getValue(elCtx);
我使用自定义转换器中的代码从上下文中读取其他转换参数.
I use the code from within my custom converter to read additional converting parameters from context.
如果在JSF上下文中将#{somebean}
定义为普通后备bean,则代码可以正常工作.
The code works correctly if #{somebean}
is defined as normal backing bean within JSF context.
Facelets允许为JSF表达式创建快捷方式".示例:
Facelets allow to create 'shortcut' to JSF expressions. Example:
<ui:param name="shortcut" value="#{somebean.someattr.someproperty}" />
<div>#{somebean.someattr.someproperty} equals #{shortcut}</div>
在这种情况下,#{somebean.someattr.someproperty}
和#{shortcut}
具有相同的值.
In this case both #{somebean.someattr.someproperty}
and #{shortcut}
have the same value.
但是,使用上面的Java代码无法访问这些快捷方式"名称.例如:
However these 'shortcut' names are not accessible using java code above. For example:
MyProperty myProperty1 = (MyProperty) exprFac.createValueExpression(elCtx, "#{somebean.someattr.someproperty}", MyProperty.class).getValue(elCtx);
// myProperty1 has expected value
MyProperty myProperty2 = (MyProperty) exprFac.createValueExpression(elCtx, "#{shortcut}", MyProperty.class).getValue(elCtx);
// myProperty2 is null
是否有一种方法可以访问facelets上下文并读取在当前JSF页面上定义的快捷方式"参数值?
Is there a way to access a facelets context and to read 'shortcut' parameter values, defined on the current JSF page?
我遇到了同样的问题,并选择了以下方法:
I had the same problem and have chosen the following approach:
/**
* Führt eine Expression im aktuellen Faces EL Context
* UND im Facelets El Context aus.
*
* @param facesContext
* @param expression
* @return object
*/
private static Object executeExpressionInUIContext (final FacesContext facesContext, final String expression) {
final ELContext elContext = facesContext.getELContext();
final Application application = facesContext.getApplication();
Object result = executeExpressionInElContext(application, elContext, expression);
if (null == result) {
FaceletContext faceletElContext = (FaceletContext) FacesContext.getCurrentInstance().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
result = executeExpressionInElContext(application, faceletElContext, expression);
}
return result;
}
private static Object executeExpressionInElContext (Application application, ELContext elContext, String expression) {
ExpressionFactory expressionFactory = application.getExpressionFactory();
ValueExpression exp = expressionFactory.createValueExpression(elContext, expression, Object.class);
return exp.getValue(elContext);
}
"ui:param"是Facelet视图处理技术的一部分. Facelets扩展了JSF. 两种技术在存储变量时都使用它们自己的 Context . 在Faces El Context旁边有一个Facelet El Context(FaceletContext).
"ui:param" is part of the Facelet view handling technology. Facelets extends JSF. Both technologies use their own Context when storing variables. Beside the Faces El Context there is a Facelet El Context (FaceletContext).
声明的方法在两种情况下都对表达式求值.请注意,如果在每个上下文中以相同的名称存储两个值,则此操作将无效.
The stated method evaluates expressions in both contexts. Be aware that this will not work if two values are stored under the same name in each context.