To create a struts 2 and hibernate 4 application you require the following libraries of struts 2 and hibernate 4.
1. Struts 2 libraries
- commons-fileupload-1.2.2.jar
- commons-lang-2.4.jar
- commons-lang-3-3.1.jar
- freemarker-2.3.19.jar
- ognl-3.0.5.jar
- struts2-core-2.3.4.1.jar
- xwork-core-2.3.4.1.jar
Download struts 2 libraries from here.
2. Hibernate 4.1.8 libraries
- antlr-2.7.7.jar
- dom4j-1.6.1.jar
- hibernate-commons-annotations-4.0.1.Final.jar
- hibernate-core-4.1.8.Final.jar
- hibernate-jpa-2.0-api-1.0.1.Final.jar
- javassist-3.15.0-GA.jar
- jboss-logging-3.1.0.GA.jar
- jboss-transaction-api_1.1_spec-1.0.0.Final.jar
Download hibernate 4.1.8 libraries from here.
We are using Netbeans IDE and MySQL for this demonstration.
Step 1: Create a web application in Netbeans.
Go to File >New Project>Java Web>Web Application>fill name and location field>Next>Select Server>finish
Now your project is created and display in project tab.

Structure of Project
Now follow these steps……
Step 2: Add all the above jar files in Libraries folder .
Step 3: Create a hibernate.cfg.xml file in Source Packages.
Step 4: Enter the following code in hibernate.cfg.xml.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping resource="entity/Login.hbm.xml"/> </session-factory> </hibernate-configuration>
Step 5: Create a hibernate.reveng.xml in Source Packages.
Step 6: Enter the following code in hibernate.reveng.xml file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd"> <hibernate-reverse-engineering> <schema-selection match-catalog="test"/> <table-filter match-name="login"/> </hibernate-reverse-engineering>
Step 7: Create a entity package in Source Packages.
Step 8: Create a Login.java file in entity package and enter the folowing code.
package entity;
import java.util.Date;
public class Login implements java.io.Serializable {
private int idLogin;
private String username;
private String password;
public Login() {
}
public Login(int idLogin) {
this.idLogin = idLogin;
}
public Login(int idLogin, String username, String password) {
this.idLogin = idLogin;
this.username = username;
this.password = password;
}
public int getIdLogin() {
return this.idLogin;
}
public void setIdLogin(int idLogin) {
this.idLogin = idLogin;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}Step 9: Create a Login.hbm.xml and enter the following code.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 28, 2012 4:05:29 PM by Hibernate Tools 3.2.1.GA --> <hibernate-mapping> <class name="entity.Login" table="login" catalog="test"> <id name="idLogin" type="int"> <column name="idLOGIN" /> <generator class="assigned" /> </id> <property name="username" type="string"> <column name="USERNAME" length="45" /> </property> <property name="password" type="string"> <column name="PASSWORD" length="45" /> </property> </class> </hibernate-mapping>
Step 10: Create a NewHibernateUtil.java in entity package and enter the following code.
package entity;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class NewHibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
Configuration configuration=new Configuration();
configuration=configuration.configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}Step 11: Now create a actionsupportpackage package in Source Packages.
Step 12: Create a LoginActionSupport.java file in this package and enter the following code.
package actionsupportpackage;
import com.opensymphony.xwork2.ActionSupport;
import entity.Login;
import entity.NewHibernateUtil;
import org.hibernate.Session;
public class LoginActionSupport extends ActionSupport {
String userName,password;
Session hibernateSession;
Login login;
public String execute() throws Exception {
hibernateSession=NewHibernateUtil.getSessionFactory().openSession();
hibernateSession.beginTransaction();
System.out.println("session get");
if(userName!=null && password!=null &&(!userName.equals(""))&&(!password.equals(""))){
login=(Login) hibernateSession.createQuery("from Login where userName='"+userName+"'AND password='"+password+"'").uniqueResult();
}else{
addActionError("User Name does not exist");
return INPUT;
}
if(login==null){
addActionError("User Name does not exist");
return INPUT;
}
addActionMessage("Welcome you logined");
return SUCCESS;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}In the above code you find that the LoginActionSupport class entends ActionSupport class which by default implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable and inteface.
Step 13: Create a index.jsp page in Web Pages and enter the following Code.
<%@taglib uri="/struts-tags" prefix="s" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body> <s:form action="doLogin" > <s:textfield name="userName" key="User Name:" /> <s:password name="password" key="Password:" /> <s:submit value="Login" /> <s:actionmessage /> </s:form> </body> </html>
Step 14: Create a Welcome.jsp page in Web Pages and enter following code.
<%@taglib uri="/struts-tags" prefix="s" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome Page</title> </head> <body> <h1>Welcome <s:property value="userName" />!</h1> </body> </html>
Step 15: Create a struts.xml file in default package of Source Packages and enter the following code.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="example.xml"/> <!-- Configuration for the default package. --> <package name="default" extends="struts-default"> </package> <package name="actionsupportpackage" extends="struts-default"> <action name="doLogin" method="execute" class="actionsupportpackage.LoginActionSupport"> <result name="success" >Welcome.jsp</result> <result name="input" >index.jsp</result> </action> </package> </struts>
Step 16: Create a web.xml if not created in Web Pages/WEB-INF folder.
Step 17: Enter the following code in web.xml.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Step 18: Create Database for this demo application
CREATE DATABASE test;
CREATE TABLE `test`.`login` ( `idLOGIN` INT NOT NULL , `USERNAME` VARCHAR(45) NULL , `PASSWORD` VARCHAR(45) NULL , PRIMARY KEY (`idLOGIN`) );
INSERT INTO `test`.`login` (`idLOGIN`,`USERNAME`, `PASSWORD`) VALUES (`1`,'compilr', 'org');
Step 18: Now save and run projcet…..done!
—OUTPUT—

Output of this application