Last Weekend I tried to integrate Struts 2 Spring 3 & Hibernate in Simple Project.
Let's see the program structure implemented in Eclipse as given below:
Requirements for implementation:
Eclipse Java EE IDE for Web Developers.
Version: Kepler Release
Download Link: http://www.eclipse.org/downloads/index-developer.php
Create a new Dynamic Web Project in Eclipse.
Use MySQL for creating database. Before starting the project, create a new database named "student".
Create a new Dynamic Web Project in Eclipse.
Use MySQL for creating database. Before starting the project, create a new database named "student".
index.jsp
From index.jsp, request goes to the web.xml and from there through DispatchFilter, It is submitted to struts.xml(Struts-Config.xml).
web.xml
Here is the the struts configuration file(Controller of application) having actions and validations inside.
struts.xml
struts.xml has a action tag which is mapped with the jsp form action and then load the class defined in the class attribute and call method defined in method attribute.In this case, "web.struts.StrutsAction" class and "authenticateStudent" method. if we dint specified any method then by default calls "execute" method.
Here is the action class,
StrutsAction.java
package web.struts;
import java.lang.Object;
import java.util.Arrays;
import javax.mail.Session;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import hibernate.spring.StudentDao;
import hibernate.Demo.Student;
public class StrutsAction {
private String message;
private String userName;
private String passWord;
private String passWord1;
private String message1;
private String message2;
private String message3;
private String name;
private int age;
private double mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
public StrutsAction() {
}
public String authenticateStudent()
{
ApplicationContext factory = new ClassPathXmlApplicationContext("hibernate-spring.xml");
StudentDao studentDao = (StudentDao)factory.getBean("studentDao");
Student stuResult = studentDao.getStudent(getUserName());
if(stuResult!=null && stuResult.getPassword().equalsIgnoreCase(getPassWord()))
{
setMessage("Hello " + stuResult.getName());
setMessage1("Name : " + stuResult.getName());
setMessage2("Age : " + stuResult.getAge());
setMessage3("Mark : " + stuResult.getMark());
return "SUCCESS";
}
return "ERROR";
}
public String RegisterStudent()
{
ApplicationContext factory = new ClassPathXmlApplicationContext("hibernate-spring.xml");
Student s = new Student();
s.setUserID(getUserName());
s.setName(getName());
s.setAge(getAge());
s.setMark(getMark());
s.setPassword(getPassWord());
StudentDao studentDao = (StudentDao)factory.getBean("studentDao");
Student stuResult = studentDao.saveOrUpdate(s);
if(stuResult!=null)
{
setMessage("Hello " + stuResult.getName());
setMessage1("Name : " + stuResult.getName());
setMessage2("Age : " + stuResult.getAge());
setMessage3("Mark : " + stuResult.getMark());
return "SUCCESS";
}
return "ERROR";
}
public String SearchStudent()
{
ApplicationContext factory = new ClassPathXmlApplicationContext("hibernate-spring.xml");
StudentDao studentDao = (StudentDao)factory.getBean("studentDao");
Student stuResult = studentDao.getStudent(getUserName());
if(stuResult!=null)
{
setMessage("Hello " + stuResult.getName());
setMessage1("Name : " + stuResult.getName());
setMessage2("Age : " + stuResult.getAge());
setMessage3("Mark : " + stuResult.getMark());
return "SUCCESS";
}
return "ERROR";
}
public String logout()
{
return "SUCCESS";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getPassWord1() {
return passWord1;
}
public void setPassWord1(String passWord1) {
this.passWord1 = passWord1;
}
public String getMessage1() {
return message1;
}
public void setMessage1(String message1) {
this.message1 = message1;
}
public String getMessage2() {
return message2;
}
public void setMessage2(String message2) {
this.message2 = message2;
}
public String getMessage3() {
return message3;
}
public void setMessage3(String message3) {
this.message3 = message3;
}
HibernateSessionFactory.xml
DataSource.xml
Here we are using "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" which is to map key-variable in .properties file. For that purpose you need to do this entry into the .classpath file as given below,
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=root
This is upto the configuration, Now for application we are adding one hibernate entity mapping (*.hbm.xml) file.
student.hbm.xml
Student.java
package hibernate.Demo;
public class Student {
private String userID;
private String password;
private String name;
private int age;
private double mark;
public Student() {
}
public String getUserID(){
return userID;
}
public void setUserID(String userID){
this.userID = userID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public double getMark(){
return mark;
}
public void setMark(double mark){
this.mark = mark;
}
public String toString(){
return "Id = " + userID + ", Name = " + name + ", Age = "
+ age + ", OverallMark = " + mark;
}
}
Now we have to create one DAO for the accessing this entity through spring,
StudentDao.java
package hibernate.spring;
import hibernate.Demo.Student;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class StudentDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate()
{
return hibernateTemplate;
}
public Student getStudent(final String userID)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)throws HibernateException,SQLException
{
Query query = session.createQuery("from Student"); // Student denotes the Student.java
List stud = query.list();
Iterator iterator = stud.iterator();
while(iterator.hasNext())
{
Student s = (Student) iterator.next();
if(s.getUserID().equalsIgnoreCase(userID))
{
return s;
}
}
return null;
}
};
return (Student) hibernateTemplate.execute(callback);
}
public Student saveOrUpdate(final Student s)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)throws HibernateException,SQLException
{
session.saveOrUpdate(s);
return session.load(Student.class, s.getUserID());
}
};
return (Student)hibernateTemplate.execute(callback);
}
}
Now, if the struts.xml action tag we have specified the action class and we do actions through the spring-hibernate configuration, and if we get an "SUCCESS" as a output from the StrutsAction then we redirecting the same request to the success.jsp page or if we get an "ERROR" in logging in we forward the same request to the registration.jsp which is registration form for student. we are again saving this information to the database through spring hibernate configuration.
registration.jsp
Through "gotoStrutsRegistration" we submit this form to struts.xml to registration function defined in the StrutsAction.java
The last one success.jsp, no need to explain more about this one.
success.jsp
Output:
index.jsp
If the user login is not successful , then registration.jsp page will be displayed. If the user is already a member, then success.jsp page will be displayed.
registration.jsp
After registering, success.jsp page will be displayed.
success.jsp
Search functionality is also provided to search other users.
Thus we have implemented the Struts 2, Spring 3, Hibernate integration in simple java application.
Thank you,
Prasanna.A
For any help or in case of any query, please mail me prasannaalaguraj@gmail.com
Wonderful Blog!!! Your post is very informative about JAVA technology. Thank you for sharing the article with us.
ReplyDeleteJAVA J2EE Training in Chennai | JAVA J2EE Training Institutes in Chennai