Enterprise Java

NetBeans Java EE Tip #9: Create JSF Application from Database

Do you need to create an database front-end very quickly?  The NetBeans IDE allows one to develop a JSF application front end for a set of database tables very quickly, and with very little coding.  Now this tutorial is an oldie, but a goodie…and it is worth mentioning again for those who are not aware of it.

First, create your application.  In this case, I create a Maven Web Application by choosing “New Project”->”Maven”->”Web Application” (Figure 1).

netbeans-ee-9a

Figure 1:  Maven Web Application

Next, I provide the web application name of JsfCustomers since I’ll be making use of the CUSTOMERS sample database tables.  Once the application project is created, create a new Java package to hold the entity classes for the database.  In this case, I right-click on the project’s “Source Packages” and select “New”->”Java Package…”, and then name it org.jsf.jsfcustomers.entity  (Figure 2).

netbeans-ee-9b

Figure 2:  New Java Package

After the package is created, right-click on it and select “New”->”Entity Classes from Database”, which I’ve covered in a previous posting.  This wizard allows me to select a set of database tables from one of my connections…here I choose my sample database and select the CUSTOMER table.  After doing so, all related database tables are automatically added to the “Selected Tables” list…click next (Figure 3).

netbeans-ee-9c

Figure 3:  Create Entity Classes from Database

The next wizard page (Figure 4) allows me to select various options for generating my entity classes…I’ll leave the defaults and click “Finish”.

netbeans-ee-9d

Figure 4:  Entity Class Options

After doing so, the entity classes for my database have been generated.  Now it is time to put the icing on the cake.  Since the database entity classes are now available, I can generate the entire JavaServer Faces infrastructure with just a few clicks.  Right click on the project’s “Web Pages” folder and select “New”->”JavaServer Pages”->”JSF Pages from Entity Classes” (Figure 5).

netbeans-ee-9f

Figure 5:  Create JSF Pages from Entity Classes

Next, select all of the entity classes that have been created, and click “Next” (Figure 6).

netbeans-ee-9g

Figure 6:  Select Entity Classes

Lastly, specify options for creating your JSF classes and pages.  First, I specify a good name for my EJB session bean and JSF Controller packages.  The EJBs and controllers will be automatically generated into these locations.  Next, select the web folder where the JSF pages should be generated…leave blank for the root.  Lastly, I chose to utilize the PrimeFaces pages template to make use of PrimeFaces components.  Click next and choose the JSF configuration that has been previously registered with the IDE (Figure 7).  Lastly…click “Finish” and see the magic…the web application and all of the JavaServer Faces infrastructure is generated automatically (Figure 8).

netbeans-ee-9h

Figure 7:  JavaServer Faces Configuration

netbeans-ee-9i

Figure 8:  Completed JSF Application

Note that you may need to right-click on your project “Dependencies” module and add PrimeFaces (Figure 9).

netbeans-ee-9j

Figure 9:  Add Maven Dependency

Now build and run your application…and it is ready to use!

netbeans-ee-9k

Josh Juneau

Josh is an application developer and technical writer. He has authored several books for Apress, primarily focusing on Java development. Most recently, he has authored Java EE 7 Recipes, Introducing Java EE 7, and Java 8 Recipes. Josh is a member of the JCP, and he is on the JSF 2.3 Expert Group.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button