3:29 pm - Saturday January 21, 2017

Struts with hibernate integration Tutorial with example program

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

 

  • This application will use  join struts and hibernate applications.
  • Here First  we will  get input from front view html or Jsp page.
  • Then it will pass model and cotroller using struts application.
  • Then In controller page input will save the database using Hibernate application.
  • After save the inputs will pass the front view.

 Project Structure:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

 AddStudentControl.java:


  • Here we will use sessionFactory for create connetion Oracle database.
    • sessionFactory = new Configuration().configure

(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

  • Then we will get input from AddController.java through arguments and then save the database using session.
    • stu.setStuname(stuname);
    • s.save(stu);
  • Then we will use  Transaction statement for  begin and commit the database.
    • Transaction tx= s.beginTransaction();
    • tx.commit();





MyController.java:

  • It is a Controller class .It will extends Action class.
  • MyModel m = (MyModel)fm;//Here model class object will store in the variable name m.
  • String s=m.getName();String s1=m.getDegree();String s2=m.getMobileno();//Here get input from model class.
  • AddStudentControl ctrl = new AddStudentControl();//Here we will create object for AddStudentControl class
  • ctrl.addNewStudent(s,s1,s2);//Here pass the input to AddNewStudent method through Arguments from MyController class.



MyModel.java: 

  • It is a Model class. It will extends ActionForm class which is  a Serializable class. Then here we will use get and set method.
    • private String name;
    • public String getName(){return name;}
    • public void setName(String n){name=n;  }



Student.java: 

  • It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.
    • private long id;
    • public long getId() {return id;}
    • public void setId(long String) {id = String;}

web.xml:

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>//This tag will load the ActionServlet class.

  • <init-param><param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value> </init-param>//This tag will use configure the struts-config.xml.

</servlet>

  • <servlet-mapping><servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern> </servlet-mapping>//here *.do invoke the ActionServlet class.

hibernate.cfg.xml: 

<form-beans><form-bean type=”com.candidjava.MyModel” name =”model” />

</form-beans>//Beans tag will use for model class.Here name=”model” which is same as the controller name=”model”.

  • <action-mappings><action path=”/myActionForm” type=”com.candidjava.MyController” name=”model” input=”/Login.jsp”>//Action mapping tag will use both view and controller.Action tag will use for controller class.Here path=”/myActionForm” which is same as the front view Login.jsp action=”/myActionForm”.
  • <forward name=”success” path=”/Success.jsp” />//Forward tag will use for view page.It will forward  input to the view page
  • <forward name=”error” path=”/Fail.jsp” /></action></action-mappings>

struts-config.xml


Student.hbm.xml:

  • <class name=”com.candidjava.Student” table=”student” >//This tag will use for mapping Pojo class name and  table name.
  • <id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>//This tag will use for generating primary key id and also increment the id value.
  • <property name=”name” column=”name” not-null=”true”/>//This property will use for mapping the pojo class veriable name(name=”name” ) and  table attribute name(column=”name”).
  • </class>”/>//End of the class.
Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<!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>Failure page</title>
</head>
<body bgcolor="pink">
<br></br>
<body>

<html:html>
<html:form action="/myActionForm" method="post">
		Enter Student Name:		<html:text property="name" size="20" />
	<br></br>
		Enter Student Degree:	<html:text property="degree" size="20" />
	<br></br>
		Enter Student Mobileno:	<html:text property="mobileno" size="20" />
	<br></br>

	<html:submit>submit</html:submit>
	<br>
	<br>
</html:form>
</html:html>

</body>
</html>

Success.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>Success page</title>
</head>
<body bgcolor="pink">
<body>

sucessfully student details added
<br></br>
<%=session.getAttribute("s")%><br></br>
<%=session.getAttribute("s1")%><br></br>
<%=session.getAttribute("s2")%><br></br>

</body>
</html>

Fail.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>Failure page</title>
</head>
<body bgcolor="pink">
<body>
sorry error


</body>
</html>

OUTPUT:

                                                                                                                                                                                                                                                                                                                                                                      

   

 

 

DOWNLOAD+SOURCECODE    

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply