This is a demo Contact Manager application that we will create using Struts2 and Hibernate framework. In this article we will see how we can use Hibernate to perform Insert / Delete operations in Struts2 framework.
Our goal will be to demonstrate the use of Struts2 with Hibernate framework and to create a demo application “Contact Manager”. The basic requirement of the Contact Manager app will be:
Add new contact in the contact list.
Display all contacts from contact list.
Delete a contact from contact list.
Once we will build the application it will look like:
For our Contact Manager example, we will use MySQL database. Create a table contacts in any MySQL database. This is very preliminary example and thus we have minimum columns to represent a contact. Feel free to extend this example and create a more complex application.
Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
After selecting Dynamic Web Project, press Next.
Write the name of the project. For example ContactManager. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.
We will need a source folder called resources. Right click on Project in project explorer and select New -> Source Folder and create a folder with name resources.
Also we will create Java packages for our application. As we will use Struts2, we will follow MVC architecture. Create 4 packages in the sources.
We created 4 new packages. The net.viralpatel.contact.controller will hold the Java class that will act as controller and will fetch the data from database and pass it to view. The net.viralpatel.contact.model package will hold the Hibernate persistent model class. The net.viralpatel.contact.view will contain the struts2 action class. And finally the net.viralpatel.contact.util will have some hibernate related util file that will be see shortly.
Required JAR Files
Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder. Create this folder if it does not exists.
Create JSP for Contact Manager
We will need only one JSP file for this tutorial. The JSP will include a form to add new contact as well as will list the contacts at the end. Create a JSP file index.jsp in WebContent folder and copy following content into it.
For adding hibernate support, we will add following source code in Contact Manager application.
hibernate.cfg.xml – This is the Hibernate configuration file. This file will contain configurations such as database connection information, persistence class info etc. Create hibernate.cfg.xml under resources folder and copy following content into it.
Note how we have mapped Contact class with Contacts table using Java persistence API annotations.
Adding Controller to access data
We will add a controller class in Contact Manager application which will be used to get/save data from hibernate. This controller will be invoked from Struts action class. Create a file ContactManager.java under net.viralpatel.contact.controller package and copy following content into it.
Note that how we have created different methods in controller class to add/delete/list the contacts. Also the ContactManager class is extending HibernateUtil class thus allowing it to access sessionFactory object.
Adding Struts2 Support
Let us add Struts2 support to our web application. For that, will add following entry in deployment descriptor (WEB-INF/web.xml).
Up-till now we have almost completed our Contact Manager application in Struts2 and Hibernate. Only task left is to add Struts Action class. Create a class ContactAction.java under net.viralpatel.contact.view package and copy following content into it.
ContactAction class contains different methods that gets called by Struts2. The execute() method is the default method which gets called when we call /index action from browser. It fetches the list of contacts and display it in index.jsp. Similarly, when a new contact is added, add() method is called. If you check the action mapping entry in struts.xml for add() method, the <result> is mapped with /index action and the type is chain. This is because we want to display the list of contact once we add a new one. Hence we have done Action chaining and called /index action after /add action.
The Contact Manager App
That’s it. The app is ready, just compile and run the project in Eclipse Run -> Run As -> Run on Server. Set the URL to:
Fill the contact form and hit enter and the new contact will be persisted in database and will be shown in below table. Similarly, click on delete link next to a record. It will delete the record from database.
Let me know your input about this application.