Integration of Struts2 with Spring and Hibernate
My this Post is regarding the integration of struts 2 with spring and hibernate. Struts 2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action.Struts2 supports annotation based configurations which are easy to create and more intuitive.Hibernate ORM (Hibernate in short) is an object-relational mapping library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database.for Integration of Hibernate with Struts we have have used the spring .because Spring helps development teams everywhere build simple, portable,. fast and ... Keep it portable – Spring-based apps run anywhere the JVM Does
Struts 2 <--(ContextLoaderListener)--> Spring <--(LocalSessionFactoryBean) --> Hibernate
Our Goal
Our goal is to create a basic Struts2+spring+hibernate application with a Add Student page. User will enter Student data after the click on Save form will submitted the form data will be saved to student table
Getting Started
Let us start with our first Struts2 +Hibernate+Spring based application.
Open Eclipse and goto File -> New -> Project and select Maven Project in the New Maven wizard screen
Open Eclipse and goto File -> New -> Project and select Maven Project in the New Maven wizard screen
Choose the Default Work space Directory Location and Select Create a Simple Project. then Click on the Next Screen.
The Next Option
Enter the Group Id. group Id is Your application Project Package Structure. and the Artifact Id is the
Project Name and Select Package as War. Fill the Name and Description for your Application. and remain other filled as Empty then Click on the Finish
Your Project Setup is ready for the Development
below screen is related the final Structure of your Application in the src/main/java/ is the package and all java file and src/main/web apps/ it will have all the .jsp pages, css, jquery and image file . and src/main/resources/ it have all classpath file,like .xml file spring and struts configuration files,and database properties file and it has also hibernate configuration file
Root Dirctroy have the pom(Project Object Model).xml file
Firstly Create the com.java.tutions.model package create the pojo(plain old java object) class of Student Entity.we used the Annotation configuration for the hibernate. Create DAO Interface and DAO Implementation class under the com.java.tutions.dao package. for Handling the database Operation using Hibernate. Final Service Interface and Service Interface's implementation class for the handling the Hibernate Transaction.
Stereotype Action
Stereotype annotations are markers for any class that fulfills a role within an application. This helps remove, or at least greatly reduce, the Spring XML configuration required for these components.
This Anotations is for the of Action Class
Action Class
We will need an Action class that will our Student Data and holds the value for student model For this we will create a package com.java.tutions.action. in the java Folder. This package will contain the action file.Action also Implement the ModelDriven Interface.
Create a class called StudentAction in com.java.tutions.action. package with following content.
Create a class called StudentAction in com.java.tutions.action. package with following content.
Student Form
We will create one JSP files to render the output to user. webapp/student/addStudent.jsp. it will be the starting point of our application which will contain a simple add form with title and name surname,password,email.
webapp/student/addStudent.jsp
WEB-INF/web.xml file
Configure the Web Application Deployment Descriptor (web.xml) file to integrate Struts2 to your web project.
Create Stuts.xml File
Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loaded from the classpath of the project. We will define struts.xml file in the resources folder. Create file struts.xml in resources folder.
resources/struts.xml file
Create the Configuration File Like This
DataSource.xml
this file read the database.properties file for creating the datasoruce for the database operation
HibernateSessionFactory.xml
this file provide the sessionfacory object and the transaction object
Database.property File
This file contains all the database credential like dialect ,and username,password,database name
This file contains all the database credential like dialect ,and username,password,database name
SpringBean.xml File
Pom.xml
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. this file contains all dependency and repository of the project
useful information, thanks for this post
ReplyDeleteweb designing