David has posted 1 posts at DZone. View Full User Profile

Let NetBeans IDE 6.8 Generate JSF 2.0 Pages from Database

  • submit to reddit

Continuing the series of articles by the NetBeans Java EE team started by team manager Petr Jiricka recently, Java EE team architect and lead engineer David Konecny introduces the rewritten JSF page generation wizard that will be part of the upcoming NetBeans IDE 6.8 release.

One of the new features recently added to the daily development builds of the upcoming NetBeans IDE 6.8 is rewritten generation of JSF pages. After you select tables from a database, a wizard generates persistence entity classes, session enterprise beans, Java Server Faces pages (that facilitate data viewing and changing), and the required Java Server Faces managed beans to glue the visual layer to the data layer.

The generated code takes advantage of the new Java EE 6 features, specifically the following:

  • the Criteria API in the Java Persistence API 2.0
  • the ability to package and deploy enterprise session beans within a web application module (.war file) in Enterprise JavaBeans 3.1
  • Facelets as the default page definition language in Java Server Faces 2.0

This article describes the wizard and the generated files in detail.


The origins of this wizard go back to NetBeans IDE 5.5 when senior NetBeans developer Pavel Buzek decided it would be a good demonstration of Java EE 5 (specifically JPA and JSF) to generate the scaffolding of an application from a database and illustrate how these technologies can be used together. Since then, this feature was periodically improved, and sometimes neglected, with NetBeans IDE 6.8 being the latest instance of its improvement.

The idea of generating scaffolding is useful, but the question is always: "How customizable, updatable, and practical is the generated scaffolding, really?" Is it just demoware, nice for technology evangelists to impress conference attendees about how quickly a whole CRUD application can be generated from a database? Or does it produce real quality code that users can build their applications upon and learn best practices from? Or can it be both?

For NetBeans IDE 6.8, it was decided to update this wizard because:

  • it can illustrate best practices and help users to learn how to put individual Java EE technologies together within a sound architecture
  • it can help to rapidly build end-to-end applications (JSF, EJB, JPA)
  • it can eliminate/simplify the mundane tasks associated with database-based applications, such as listing table fields in JSF forms/tables
  • it is not only demoware, although it could also be used for demonstrations if it makes sense in the context of the presentation, since it is a really cool feature

However, we need to acknowledge that:

  • it is not going to be yet another web framework - whatever is generated should be plain-vanilla JSF and Java code
  • it cannot be comprehensive - there are zillions of ways how to write web applications, how to separate data layers from UI layers, zillions of different JSF components are available, etc. A wizard can never fully satisfy all users.

Java Server Faces Generation

Two independent wizards are involved in generating application scaffolding:

  • Entity Classes from Database wizard
  • JSF Pages from Entity Classes wizard

To see what is generated by these wizards, have a look at the attached example of generated sources. Or try it live in the latest development build of NetBeans IDE. (Or wait a couple of days for NetBeans 6.8 Beta release.)

  1. Create a new Web Application project for GlassFish v3 server, which comes with NetBeans IDE 6.8, and use the Frameworks panel to add the JSF 2.0 framework.

  2. Use your database (or sample database which comes with GlassFish) to generate entity classes from database by right-clicking the project node and choosing New | Entity Classes from Database.

  3. Once you have some entity classes, you can generate JSF pages for them by right-clicking the project node and choosing New | JSF Pages from Entity Classes:

JSF Page From Entity wizard

In the screenshot on the right, wizards were used to generate an entitfiles generated by wizardy class and JSF pages for an ADDRESS table from the GlassFish sample database. As a result, the following files were generated:

  • com.something.data.Address - JPA entity class for ADDRESS table from sample database
  • com.something.data.AddressFacade - stateless session bean for creation, retrieval, modification and removal of Address entities
  • com.something.ui.AddressController -  session scoped managed bean for address related JSF pages
  • address/Create.xhtml - form for creating new address
  • address/View.xhtml - form for viewing address details
  • address/Edit.xhtml - form for editing an address
  • address/List.xhtml - table for scrolling through addresses
  • Bundle.properties - localizable Strings used from above forms and table

The controller class and JSF form files are generated using customizable file templates. These templates can be opened for modification directly from the wizard using the Customize Templates link or via the Templates customizer from the Tools menu in the main menubar. Look for the template category JavaServer Faces/JSF From Entity. Each template starts with a list of fields available in the template, together with their description.

