Reference Manual for Gracelets - 2.0.0.SP2SourceForge.net Logo
2.12 - Tutorial : Using Closures Factories
Many JSF/Facelet views need to be able to generate data for grids, tables and other components. More than not this data needs to be scoped, many times in a conversational scope so that multiple window browsers open to the same site in the same session will not cross data boundaries.

Gracelets comes with a factory framework, and provides 3 basic factories that allow you to generate JSF Data Model's, Select Item lists or generic data/beans when the target variable has no current value in the context scope that the factory is setup for.

The following shows how you could use each one of these basic factories.

Data Model Factory


Toggle Line Numbers
1 DataModel("someDataModel", "session") { [1, 2, 3] } 
2  
3 h.dataTable(value: { someDataModel }, var: "row") { 
4     h.column(facets: [header: { print "Value" }]) { print { row } } 
5 } 
As can be seen above we define a Data Model tieing it to the session scoped variable 'someDataModel'. When that variable is null, not defined, in the session scope, then the provided closure will be used to initialize its value. The DataModel() factory will take many return types (lists, sets, maps, arrays) and wrap them with valid JSF DataModel implementations. Of course if you return a data model it will simply pass that on as well.

Select Items Factory


Toggle Line Numbers
1 SelectItems("someList") { SomeEnum.values() } 
2  
3 ... 
4     h.form { 
5         h.selectOneMenu(value: { someList }, converter: { SomeEnum.valueOf(it) }) 
6     } 
7 ... 
Above we define a factory that will take lists, array's, map's or single objects and generate a JSF List array so that they can be used with components that use select items (selectOneMenu, selectOneRadio, etc.,). We do not specify a scope here, since it will default to the conversation scope.

Generic Factories


Toggle Line Numbers
1 import some.package.Bean 
2  
3 Factory("someBean", "request") { new Bean() } 
4  
5 ... 
6     h.form { 
7         h.inputText(value: Value { someBean.someProperty }) 
8     } 
9 ... 
Above we make sure that during the request event there is a someBean variable initialized with an instance of our Bean class. This will ensure that the input components do not hit a null object when trying to get their value.