Struts

Update data through struts2 and Hibernate 3.0 framework
adplus-dvertising
Previous Home Next

Introduction:

This application provide update data using struts2 and hibernate3.0

Class/Library File Descriptions:

Require tools to run this application 1. Struts2.0 jar file 2. Tomcat server 3. Database Oracle10g 4. Hibernate 3.0 jar file

Source Code:

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
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_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
 org.apache.struts2.dispatcher.ng.filter
 .StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="fetch">
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation
//DTD Struts Configuration 
2.1//EN" "http://struts.apache.org
/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="fetch"
class="org.r4r.UserListAction">
<result name="success">/fetch.jsp</result>
</action>
<action name="update"
class="org.r4r.UserListAction" method="update">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
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.Oracle9Dialect</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system
</property>
<property name="connection.password">
system</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver</property>
<mapping resource="update.hbm.xml"/>
</session-factory>
</hibernate-configuration>
update.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net
/hibernate-mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.-->
<hibernate-mapping>
<class name="org.r4r.UserList" table="userlist1">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="address"/>
<property name="city"/>
<property name="state"/>
</class>
</hibernate-mapping>
UserList.java
package org.r4r;
public class UserList {
	int id;
	String name;
	String address;
	String city;
	String state;	
	public UserList() {
		super();
	// TODO Auto-generated constructor stub
	}
public UserList(String name, String address,
	String city, String state) {
		super();
		this.name = name;
		this.address = address;
		this.city = city;
		this.state = state;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}
UserListAction.java
package org.r4r;
import java.util.List;
public class UserListAction{
private static final long serialVersionUID 
	= 1L;
private String name;
private String address;
private String city;
private String state;
private UserList userlist;
private List<UserList> userlistlist;
DAO dao=new DAO();
private int id;
public String execute(){
userlistlist=dao.list(getName());
return "success";
}
public String update(){
dao.update(getId(),getName(),
	getAddress(),getCity(),getState());
return "success";
}	 	
public UserList getUserlist() {	 		
return userlist;
} 	
public void setUserlist(UserList userlist) {	
this.userlist = userlist;
public List<UserList> getUserlistlist() {		
return userlistlist;
}	
public void setUserlistlist(List<UserList> 
userlistlist) {
this.userlistlist = userlistlist;
}
public String getName() {	 		
return name;
}
public void setName(String name) { 
this.name = name;
}	
public int getId() { 
return id;
}
public void setId(int id) {	 		
this.id = id;
}
public String getAddress() {	 		
return address;
}
public void setAddress(String address) {	
this.address = address;
}
public String getCity() {	 		
return city;
}
public void setCity(String city) { 
this.city = city;
}
public String getState() {	 		
return state;
}
public void setState(String state) { 
this.state = state;
}
}
DAO.java
package org.r4r;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DAO {
@SuppressWarnings("unchecked")
public List<UserList> list(String name){
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
Session session=f.openSession();
Transaction t=session.beginTransaction();
List<UserList> userlistlist=null;
try{
Query q=session.createQuery("select userlist from 
UserList userlist where userlist.name='"+name+"'");
userlistlist=q.list();
}catch(HibernateException e){
System.out.println(e);
}
t.commit();
session.close();
return userlistlist;
}
public UserList update(int id,String name,String
	address,String city,String state){
Configuration cfg=new Configuration().configure();
SessionFactory f=cfg.buildSessionFactory();
Session session=f.openSession();
Transaction t=session.beginTransaction();
UserList userlist=(UserList) session.load
	(UserList.class, id);
userlist.setName(name);
userlist.setAddress(address);
userlist.setCity(city);
userlist.setState(state);
if(null!=userlist){
session.update(userlist);
}
t.commit();
session.close();
return userlist;
}
}
fetch.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="update">
<s:iterator value="userlistlist" 
var="userlist">
<s:hidden name="id"/>
<s:textfield name="name" label="Name"/>
<s:textfield name="address" label="Address"/>
<s:textfield name="city" label="City"/>
<s:textfield name="state" label="State"/>
<s:submit value="Update"/>
</s:iterator>
</s:form>
success.jsp

You are successfully update data

output
Previous Home Next