Reference Manual for Gracelets - 2.0.0.RC2SourceForge.net Logo
2.8 - Tutorial : Using Closure Bindings
In order to replace normal EL value bindings and use closures instead, you must use a special type of closure. You can create this type of closure with the Value() method that is automatically available to view scripts.

To illustrate, following is a bad example and a good example showing the difference between normal value closures and a closure binding:

Bad Example
Toggle Line Numbers
1 h.inputText(value: { someBean.property }) 
Good Example
Toggle Line Numbers
1 h.inputText(value: Value { someBean.property }) 
In the Bad Example, we are simply using read-only value closures. When the JSF Input component tries to set a value on someBean.property, it will fail. This is because gracelets has no way of knowing what your closure is doing, so it is, at least at the moment, reasonably impossible for gracelets to set any value in that closure.

However, as shown in the Good Example, when you use the Value() method to produce the closure you are telling Gracelets that this is a special closure, that only has a simple instruction in it referring to a property. Gracelets assuming what is expected will then use a Binding Walking technique to figure out the bean and the property on it that you want to get/set.

The type of logic you can put into a closure binding as show above is limited. Basically remember that inside this closure you MUST return a property of some bean. If you have very complex logic you will want to use the solution discussed below. But for most cases, where you simply just want to refer to a property, the above method should work fine.

Complex Closure Bindings

To write some complex setter/getter right inside of your script, you can use the Value() method in a different way. The following example will demonstrate this:

Toggle Line Numbers
1 h.inputText(value: Value( 
2     setter: { value -> if (value) { bean1.property = value; } else { bean1.cleanup(); } },  
3     getter: { bean1.property } 
4 )) 
Above we say that when we want to get the value, call the closure getter, which simple returns the property "property" of the "bean1" bean. However in the setter we say that if the value is not null or 0, then go ahead and set the property as usualy, otherwise call a special cleanup() method on the "bean1" bean.