In this example, We will discuss about the Hibernate Integration using struts2.2.1. In this example,we take the input from the user by a form add save the detail in the database using hibernate.The action mapping is in the struts.xml file.
![]() |
Jar files Used in the application
![]() |
1-SignUp.jsp
|
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>RegisterUser - Struts2 Hibernate Example</title> <s:head /> </head> <body> <h1>User Registration</h1> <s:actionerror /> <s:form action="register" method="post"> <s:textfield name="name" label="Name" /> <s:textfield name="emailId" label="Email" /> <s:password name="password" label="Password" /> <s:textfield name="cellNo" label="Cell No." /> <s:textfield name="website" label="Homepage" /> <s:textfield name="birthDate" label="Birthdate" /> <s:submit value="Register" align="center" /></s:form> </body> </html> |
2-SignUpSuccess.jsp
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Registration Successful</title> </head> <body> <h1>Registration Successful</h1><hr> Welcome<s:property value="name" /><br> User name :<s:property value="emailId" /><br> Password : <s:property value="password" /> </body> </html> |
3-SignUpFail.jsp
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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=UTF-8"> <title>Registration Failed</title> </head> <body> <h1>Registration Failed</h1> <a href="SignUp.jsp">SignUp Again</a> </body> </html> |
4-RegisterAction.java
|
package roseindia.view; import java.sql.Date; import roseindia.dao.RegisterDao; import roseindia.model.Register; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private static final long serialVersionUID = 908606616890722294L; private String emailId, password, name, cellNo, website; private Date birthDate; private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCellNo() { return cellNo; } public void setCellNo(String cellNo) { this.cellNo = cellNo; } public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public RegisterAction() { } public String execute() throws Exception { Register Rgst = new Register(); Rgst.setCellNo(cellNo); Rgst.setWebsite(website); Rgst.setBirthDate(birthDate); Rgst.setEmailId(emailId); Rgst.setName(name); Rgst.setPassword(password); if (RegisterDao.registerUser(Rgst)) return "success"; else return "fail";} } |
5 -Register.java - This class is used to mapped the Register table in the Mysql database, we use the annotation to provide metadata to the class and fields of the table.
|
package roseindia.model; import java.io.Serializable; import java.sql.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "Register") public class Register implements Serializable { private static final long serialVersionUID = -6430275524165380638L; private Long id; private String name; private String emailId; private String password; private String cellNo; private String website; private Date birthDate; @Id @GeneratedValue @Column(name = "id")public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "emailId") public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } @Column(name = "password") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name = "cellNo") public String getCellNo() { return cellNo; } public void setCellNo(String cellNo) { this.cellNo = cellNo; } @Column(name = "website") public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } @Column(name = "birthDate") public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } } |
6 - RegisterDao.java - This class is based on DAO (Data Access Object ) design pattern and used for accessing the hibernate from action.
|
package roseindia.dao; import roseindia.dao.HibernateUtil; import roseindia.model.Register; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; public class RegisterDao { public static boolean registerUser(Register Rgst) { SessionFactory sf = HibernateUtil.getSessionFactory(); Transaction t = null; try { Session s = sf.openSession(); t = s.beginTransaction(); // start a new transaction s.persist(Rgst); t.commit(); // commit transaction return true;} catch (Exception ex) { System.err.println("Error -->" + ex.getMessage()); if (t != null) t.rollback(); return false; } } } |
7 - HibernateUtil.java - This class creates the SessionFactory object and return it. The method AnnotationConfiguration() is used to get the support of annotation for hibernate.
8 package.properties
|
requiredstring = ${getText(fieldName)} is required. requiredemail = ${getText(fieldName)} is not in correct format. requiredpassword = ${getText(fieldName)} should be more than 6 character. requiredinteger = ${getText(fieldName)} should be numeric. password = Password name = Name emailId = Email Address website = Website cellNo = CellNo |
9 struts.xml - In this file we do the action mapping.
|
<?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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="default" extends="struts-default"><action name="register" class="roseindia.view.RegisterAction"> <result name="input">/SignUp.jsp</result> <result name="success">/SignUpSuccess.jsp</result> <result name="fail">/SignUpFail.jsp</result> </action></package> </struts> |
10 hibernate.cfg.xml - The hibernate.cfg.xml file is hibernate configuration file and it is used to configure the Register table in the gyan database.
|
<?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> <!-- Database connection settings --> <property name="connection.driver_class"> com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/gyan </property> <property name="connection.username">root</property> <property name="connection.password">root</property><!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property><!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property><!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider </property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property><mapping class="roseindia.model.Register" /> </session-factory> </hibernate-configuration> |
11- RegisterAction-Validation.xml
|
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN""http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="trim">true</param> <message key="requiredpassword" /> </field-validator> </field> <field name="emailId"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> <field-validator type="email"> <message key="requiredemail" /> </field-validator> </field> <field name="website"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> </field> <field name="cellNo"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> <field-validator type="stringlength"> <param name="minLength">10</param> <message key="requiredinteger" /> </field-validator></field> </validators> |
SignUp.jsp

required.gif

Incorrect.gif

Success.gif

DatabaseTableData.gif
