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

后端 未结 5 1014
北恋
北恋 2020-12-22 21:09

I\'m programming in JSF2 and NetBeans creates many pages with #{} that contains an expression. However sometimes on the web I found ${} for the sam

相关标签:
5条回答
  • 2020-12-22 21:29

    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.

    0 讨论(0)
  • 2020-12-22 21:29

    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.

    0 讨论(0)
  • 2020-12-22 21:40

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

    0 讨论(0)
  • 2020-12-22 21:41

    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

    0 讨论(0)
  • 2020-12-22 21:46
    • #{} are for deferred expressions (they are resolved depending on the life cycle 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

    0 讨论(0)
提交回复
热议问题