Reference Manual for Gracelets - Logo
2.7 - Tutorial : Using Value Closures
Value closures can be considered some what "read only" closures. They are only used to retreive some value, like value expressions in EL.

These are to be distinguished clearly from closure bindings in Gracelets since making something like a value binding (Closure Bindings are discussed next) requires a closure to behave differently than a normal value closure. Well explain that in the next section. But again, to keep it straight, you use a value closure when you are only wanting to return a value, not when you are using it as a binding that would also have a corresponding setter side to it.

The following two examples we set the value on the JSF Core param tag, and the examples are practically the same, one uses EL and the other uses a value closure:

Toggle Line Numbers
1 j.param(name: "someName", value: "#{}") 
Toggle Line Numbers
1 j.param(name: "someName", value: { }) 
Of course, you do need to be aware of the expected value. In most cases it can be any object, but for example, if you are using a value closure for the 'rendered' attribute which is common for most all JSF components, you would need to make sure it would return a boolean value. The following two examples show a bad example and a good example of using value closures with the 'rendered' attribute:

Bad Example
Toggle Line Numbers
1 u.component(rendered: { someBean.setting * 5 }) 
Good Example
Toggle Line Numbers
1 u.component(rendered: { someBean.setting == 'on' }) 
In the Bad Example above, we are returning a non-boolean, thus it would fail with an attribute exception. However in the Good Example, we are returning the boolean value which is a result of the condition expressed (someBean.setting == 'on').

Now what about binding a value to, for example, some input component, like a text box. Since we have already mentioned above that value closures are 'read only', you could not use them successfully for this purpose. See the next section, Closure Bindings to see how to use closures with component attribute bindings.