immediate vs deferred evaluation for reading bean properties
Asked Answered
E

1

6

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)?
Ezequiel answered 20/3, 2013 at 14:35 Comment(1)
Sorry, wrong question posted. I think this one is related: Difference between JSP EL, JSF EL and Unified ELHuesman
L
9

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:

Lyndel answered 20/3, 2013 at 14:41 Comment(6)
Thanks for your quick reply. I don't understand what you mean by "autocreating the managed bean", however, as well as whether or not I can "guarantee that #{} ... has been used somewhere before in the component tree...". I did a little test: I created a new project, one index.xhtml page and one Bean.java JSF managed bean with one getter/property. I referenced the property with ${bean.name}. I deployed and everything worked on the first request. I'm not sure where this managed bean could have been used "before in the component tree" but my code ran without error. Can you explain what you meant?Ezequiel
p.s. I noticed you removed the "JSF EL" in the title of my question. When I look for extisting questions on SO it is harder to know what this question is about without that prefix. The tags are harder for the eye to catch when scanning a list of multiple questions. Is this a SO format request? If not, I'm not sure why you prefer to remove it.Ezequiel
I improved the answer. As to the title, they are already represented by tags. You can add e.g. [jsf][el] to search keywords to filter on tags. You do not need to repeat tags in the title like as in old fashioned dicussion forums where tags don't exist.Lyndel
ok. however, I'm still unclear about why docs.oracle.com/javaee/6/tutorial/doc/bnahr.html goes to such lengths to explain that, for #{}, In the case of JavaServer Faces technology, its controller can evaluate the expression at different phases of the lifecycle, depending on how the expression is being used in the page., whereas, for ${}, they explain that The JavaServer Faces implementation evaluates the expression ${sessionScope.cart.total}, converts it, and passes the returned value to the tag handler.Ezequiel
The <fmt:xxx> is a taghandler, not an UI component. Taghandlers run during view build time and thus by default evaluate EL during view build time already. I admit that this explanation/example in Java EE tutorial is misleading as it's not related to immediate/deferred evaluation. Exactly the same effect would happen when using #{} instead in that particular example. Note further also that <fmt:xxx> taglib is at its whole own not supported in Facelets, but only in JSP.Lyndel
Sounds like I'd better stick to #{} then :-). Thanks again for your help!Ezequiel

© 2022 - 2024 — McMap. All rights reserved.