What are the differences between ${} and #{}?

2019-01-16 07:06发布

I'm programming in JSF2 and netbeans creates many pages with #{} that contains an expression.

However sometimes on the web I found ${} for the same thing!

Are there any differences? What are they?

Google ignores the characters # and $ so it makes it difficult to search for.

标签: jsf-2 el
5条回答
萌系小妹纸
2楼-- · 2019-01-16 07:33

#{} are for deferred expressions (deferred expressions are resolved depending on the lifecycle of the page) and can be used to read or write from or to a bean or to make a method call. ${} are expressions for immediate resolution, as soon as they are encountered they are resolved. They are read-only. You can read more here: http://docs.oracle.com/javaee/6/tutorial/doc/bnahr.html

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-16 07:33

JSF EL uses a hash (#) where the JSP EL uses dollar sign ($) in jsf1.2 both syntaxes were unified

查看更多
你好瞎i
4楼-- · 2019-01-16 07:42

A google search for "Java Server Faces dollar pound" gave the following result, from the JBoss Expression Language FAQ:

Why do some expressions start with pound and others start with dollar sign?

For the EL specification itself, there is no difference. It is up to the technology using the EL to decide what it means. For both JSP and JSF, expressions that start with a pound sign mean deferred evaluation and a dollar sign means immediate evaluation. This all has to do with when the expression will actually be evaluated during request processing. The pound sign is used in JSF components because we want the expression to be evaluated by the JSF lifecycle and not by the JSP or Facelets engine.

查看更多
唯我独甜
5楼-- · 2019-01-16 07:44

The Java documentation gives the following explanation:

${customer.name}
#{customer.name}

The former uses immediate evaluation syntax, whereas the latter uses deferred evaluation syntax. The first expression accesses the name property, gets its value, adds the value to the response, and gets rendered on the page. The same can happen with the second expression. However, the tag handler can defer the evaluation of this expression to a later time in the page lifecycle, if the technology using this tag allows.

In the case of JavaServer Faces technology, the latter tag’s expression is evaluated immediately during an initial request for the page. In this case, this expression acts as an rvalue expression. During a postback request, this expression can be used to set the value of the name property with user input. In this case, the expression acts as an lvalue expression.

Read more here: value expressions

查看更多
虎瘦雄心在
6楼-- · 2019-01-16 07:45

That's a good question! I faced it once and like you, had a lot of trouble finding the answer... until I stumbled upon this piece of documentation:

One key feature of the unified EL is its support for both immediate and deferred evaluation of expressions. Immediate evaluation means that the JSP engine evaluates the expression and returns the result immediately when the page is first rendered. Deferred evaluation means that the technology using the expression language can employ its own machinery to evaluate the expression sometime later during the page's life cycle, whenever it is appropriate to do so. Those expressions that are evaluated immediately use the ${} syntax, which was introduced with the JSP 2.0 expression language. Expressions whose evaluation is deferred use the #{} syntax, which was introduced with by JavaServer Faces technology.

查看更多
登录 后发表回答