immediate vs deferred evaluation for reading bean

2019-06-27 16:37发布

问题:

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.

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.

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.

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

So, in summary:

  • 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)?

回答1:

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

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.

See also:

  • Difference between JSP EL, JSF EL and Unified EL