FreeMarker is used as the templating language and its documentation should be consulted for questions about its syntax.

Form/Table Generation

Apart from generating complete scaffoldings, it is also possible to generate only a form or a table for an entity class. While editing JSF pages, try the "Insert Code" action, accessible from the Source menu in the main menu bar or via the Alt+Insert shortcut. Or try the regular code completion (type jsf and then CTRL+Space) to insert JSF Data Table From Entity or JSF Form From Entity:

inserting component palette item

A dialog listing all the available entity classes is shown above. Selecting one of the entity classes populates the "Managed Bean Property" drop-down with suitable managed beans, that is, is managed beans which have getter(s) with the return value of the selected entity.

Clicking OK above enables the customizable file template to generate code by enumerating all the fields from the given entity. These templates can be opened directly from the wizard or from the Templates customizer, via the category JavaServer Faces/JSF Data/Form From Entity.

Future Directions

This is an early preview and some parts of these features are still being polished. Any feedback or suggestions are always welcome, either here in the comments to this article, or directly via the NetBeans bug tracking database (choose "web" component and "jsf" subcomponent).

It is also more of a first step in a new direction, rather than the final feature set, as there is a lot of room for further improvements. For example, additional variables could be made available in the templates for better customization, and additional Java EE 6 technologies could be added to the templates, such as WebBeans. There is also an opportunity for contributions from NetBeans users or JSF component providers. While not supported right now, it would not be difficult to expand the current solution and allow for switching between sets of templates, such as NetBeans vanilla JSF templates, ICEFaces templates using ICEFaces JSF components, and user custom per-project specific templates.


MyWebApplication.zip27.65 KB
wizard.png20.69 KB
component.png44.1 KB
files.png18.82 KB
Published at DZone with permission of its author, David Konecny.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Bryan Low replied on Fri, 2013/02/01 - 10:33pm

It is also near to Singapore Sports School and Innova Junior College.For vehicle owners, Twin Fountains ECtakes less than 30 minutes to drive to the business hub and vibrant Orchard Road shopping district, via Central Expressway (CTE) and Seletar Expressway (SLE)A wonderful and unique lifestyle awaits you. Please see Twin Fountains EC project details and floor plans for more information.Twin Fountains Location

Bryan Low replied on Tue, 2013/04/23 - 2:38am

Great Post. I have not been visiting the site recently. Took a visit again and there were some great comments on the site. Excellent post. Keep up the good work.Punggol EC

Bryan Low replied on Tue, 2013/05/14 - 7:27am

For vehicle owners, it takes less than 25 minutes to drive to the business hub and vibrant Orchard Road shopping district via Ayer Rajah Expressway (AYE). A wonderful and unique lifestyle awaits you. Please see J Gateway project details and units available for more information. J Gateway Site Plan

Bryan Low replied on Sat, 2013/05/25 - 12:44am

For vehicle owners, it takes less than 30 minutes to drive to the city area, via Central Expressway (CTE) and KPE (Kallang-Paya Lebar Expressway). A wonderful and unique lifestyle awaits you. Please see Jewel at Buangkok project details and units available for more information. Buangkok Project Details

Bryan Low replied on Tue, 2013/06/04 - 12:58am

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Bugis. Several buses are available near Rocher Road and Beach Road along with shopping centers and restaurants. DUO Residences

Bryan Low replied on Mon, 2013/06/17 - 1:11am

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Sengkang. Sengkang West Way EC

John Brown replied on Wed, 2013/07/24 - 2:52am

 Very great application, the pages created from this application is very great. I like it very much.

Pure oud oil

Jurongwestcondo... replied on Tue, 2013/10/08 - 11:04pm

New Condo in Jurong is a 99-years leasehold Jurong West New Launch Condo development located at Jurong West Street 41. new Jurong West Condo location

The Hillford Th... replied on Wed, 2013/10/09 - 9:24pm

Jalan Jurong Kechil Condo will be accessible via the upcoming Beauty World MRT station on the Downtown Line. Commuting to the Bukit Timah Area as well as the city area is therefore very convenient. It is also near to Ten Mile Junction, Rail Mall, Beauty World, King Albert's Park, West Mall, Bukit Panjang, Cold Storage, and many more. bukit timah plaza new condo

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.