Tuesday, May 31, 2016

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).

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).

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).
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".
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).

Figure 5:  Create JSF Pages from Entity Classes

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

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).

Figure 7:  JavaServer Faces Configuration


Figure 8:  Completed JSF Application

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

Figure 9:  Add Maven Dependency

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












No comments:

Post a Comment

Please leave a comment...