【发布时间】:2011-10-20 09:12:03
【问题描述】:
我们如何在 Seam 托管 bean 上以编程方式解析 JSF EL 表达式?我尝试了以下方法,但它不起作用:
private String resolveExpression(String expression){
if(expression == null)
{
return null;
}
javax.faces.context.FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
try
{
// Here we bind a value expression into the item,
// so it can dynamically change its language
ELContext elCtx = facesCtx.getELContext();
ExpressionFactory ef = app.getExpressionFactory();
ValueExpression ve = ef.createValueExpression(elCtx, expression, String.class);
return (String) ve.getValue(elCtx);
}
catch (ELException ex) {
}
return expression;
}
在我的应用程序中,我有一个名为User 的bean,它位于Seam 的会话范围内,并且这个bean 有一个属性name。 EL 表达式为 #{usr.name},但该表达式在 Facelet 文件中正常工作时返回空。
【问题讨论】:
-
你试过#{usr.username}吗?
-
对不起 'name' 不是 'username' :D
-
也许你不应该在这里删除
ELException。尝试记录该异常,也许这会给您一些提示。 -
我按照你的建议记录了这个异常,但这里没有异常:(