June 25, 2017

CRUD functionality with Struts MVC and Hibernate

In a previous article I’ve explaned how to integrate the Hibernate framework in a Struts-MVC web application. Now I will show how to use Hibernate to implement the CRUD functionality for the Customer domain object.

Follow the main Struts-MVC objects that I will use:

ActionForm: to maintains the session state for web application; ActionForm object is automatically populated on the server side with data entered from a form on the client side
DispatchAction: to group a set of Customer related actions like create, update, read and delete (CRUD) into a single action called CutomerAction
ActionForward, ActionMapping: to integrate Struts controller servlet’s functionality with the business logic component and the Velocity pages of Customer.

CHECK OUT THE SOURCE CODE ON GITHUB

Create ActionForm object for the Customer

– CustomerForm

Define the form bean in struts-config.xml

Now let’s see how to configure the Controller layer for the Customer using the “org.apache.struts.action” components. Create the CustomerAction class which extends the Struts DispatchAction. The CustomerAction class contains the action methods for the CRUD functionality.

– CustomerAction

Configure the Customer action mapping in the struts-config.xml. Note the use of parameter=”action” definition used to specify the ActionForward which is called in the http request (i.e.  http://localhost:8080/demo/customers.do?action=multiRetrieve)

We now proceed to the configuration of the View layer using the Velocity template engine. For this we must create a page to show a list of Customers and a second page to read and update an existing Customer (or to create a new one).

– WebContent/pages/customers.vm

– WebContent/pages/customer.vm

Here the project layout

 crud-struts-hibernate-project-layout

 

Related posts

Leave a Reply

Your email address will not be published.