12

My question is straightforward as you can see.

What is the difference between #{...} and ${...} in EL Syntax?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kowser
  • 8,123
  • 7
  • 40
  • 63
  • @McDowell: yes. I see exact duplicate :-S. I believe here we have nice answers. However I am not sure whether to delete it or not. – Kowser Aug 27 '11 at 15:23

4 Answers4

17

Simply put, the ${} can do only a get, while the #{} can do a get and a set of the value.

In JSF on legacy JSP, the #{} syntax is mandatory to trigger auto-creation of managed beans and to set request parameters as model values. If you used ${bean.value} in a JSF page, then the managed bean with name bean won't be auto-created if it isn't already in the scope. Also, the managed bean property value won't be set if the form was submitted with that value in an input component.

In JSF on Facelets, the ${} is reinterpreted as #{} and thus they will behave exactly the same.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi! Thanks for the reply. I don't quite get your last statement: I took the simple JSF example from https://docs.oracle.com/javaee/7/tutorial/jsf-facelets003.htm and changed the `value` property of the `h:inputText` component with `id=userNo` from `#` (deferred) to `$` (immediate), and the app apparently works the same. What am I missing? – fr_andres Nov 08 '17 at 18:39
  • @fr_andres The answer was written with JSF on JSP in mind. When using Facelets, they behave both exactly the same. See also the "See also" link in above answer. – BalusC Nov 08 '17 at 18:41
  • lol that's what I thought, I'm missing the date of the answer. Thank you, I'll check the link – fr_andres Nov 08 '17 at 18:42
  • now that I catch you, and totally off topic, would you recommend me to go in 2017 down the JSF+persistence route for a scalable dynamic web (not asking if better as spring, just if it is fine)? And is there a preferred relational DB to work with? I struggle to find clear clues for that – fr_andres Nov 08 '17 at 18:46
  • 1
    @fr_andres This should get you started:https://stackoverflow.com/q/18369356 and https://stackoverflow.com/q/4421839. Continue through the "See also" links. Every time. As to the relational DB, I don't have a particular preference. I've hands on experience with PostgreSQL, MySQL and DB2. They have all its own set of quirks, but generally JPA/Hibernate can handle it well. – BalusC Nov 08 '17 at 19:35
  • Cool! So although this links are still at least 4 years old, I assume the only drawbacks nowadays are the "steep" learning curve and a bad reputation. I can deal with that! Danke schoen – fr_andres Nov 08 '17 at 20:01
  • 1
    @fr_andres I generally continuously review and update my answers as per current state of technology when I retrieve a vote or comment on it. – BalusC Nov 08 '17 at 20:04
8

The result of ${...} is a value, while the result of #{...} is a binding. This binding can be executed over and over again.

EL distinguishes between two kinds of bindings; a value binding and a method binding. The value binding is just a convenience for a general method binding, as it represents both a getter and setter via a single expression.

In a way, ${...} can be compared with passing a value into a method via an expression:

foo(bar.kaz());

At runtime, bar.kaz() is evaluated and foo only receives the value returned. The foo method knows nothing about bar.kaz() and cannot do the evaluation again at a later time.

#{...} can be compared a little with passing a lambda into a method, or an old anonymous inner class:

foo(new IntegerReturn() { public int execute() {
    bar.kaz();
});

Here, foo gets an IntegerReturn that it can invoke as much as it wants at the time it wants.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
4

Right from the source

Consider these two value expressions:

${book.quantity}
#{book.quantity}

The first one uses immediate evaluation syntax, whereas the second one uses deferred evaluation syntax. The first expression accesses the quantity property, gets its value, and the value is added to the response and rendered on the page. The same thing happens with the second expression if it is evaluated during an initial request. In this case, both expressions are rvalue expressions.

Community
  • 1
  • 1
Nicholas
  • 7,403
  • 10
  • 48
  • 76
1

Check out these two great articles from Sun:

Web Tier to Go With Java EE 5: Summary of New Features in JSP 2.1 Technology

Unified Expression Language