English 中文(简体)
What is the difference between #{expr} and ${expr} in jsf? Are there any cases when we should prefer ${expr}? [duplicate]
原标题:
  • 时间:2009-12-04 15:35:19
  •  标签:
  • jsf
  • el

I ve read some time ago about the difference in Core JSF but now I can t find that place.

Nevertheless I don t remember that there was a word about cases when we should use ${expr} in jsf. So I m just curious what is the difference (in a chestnut) and if there a case to use ${expr} in JSF application?

最佳回答

To summarize in clear language: ${expression} does only get, while #{expression} can do both get and set. This is because the ${expression} is evaluated only once (immediate), while the #{expression} is evaluated on every access (deferred).

In JSF on JSP 2.0 or Facelets 1.x, when you put something like this as first expression of the page

${bean.property}

where bean is a request scoped managed bean, you will see nothing. But if bean is a session scoped managed bean and already been created before, then you will see the property value being printed. This also applies if the request scoped managed bean is created before by #{bean.xxx} in the same page.

If you instead do as first expression of the page

#{bean.property}

then EL will test if bean is null and if so, then it will set (create) a new one. If the property is set during bean construction, then you will see the property being displayed by this expression.

This all is mandatory to get among others JSF UIInput components such as <h:inputText> to work. When you submit the form, the #{expression} will set the values in the bean.

问题回答

From JavaEE tutorial:

All expressions using the ${} syntax are evaluated immediately. These expressions can only be used within template text or as the value of a JSP tag attribute that can accept runtime expressions. [...] Immediate evaluation expressions are always read-only value expressions. The expression shown above can only get the total price from the cart bean; it cannot set the total price.
Deferred evaluation expressions take the form #{expr} and can be evaluated at other phases of a page life cycle as defined by whatever technology is using the expression. In the case of JavaServer Faces technology, its controller can evaluate the expression at different phases of the life cycle depending on how the expression is being used in the page.




相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签