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

帅比萌擦擦* 提交于 2019-11-27 03:20:27

问题


This question already has an answer here:

  • Whats the difference between “${foo.bar}” and “#{foo.bar}”? 2 answers

My question is straightforward as you can see.

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


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/7209377/what-is-the-difference-between-and-in-el-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!