November 11, 2010 by huionn
Scaffolding (aka CRUD pages generation) has been popularized by Ruby on Rails framework. Since then, there are a few Java framework providing this capability, such as Trails, Grails, Spring Roo and JBoss Seam.
I have tried Spring Roo before. At that time, I found that Roo is not mature enough but it has a very innovative feature – code generation with AspectJ ITDs (Inter Type Declarations).
After that I have been focusing on JBoss Seam. It seems that there is no much documentation about seam-gen customization.
One drawback of seam-gen ver2.2 (Seam 3 has been evolving in different path, so I am not sure about it) is the lack of path configuration ‘strategy’ for files generation. In medium to large projects, the files will be put in different folder based on module/package.
On workaround is to use ‘Hibernate Code Generation Configurations…’. ‘Package’ allow package configuration and ‘reveng.xml’ setting can filter the tables to generate. However there is no way to configure path of xhtml files. Therefore, it is recommended to generate files in separate sandbox project and copy the file to actual project manually.
In my project, the page layout is significantly different with seam-gen template. In addition, all pages are fully ajax-enabled – which means there is no page refresh within same page – with JBoss RichFaces.
So, I customized a sample page following the requirement. Then, based on the sample page, I have to customize seam-gen templates.
seam-gen provides very flexible customization at different level of abstraction.
i) In ‘Hibernate Code Generation Configurations’, there is reveng. strategy which usually extends org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy to provide standard naming conventions (Eclipse restart is required for changes to take effect).
ii) The code generation steps are configurable on ‘Exporters’ tab in ‘Hibernate Code Generation Configurations’. I add an additional Exporter for my custom class generation. The Exporter can be configured in Eclipse or xml (<workspace>\.metadata\.plugins\org.eclipse.debug.core\.launches\<project>-generate-entities.launch file directly.
iii) All freemarker templates (*.ftl) in \seam-gen folder can be cloned and modified. It takes little time to understand how the templates work as it is intuitive. The error reporting (freemarker syntax error) is clear and helpful too.
The end result is encouraging – now I can generate a fully functional pages (create, update, search, list, sort, pagination, validation, etc) with single click. By the ways, the columns labels and sequence still need to be modified manually. Special data type such as drop down box also has to be coded manually.
Besides the productivity gain, it also promotes consistency which help simplify software maintenance. Nevertheless, the generated page can only serve as skeleton for more complicated pages where the list and home are for different entities.