Struts2 Hibernate Example With XML | Code Factory

Code Factory
2 min readNov 29, 2019

--

Reference Link : Link

Download Code and Jars : Link

File : index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<br>
<br>
<form action="insertData" method="post">
First Name :
<input type="text" name="firstName">
<br>
<br> Last Name :
<input type="text" name="lastName">
<br>
<br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>

File : struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<include file="struts-default.xml" />
<package name="HibernateWithXML" extends="struts-default,json-default">
<action name="insertData" class="com.codeFactory.action.StoreData">
<result name="success">index.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>

File : web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>HibernateWithXML</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

File : StoreData.java

package com.codeFactory.action;import org.hibernate.Session;
import org.hibernate.Transaction;
import com.codeFactory.bean.Employee;
import com.codeFactory.connection.HibernateConnection;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class StoreData extends ActionSupport implements ModelDriven < Employee > {private Employee emp = new Employee();
HibernateConnection hbmCon = new HibernateConnection();
public String execute()
{
Session session1 = hbmCon.getHbmConnection();
Transaction t = session1.beginTransaction();
session1.persist(emp);
t.commit();
session1.close();
System.out.println("saved.");
System.out.println("first name : " + emp.getFirstName());
System.out.println("last name : " + emp.getLastName());
return SUCCESS;
}
@Override
public Employee getModel() {
return emp;
}
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
}

File : Employee.java

package com.codeFactory.bean;public class Employee {private int id;
private String firstName, lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

File : HibernateConnection.java

package com.codeFactory.connection;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateConnection {Configuration cfg = new Configuration();
SessionFactory factory;
Session session1;
public Session getHbmConnection()
{
cfg.configure("hibernate.cfg.xml");
factory = cfg.buildSessionFactory();
session1 = factory.openSession();
return session1;
}
}

File : employee.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.codeFactory.bean.Employee" table="employee">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>

File : hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration>

--

--

No responses yet