开发者

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

开发者 https://www.devze.com 2023-03-30 21:23 出处:网络
This question already has answers here: Whats the difference between "${foo.bar}" and "#{foo.bar}"?
This question already has answers here: Whats the difference between "${foo.bar}" and "#{foo.bar}"? (2 answers) Closed 9 years ago.开发者_开发问答

My question is straightforward as you can see.

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


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:

  • Difference between JSP EL, JSF EL and Unified EL


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消