Structr 2 hibernate 3 integration:
TUTIORALS FROM MADHAV:
Form.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 -Hibernate 3- create dept Application </title>
</head>
<body bgcolor="wheat">
<h2>Struts 2 -Hibernate 3- create dept Application</h2>
<s:form action="dept.action" method="post">
<<%@ page contentType="text/html; charset=UTF-8"%>
s:textfield name="deptno" label="enter deptno" size="20" />
<s:textfield name="dname" label="enter dname" size="20" />
<s:textfield name="loc" label="enter loc" size="20" />
<s:submit method="execute" align="center" />
</s:form>
</body>
</html>
Success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="wheat">
<h2 color="green"> inssertin is success</h2> <br>
</body>
</html>
Struct.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.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />
<package name="default2" extends="struts-default" namespace="/">
<action name="dept"
class="madhav.controller.DepartmentController">
<result name="success">success.jsp</result>
<result name="error">form.jsp</result>
</action>
</package>
</struts>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</display-name>
<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>
<welcome-file-list>
<welcome-file>form.jsp</welcome-file>
</welcome-file-list>
</web-app>
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="madhav.dao.Department" />
</session-factory>
</hibernate-configuration>
Department.java
package madhav.dao;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Department {
@Id
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
DaoService.java
package madhav.dao;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class DaoService {
/**
* @param args
*/
public static void create(int deptno,String dname,String loc) {
// TODO Auto-generated method stub
try {
// Create the SessionFactory from hibernate.cfg.xml
SessionFactory sf= new
AnnotationConfiguration().configure().buildSessionFactory();
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
Department d=new Department();
d.setDeptno(deptno);
d.setDname(dname);
d.setLoc(loc);
s.save(d);
tx.commit();
System.out.print(d.getDeptno());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
}
}
}
DepartmentController.java
package madhav.controller;
import javax.servlet.http.HttpServletRequest;
import madhav.dao.DaoService;
import com.opensymphony.xwork2.ActionSupport;
public class DepartmentController extends ActionSupport {
private int deptno;
private String dname;
private String loc;
public String execute() {
DaoService deptservice=new DaoService();
deptservice.create(deptno,dname,loc);
return "success";
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Output:
Related topics:
2. Restfulwebservice java, jquery, json,ajax example
3. Structr2 hibernate 3 integration example program
4. springmvc spring provided hibernate example
5. struct 2 spring dao
6. struts 2hibernate integration example
TUTIORALS FROM MADHAV:
can u provide an example spring mvc with spring provicde hibernateDAoSupport as backend example
ReplyDeleteyea ,karthik we can see that example program through the below link.
Deletehttp://softtechgmr.blogspot.in/2012/11/spring-mvc-spring-provided-hibernate.html
Nice tutorial using Struts 2 with good images. I have a Basic Struts Login Application program using Strus1.2. I think this will also very helpful to your blog followers.
ReplyDelete