before starting please add all required jars for Struts2, Spring3 and Hibernate4,
Here goes the index page which has the action form has to be submit to struts.xml to perform struts action and validations.
spring-hibernate.xml
|
spring-hibernate.xml |
HibernateSessionFactory.xml
|
HibernateSessionFactory.xml |
DataSource.xml
|
DataSource.xml |
Here we are using "
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" which is to map key-variable in .properties file. for that perpose you need to do this entry into the
.classpath file. Given below,
database.properties
jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=HibernateDemo
jdbc.username=sa
jdbc.password=sa
This is upto the configuration,
now for application we are adding one hibernate entity mapping (*.hbm.xml) file
employee.hbm.xml
|
employee.hbm.xml |
related entity to this mapping file is,
Employee.java
package MyDemo.hibernate;
public class Employee {
private String userID;
private String password;
private String name;
private int age;
private double salary;
public Employee() {
}
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 getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public String toString(){
return "Id = " + userID + ", Name = " + name + ", Age = "
+ age + ", Salary = " + salary;
}
}
Now create one DAO for the accessing this entity through spring,
EmployeeDao.java
package spring.hibernate;
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;
import MyDemo.hibernate.Employee;
public class EmployeeDao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate
hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate(){
return hibernateTemplate;
}
public Employee getEmployee(final String userID){
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException,SQLException {
Query query = session.createQuery("from Employee");
List
employees = query.list();
//employees.contains(id);
Iterator iterator = employees.iterator();
while(iterator.hasNext()){
Employee employee = iterator.next();
if(employee.getUserID().equalsIgnoreCase(userID)){
return employee;
}
}
return null;
}
};
return (Employee) hibernateTemplate.execute(callback);
}
public Employee saveOrUpdate(final Employee employee){
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException,SQLException {
session.saveOrUpdate(employee);
return session.load(Employee.class, employee.getUserID());
}
};
return (Employee)hibernateTemplate.execute(callback);
}
}
Now, if the struts.xml action tag we have specified the action class in that we are verifying the details with data base 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 page or if we get an "ERROR" in logging in we forward the same request to the registration.jsp which is registration form for employee. on save of registration we are again saving this information to the database through spring hibernate configuration.
registration.jsp
|
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
|
success.jsp |
Welcome to Struts2
Thank you for the visiting here, for more help or in case of any query please mail me at
amol.phasale@gmail.com
Thank you.
Amol Fasale