This article is to use hibernate with struts. As when we use hibernate, we have to set values of all fields externally. If we use struts with hibernate then all the values are set by struts. So it helps decreasing the number of lines of code. As well struts also helps in organizing the application
(1) Configure the struts configuration in web.xml. If you don't know how to do so then first read a little about struts. At: Struts Action Example
(2) In struts-config.xml define the action and form.
e.g.
<!-- ========== Form Bean Definitions ================= -->
<form-beans>
<form-bean name="User" type="com.persistence.UserVO"/>
</form-beans>
<!-- ========== Action Mapping Definitions ============ -->
<action-mappings>
<action path="/user" type="com.control.UserController" name="User" input="/home.jsp">
<forward name="success" path="/home.jsp"/>
<forward name="failure" path="/room.jsp"/>
</action>
</action-mappings>
(3) Now create bean class as per the requirement the database as well HTML form. E.g.
import org.apache.struts.action.ActionForm;
public class UserVO extends ActionForm {
private int id;
private String userName;
private String userPassword;
private String userFirstName;
private String userLastName;
private String userEmail;
// ----- getter and setter methods for all fields
}
But keep one thing clear that the fields of bean class must have same name as the fields of HTML form you submit from. Here I created this class as my HTML form is as like below.
<form action="user.do" method="post"><br />
Name:<input type="text" name="username" /> <br />
Password:<input type="password" name="userPassword" /> <br />
First name:<input type="text" name="userFirstName" /> <br />
Last name:<input type="text" name="userLastName" /> <br />
E-mail:<input type="text" name="userEmail" />
<input type="submit">
</form>
(4) Now configure hibernate configuration files. First of all create hibernate.cfg.xml file as per your requirements. If you don't know how to create then please read about hibernate first. Hibernate tutorial
Then after create *.hbn.xml file to map bean class with database table (here * in the name means you can give name as per your choice). As below
<hibernate-mapping>
<class name="com.persistence.UserVO" table="contact">
<id column="USER_ID" name="id" type="int">
<generator class="native" />
</id>
<property column="USER_NAME" name="userName" type="java.lang.String" />
<property column="USER_PASSWORD" name="userPassword" type="java.lang.String" />
<property column="USER_FIRST_NAME" name="userFirstName" type="java.lang.String" />
<property column="USER_LAST_NAME" name="userLastName" type="java.lang.String" />
<property column="USER_EMAIL" name="userEmail" type="java.lang.String" />
</class>
</hibernate-mapping>
PLEASE NOTE:
This is main part where you have to take enough care. In class tag value of attribute name must be equal to the value of attribute type in tag form-bean.
As well in id and property tags column name must contain the name of column of your table and name tag must contain the value you define in bean class.
(5) Now define the class extending Action class of struts' action class as below.
public class UserController extends Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
try {
UserVO user = (UserVO)form;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction t = session.beginTransaction();
session.saveOrUpdate(user);
t.commit();
return mapping.findForward("success");
} catch (Exception e) {
return mapping.findForward("failure");
}
}
}