May 22, 2017

How to create a MVC web application using Struts Hibernate and Velocity

In this article I’ve provided a basic tutorial to create a java web-project skeleton based on the MVC (Model View Controller) design pattern. This pattern is used to separate and stratify the logic of a web application.

Model represents an object or java POJO carrying data. It can also have logic to update controller if its data changes.
View represents the visualization of the data that model contains.
Controller acts on both Model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps View and Model separate.

I’ve based this tutorial on the following three java technologies:
Apache Struts: the MVC framework of the Apache Foundation
Hibernate: the ORM framework for the persistence layer
Apache Velocity: the template engine framework used to reference objects defined in Java code (a viable alternative to Java Server Pages)

I’ve reused previous articles in which I have alredy explained how to configure the Apache Velocity template engine and the persistence layer based on the Hibernate framework. The tutorial covers the creation of a demo web project called “sample-struts” in the Eclipse IDE . The sample-struts project implements the MVC pattern for a hypotetichal Customer business domain.

CHECK OUT THE SOURCE CODE ON GITHUB

ROADMAP

STEP 1 – Create a web project in Eclipse, configure Struts-MVC and Velocity
STEP 2 – Basic configuration of Hibernate framework whitin the web project
STEP 3 – Enhance of Hbernate, the DAO-Factory design pattern
STEP 4 – Java POJO and Hibernate mapping
STEP 5 – CRUD functionality

STEP 1 – Create a web project in Eclipse, configure Struts-MVC and Velocity

Create a struts-blank web project in Eclipse and configure the Velocity engine. To do this follow this article (I’ve called the web project as “sample-struts” ). If no errors occur you can see the Velocity test page:

velocity-test-screen

Add now a default welcome page to map the base web context http://localhost:8080/sample-struts. You can see also a link to a Customer list, the hypotethical object representing the business model layer

– WebContent/pages/index.vm

 

Define the welcome page in WebContent/WEB-INF/web.xml

Here the output of the welcome page on http://localhost:8080/sample-struts

struts-hibernate-velocity-index

STEP 2 – Basic configuration of Hibernate framework whitin the web project

Now go to this article and add a basic configuration of the Hibernate framework whitn the web project. If no errors occur you can see the Hibernate session factory creation at Tomcat startup.

hibernate-build-session-factory

STEP 3 – Enhance of Hbernate, the DAO-Factory design pattern

We proceed with an advanced hibernate configuration integrating the DAO-Factory design pattern within the web project. To do this follow this article. Here the partial project layout.

dao-factory-hibernate-project-layout

STEP 4 – Java POJO and Hibernate mapping

Continue with the POJO object definition and the ORM Hibernate mapping. Follow this article, you should have this new project layout.

pojo-hibernate-project-layout


STEP 5 – CRUD functionality

Implement the CRUD functionality within the web project following this article. If no errors occur finally you can perform the retrieve, insert, update and delete actions for the Customer domain object. Here some screenshots.

– Multi Retrieve

struts-hibernate-velocity-multi-customer

– Single retrieve ,update and create

struts-hibernate-velocity-single-customer

 

Related posts

Leave a Reply

Your email address will not be published.