即时与延迟评估以读取bean属性 [英] immediate vs deferred evaluation for reading bean properties

查看:94
本文介绍了即时与延迟评估以读取bean属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用JSF immediate evaluationdeferred evaluation还是不清楚,主要是因为在线示例几乎从未使用前一种方法.

I am still unclear about the use of JSF immediate evaluation vs deferred evaluation, mainly because the online examples almost never seem to use the former method.

我已经看到了很多JSF示例,包括 http:中包含的示例: //docs.oracle.com/javaee/6/tutorial/doc/,我几乎只看过使用#{}而不是${}的示例,即使所有我们正在做的是读取bean属性.

I have seen a fair amount of JSF Examples, including those contained in http://docs.oracle.com/javaee/6/tutorial/doc/, and I am a bit puzzled by the fact that I almost only ever see examples with #{} instead of ${}, even when all we're doing is reading a bean property.

据我对这两种用法的理解,${}仅可用于读取bean属性,而不能用于编写.我还没有找到关于immediatedeferred评估在JSF生命周期方面的确切差异以及对读取bean属性有何差异的清晰解释.

From my understanding of these two uses, ${} can only be used to read bean properties, not to write. I have yet to find a clear explanation of the exact difference of immediate vs deferred evaluation in respect to the JSF life cycle and what difference that would make for reading bean properties.

如果每个人都总是使用#{}来读取bean属性,那么您何时会建议使用${}?

If everyone always uses the #{} for reading bean properties, when would you recommend the use of ${}?

因此,总而言之:

  • 为什么读取 bean属性时,每个人似乎都比${}更喜欢#{}?
  • 您何时建议使用${}代替#{}?
  • 您能否举一个例子,说明使用${}与使用#{}会有何不同(并且您可以解释为什么结果不同)吗?
  • Why does everyone seem to prefer #{} over ${} when reading bean properties?
  • When would you recommend using ${} instead of #{}?
  • Can you give me an example of where using ${} would have a different result from using #{} (and can you explain why the result is different)?

推荐答案

在Facelets中,${}被视为#{},因此从技术上讲没有区别,并且总是被延迟.

In Facelets, the ${} is treated as #{}, so there's technically no difference and it's always deferred.

在JSP 2.0和更早的版本中,${}不在范围内时不会自动创建托管bean.因此,只有在可以确保在组件树中的之前的某个地方使用了同一托管Bean上的#{}并采用查看构建时间与查看渲染时间生命周期.在所有JSP版本中,${}不能在模型中设置JSF输入组件的提交值.总而言之,将它们混合在一起可能会对初学者产生混淆,从长远来看,甚至对您自己也可能造成混淆,因此不建议这样做.

In JSP 2.0 and older, the ${} won't autocreate the managed bean when it's not in scope yet. You can thus only use it if you can guarantee that #{} on the very same managed bean is been used somewhere before in the component tree and also take view build time vs view render time lifecycle into account. In all JSP versions, the ${} can't set submitted values of JSF input components in model. All in all, mixing them is potentially confusing to starters and in long term even also to yourself and therefore not recommended.

这篇关于即时与延迟评估以读取bean属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