JSF:如何判断 ValueExpression 是否不存在或者实际上它的值为 null?
FacesContext context = FacesContext.getCurrentInstance();
ELContext ctx = context.getELContext();
ValueExpression expression = context.getApplication().getExpressionFactory().createValueExpression(
ctx, "#{foo111.bar}", String.class);
String value = expression.getValue(ctx);
返回值为null。显然,bar不存在。有没有办法判断表达不存在或实际的价值为null。
FacesContext context = FacesContext.getCurrentInstance();
ELContext ctx = context.getELContext();
ValueExpression expression = context.getApplication().getExpressionFactory().createValueExpression(
ctx, "#{foo111.bar}", String.class);
String value = expression.getValue(ctx);
The return value is null. Obviously foo111.bar does not exist. Is there a way to tell if an expression does not exist or actually its value is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的方法是评估表达式。如果它是一个属性表达式,则应该没有副作用(并且智能 EL 实现会缓存此操作,因此如果多次调用,它应该很便宜)。
如果它是一个方法表达式,它会变得更加复杂。如果您只想“检查是否存在”,则调用该函数可能会导致状态突变。理论上,您可以获取基础对象并查看它是否存在,而无需调用该函数,这可以让您更接近一些,但这并不是一个完整的解决方案:
The best way to do that would be to evaluate the expression. If it's a property expression this should be side-effect free (And smart EL Implementations cache this operation, so it should be cheap if invoked several times).
If it's a method expression it'll get more complicated. Invoking the function could cause a state mutation if all you're wanting to do is 'check for existence'. You could in theory get the base object and see if it exists without invoking the function, which gets you a little closer, but isn't a complete solution: