What is the difference between #{...} and ${...} in EL Syntax [duplicate]
Asked Answered
G

4

12

My question is straightforward as you can see.

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

Growing answered 26/8, 2011 at 19:2 Comment(1)
@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.Growing
P
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:

Pleuron answered 26/8, 2011 at 19:21 Comment(7)
Hi! Thanks for the reply. I don't quite get your last statement: I took the simple JSF example from 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?Mumbletypeg
@Mumbletypeg 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.Pleuron
lol that's what I thought, I'm missing the date of the answer. Thank you, I'll check the linkMumbletypeg
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 thatMumbletypeg
@Mumbletypeg This should get you started:https://mcmap.net/q/16668/-when-is-it-necessary-or-convenient-to-use-spring-or-ejb3-or-all-of-them-together and https://mcmap.net/q/17118/-what-is-the-need-of-jsf-when-ui-can-be-achieved-with-javascript-libraries-such-as-jquery-and-angularjs. 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.Pleuron
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 schoenMumbletypeg
@Mumbletypeg I generally continuously review and update my answers as per current state of technology when I retrieve a vote or comment on it.Pleuron
H
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.

Hamamatsu answered 26/8, 2011 at 20:29 Comment(0)
J
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.

Jamaaljamaica answered 26/8, 2011 at 19:6 Comment(0)
P
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

Prelatism answered 26/8, 2011 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.