Over a million developers have joined DZone.

Let NetBeans IDE 6.8 Generate JSF 2.0 Pages from Database

DZone's Guide to

Let NetBeans IDE 6.8 Generate JSF 2.0 Pages from Database

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 entit files 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.


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}