Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between #{expr} and ${expr} in jsf? Are there any cases when we should prefer ${expr}? [duplicate]

Tags:

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?

like image 884
Roman Avatar asked Dec 04 '09 15:12

Roman


1 Answers

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.

like image 196
BalusC Avatar answered Sep 28 '22 06:09

BalusC