我们建议不委托“先前”的 VariableResolver 来解析表达式,而是创建 ValueExpression 并计算它的值。
public class CustomVR extends VariableResolver {
private VariableResolver previous = null;
public CustomVR(VariableResolver previous) {
this.previous = previous;
}
public Object resolveVariable(FacesContext context, String name)
throws EvaluationException {
Object result = null;
// Do some action that may resolve the variable. If not, you
// may be tempted to simply do:
// result = previous.resolveVariable(context, name);
// But this would not work due to bug 6419278. A fix is
// available, please see the Release Notes. However, a
// workaround is the following.
ValueExpression ve = context.getApplication().getExpressionFactory(). \
createValueExpression(context.getELContext(), "#{" + name + "}", Object.class);
try {
result = ve.getValue(context.getELContext());
}
catch (PropertyNotFoundException pnfe) {
throw new EvaluationException(pnfe);
}
catch (ELException ele) {
throw new EvaluationException(ele);
}
return result;
}
}