- 浏览: 30206 次
- 性别:
- 来自: 宜昌
最新评论
很有段时间没有研究代码了,这几天自己动手写了一个小练习,主要用到的技术有
spring
hibernate
dwr
js
html
现将代码共享出来,希望对研究以上技术的朋友们有点帮助,互相学习!!!
下面进行下分解介绍
数据访问层
UserDao.java:
package com.testproject.dao;
import java.util.List;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public interface UserDao {
void save(User user);
void delete(Integer userid);
void update(User user);
List<User> findUsers();
User getUser(Integer userid);
}
UserDaoImpl.java:
package com.testproject.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.testproject.dao.UserDao;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
@Transactional
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;
public void delete(Integer userid) {
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(User.class, userid));
}
public void save(User user) {
sessionFactory.getCurrentSession().persist(user);
}
public void update(User user) {
sessionFactory.getCurrentSession().merge(user);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
@SuppressWarnings("unchecked")
public List<User> findUsers() {
return sessionFactory.getCurrentSession().createQuery("from User")
.list();
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public User getUser(Integer userid) {
return (User) sessionFactory.getCurrentSession()
.get(User.class, userid);
}
}
//*********************************
数据实体
package com.testproject.model;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public class User {
/** id */
private Integer id;
/** uname */
private String uname;
/** uaddress */
private String uaddress;
public User(String uname, String uaddress) {
this.uname = uname;
this.uaddress = uaddress;
}
public User() {
}
public User(Integer uid, String uname, String uaddress) {
this.id = uid;
this.uname = uname;
this.uaddress = uaddress;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
//*********************************8
业务处理层
UserService.java:
package com.testproject.service;
import java.util.List;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public interface UserService {
int addUser(String name,String address);
int deleteUser(Integer userid);
int updateUser(Integer id,String name,String address);
List<User> findUsers();
User getUser(Integer userid);
}
UserServiceImpl.java:
package com.testproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.testproject.dao.UserDao;
import com.testproject.model.User;
import com.testproject.service.UserService;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int deleteUser(Integer userid) {
int result=0;
try {
userDao.delete(userid);
result=1;
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
public List<User> findUsers() {
List<User> result = new ArrayList<User>();
try {
List<User> users = userDao.findUsers();
for (Object o : users) {
User u = (User) o;
result.add(new User(u.getId(),u.getUname(),u.getUaddress()));
}
} catch (Exception e) {
}
return result;
}
public int updateUser(Integer id,String name,String address) {
int result=0;
try {
User user=userDao.getUser(id);
user.setUname(name);
user.setUaddress(address);
userDao.update(user);
result=user.getId();
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
public User getUser(Integer userid) {
User user = null;
try {
user =userDao.getUser(userid);
} catch (RuntimeException e) {
e.printStackTrace();
}
return user;
}
public int addUser(String name, String address) {
int result=0;
try {
User user=new User();
user.setUname(name);
user.setUaddress(address);
userDao.save(user);
result=user.getId();
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
}
//***************************
spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>master</value>
</property>
<!--连接池启动时的初始值-->
<property name="initialSize" value="1" />
<!--连接池的最大值-->
<property name="maxActive" value="500" />
<!--最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放
一部分,一直减少到maxIdle为止-->
<property name="maxIdle" value="2" />
<!--最小空闲值当空闲的连接数少于阀值时,连接池就会预申请一些连接,以免洪峰来时
来不及申请-->
<property name="minIdle" value="1" />
</bean>
<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.testproject.dao.impl.UserDaoImpl"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="userService"
class="com.testproject.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
dwr配置文件
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="us" creator="spring">
<param name="beanName" value="userService"/>
<include method="addUser"/>
<include method="updateUser"/>
<include method="deleteUser"/>
<include method="findUsers"/>
<include method="getUser"/>
</create>
<convert match="com.testproject.model.User" converter="bean"></convert>
</allow>
</dwr>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/page/welcome.html</welcome-file>
</welcome-file-list>
</web-app>
//********************
hibernate配置文件
User.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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="com.testproject.model" >
<class name="User" table="userinfo" >
<id name="id" >
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" length="20" not-null="true" />
</property>
<property name="uaddress" type="java.lang.String">
<column name="uaddress" length="100" not-null="true" />
</property>
</class>
</hibernate-mapping>
//*****************
页面+js脚本文件
add.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>增加</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body>
<center>
<table border="1" CellSpacing="0" cellpadding="0" width="400"
height="250">
<tr align="center">
<td colspan=2>
<b>add</b>
</td>
</tr>
<tr>
<td align="right">
name:
</td>
<td align="center">
<input id="name" type="text">
</td>
</tr>
<tr>
<td align="right">
address:
</td>
<td align="center">
<input id="address" type="text">
</td>
</tr>
<tr align="center">
<td colspan=2>
<input type="button" value=" add " onclick="addUser()">
<input type="reset" value="cancel" onclick="clearText()">
<a href="main.html">返回详细</a>
</td>
</tr>
</table>
</center>
</body>
</html>
common.js
//增加
function addUser(){
var name =$('name').value;
var address =$('address').value;
if(name==null||name==""||address==""||address==null)
{
alert("请先输入用户名和地址信息");
return false;
}
us.addUser(name,address,addUserCb);
}
function addUserCb(data)
{
if(data>0)
{
$('name').value="";
$('address').value="";
alert("添加用户成功");
history.back();
}
else
{
alert("添加用户失败");
}
}
function clearText()
{
$('name').value="";
$('address').value="";
}
//查询
function getUsers(){
us.findUsers(callBack);
}
function callBack(data){
var cell=[
function(data){
return data.id;
},
function(data){
return data.uname;
},
function(data){
return data.uaddress;
},
function(data){
var up=document.createElement("a");
//up.href="update.html?id="+data.id;
up.href="javascript:getInfoToPage("+data.id+")";
up.innerHTML="编辑";
return up;
}
,
function(data){
var del=document.createElement("a");
del.setAttribute("href","javascript:delUser("+data.id+")");
var txt=document.createTextNode("删除");
del.appendChild(txt);
return del;
}
];
if (typeof window['DWRUtil'] == 'undefined')
window.DWRUtil = dwr.util;
DWRUtil.addRows("tab",data,cell);
}
//删除
function delUser(userid)
{
us.deleteUser(userid,delUserCb)
}
function delUserCb(data)
{
if(data>0)
{
alert('删除用户成功');
window.location.reload();
}
}
//修改
//先获得信息赋值给页面控件
function getInfoToPage(userid)
{
us.getUser(userid,InfoCb);
}
function InfoCb(data)
{
if(data!=null)
{
window.showModalDialog("update.html",data);
}
}
function upOnLoad()
{
var table=document.createElement("updiv");
table.innerHTML ="<table border='1' CellSpacing='0' cellpadding='0' width='400'height='250'><tr align='center'><td colspan=2> <b>update</b></td></tr><tr><td align='right'>id:</td><td><input type='text' id='upid' name='upid' readonly='readonly' value='"+window.dialogArguments.id+"'></td></tr><tr><td align='right'>name:</td><td><input type='text' id='upname' name='upname'value='"+window.dialogArguments.uname+"'></td></tr><tr> <td align='right'>address:</td><td><input type='text' id='upaddress' name='upaddress'value='"+window.dialogArguments.uaddress+"'></td></tr><tr align='center'><td colspan=2><input type='button' value='update' onclick='updateUser()'><input type='reset' value='cancel'></td></tr></table>";
document.body.appendChild(table);
}
function updateUser()
{
var id =$('upid').value;
var upname =$('upname').value;
var upaddress =$('upaddress').value;
if(upname==null||upname==""||upaddress==""||upaddress==null)
{
alert("请先输入用户名和地址信息");
return false;
}
us.updateUser(id,upname,upaddress,upCb);
}
function upCb(data)
{
if(data>0)
{
alert("修改用户数据成功");
window.close();
}
}
main.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>详细</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body onload="getUsers()">
<center>
<table border="1">
<tr>
<td width="30" align="center">
编号
</td>
<td width="30" align="center">
姓名
</td>
<td width="30" align="center">
地址
</td>
<td colspan="2" width="60" align="center">
操作
</td>
</tr>
<tbody id="tab">
</tbody>
</table>
<a href="add.html">添加用户</a>
<a href="javascript:window.location.reload()">数据刷新</a>
</center>
</body>
</html>
update.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>修改</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body onload="upOnLoad()">
<center>
<div id="updiv"></div>
</center>
</body>
</html>
welcome.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>welcome.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<a href="./page/main.html">显示</a>
</body>
</html>
以上系统完整的文件内容,环境搭好即可运行
运行环境:mysql+tomcat6+jdk6
spring和hibernate均用的最新版本的
spring
hibernate
dwr
js
html
现将代码共享出来,希望对研究以上技术的朋友们有点帮助,互相学习!!!
下面进行下分解介绍
数据访问层
UserDao.java:
package com.testproject.dao;
import java.util.List;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public interface UserDao {
void save(User user);
void delete(Integer userid);
void update(User user);
List<User> findUsers();
User getUser(Integer userid);
}
UserDaoImpl.java:
package com.testproject.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.testproject.dao.UserDao;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
@Transactional
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;
public void delete(Integer userid) {
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(User.class, userid));
}
public void save(User user) {
sessionFactory.getCurrentSession().persist(user);
}
public void update(User user) {
sessionFactory.getCurrentSession().merge(user);
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
@SuppressWarnings("unchecked")
public List<User> findUsers() {
return sessionFactory.getCurrentSession().createQuery("from User")
.list();
}
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public User getUser(Integer userid) {
return (User) sessionFactory.getCurrentSession()
.get(User.class, userid);
}
}
//*********************************
数据实体
package com.testproject.model;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public class User {
/** id */
private Integer id;
/** uname */
private String uname;
/** uaddress */
private String uaddress;
public User(String uname, String uaddress) {
this.uname = uname;
this.uaddress = uaddress;
}
public User() {
}
public User(Integer uid, String uname, String uaddress) {
this.id = uid;
this.uname = uname;
this.uaddress = uaddress;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
//*********************************8
业务处理层
UserService.java:
package com.testproject.service;
import java.util.List;
import com.testproject.model.User;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public interface UserService {
int addUser(String name,String address);
int deleteUser(Integer userid);
int updateUser(Integer id,String name,String address);
List<User> findUsers();
User getUser(Integer userid);
}
UserServiceImpl.java:
package com.testproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.testproject.dao.UserDao;
import com.testproject.model.User;
import com.testproject.service.UserService;
/**
*
* @author davy liu
* Apr 4, 2010 9:51:40 PM
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public int deleteUser(Integer userid) {
int result=0;
try {
userDao.delete(userid);
result=1;
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
public List<User> findUsers() {
List<User> result = new ArrayList<User>();
try {
List<User> users = userDao.findUsers();
for (Object o : users) {
User u = (User) o;
result.add(new User(u.getId(),u.getUname(),u.getUaddress()));
}
} catch (Exception e) {
}
return result;
}
public int updateUser(Integer id,String name,String address) {
int result=0;
try {
User user=userDao.getUser(id);
user.setUname(name);
user.setUaddress(address);
userDao.update(user);
result=user.getId();
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
public User getUser(Integer userid) {
User user = null;
try {
user =userDao.getUser(userid);
} catch (RuntimeException e) {
e.printStackTrace();
}
return user;
}
public int addUser(String name, String address) {
int result=0;
try {
User user=new User();
user.setUname(name);
user.setUaddress(address);
userDao.save(user);
result=user.getId();
} catch (RuntimeException e) {
e.printStackTrace();
}
return result;
}
}
//***************************
spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>master</value>
</property>
<!--连接池启动时的初始值-->
<property name="initialSize" value="1" />
<!--连接池的最大值-->
<property name="maxActive" value="500" />
<!--最大空闲值。当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放
一部分,一直减少到maxIdle为止-->
<property name="maxIdle" value="2" />
<!--最小空闲值当空闲的连接数少于阀值时,连接池就会预申请一些连接,以免洪峰来时
来不及申请-->
<property name="minIdle" value="1" />
</bean>
<!-- 配置sessionFactory, 注意这里引入的包的不同 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="userDao" class="com.testproject.dao.impl.UserDaoImpl"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="userService"
class="com.testproject.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
dwr配置文件
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create javascript="us" creator="spring">
<param name="beanName" value="userService"/>
<include method="addUser"/>
<include method="updateUser"/>
<include method="deleteUser"/>
<include method="findUsers"/>
<include method="getUser"/>
</create>
<convert match="com.testproject.model.User" converter="bean"></convert>
</allow>
</dwr>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/page/welcome.html</welcome-file>
</welcome-file-list>
</web-app>
//********************
hibernate配置文件
User.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">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="com.testproject.model" >
<class name="User" table="userinfo" >
<id name="id" >
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" length="20" not-null="true" />
</property>
<property name="uaddress" type="java.lang.String">
<column name="uaddress" length="100" not-null="true" />
</property>
</class>
</hibernate-mapping>
//*****************
页面+js脚本文件
add.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>增加</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body>
<center>
<table border="1" CellSpacing="0" cellpadding="0" width="400"
height="250">
<tr align="center">
<td colspan=2>
<b>add</b>
</td>
</tr>
<tr>
<td align="right">
name:
</td>
<td align="center">
<input id="name" type="text">
</td>
</tr>
<tr>
<td align="right">
address:
</td>
<td align="center">
<input id="address" type="text">
</td>
</tr>
<tr align="center">
<td colspan=2>
<input type="button" value=" add " onclick="addUser()">
<input type="reset" value="cancel" onclick="clearText()">
<a href="main.html">返回详细</a>
</td>
</tr>
</table>
</center>
</body>
</html>
common.js
//增加
function addUser(){
var name =$('name').value;
var address =$('address').value;
if(name==null||name==""||address==""||address==null)
{
alert("请先输入用户名和地址信息");
return false;
}
us.addUser(name,address,addUserCb);
}
function addUserCb(data)
{
if(data>0)
{
$('name').value="";
$('address').value="";
alert("添加用户成功");
history.back();
}
else
{
alert("添加用户失败");
}
}
function clearText()
{
$('name').value="";
$('address').value="";
}
//查询
function getUsers(){
us.findUsers(callBack);
}
function callBack(data){
var cell=[
function(data){
return data.id;
},
function(data){
return data.uname;
},
function(data){
return data.uaddress;
},
function(data){
var up=document.createElement("a");
//up.href="update.html?id="+data.id;
up.href="javascript:getInfoToPage("+data.id+")";
up.innerHTML="编辑";
return up;
}
,
function(data){
var del=document.createElement("a");
del.setAttribute("href","javascript:delUser("+data.id+")");
var txt=document.createTextNode("删除");
del.appendChild(txt);
return del;
}
];
if (typeof window['DWRUtil'] == 'undefined')
window.DWRUtil = dwr.util;
DWRUtil.addRows("tab",data,cell);
}
//删除
function delUser(userid)
{
us.deleteUser(userid,delUserCb)
}
function delUserCb(data)
{
if(data>0)
{
alert('删除用户成功');
window.location.reload();
}
}
//修改
//先获得信息赋值给页面控件
function getInfoToPage(userid)
{
us.getUser(userid,InfoCb);
}
function InfoCb(data)
{
if(data!=null)
{
window.showModalDialog("update.html",data);
}
}
function upOnLoad()
{
var table=document.createElement("updiv");
table.innerHTML ="<table border='1' CellSpacing='0' cellpadding='0' width='400'height='250'><tr align='center'><td colspan=2> <b>update</b></td></tr><tr><td align='right'>id:</td><td><input type='text' id='upid' name='upid' readonly='readonly' value='"+window.dialogArguments.id+"'></td></tr><tr><td align='right'>name:</td><td><input type='text' id='upname' name='upname'value='"+window.dialogArguments.uname+"'></td></tr><tr> <td align='right'>address:</td><td><input type='text' id='upaddress' name='upaddress'value='"+window.dialogArguments.uaddress+"'></td></tr><tr align='center'><td colspan=2><input type='button' value='update' onclick='updateUser()'><input type='reset' value='cancel'></td></tr></table>";
document.body.appendChild(table);
}
function updateUser()
{
var id =$('upid').value;
var upname =$('upname').value;
var upaddress =$('upaddress').value;
if(upname==null||upname==""||upaddress==""||upaddress==null)
{
alert("请先输入用户名和地址信息");
return false;
}
us.updateUser(id,upname,upaddress,upCb);
}
function upCb(data)
{
if(data>0)
{
alert("修改用户数据成功");
window.close();
}
}
main.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>详细</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body onload="getUsers()">
<center>
<table border="1">
<tr>
<td width="30" align="center">
编号
</td>
<td width="30" align="center">
姓名
</td>
<td width="30" align="center">
地址
</td>
<td colspan="2" width="60" align="center">
操作
</td>
</tr>
<tbody id="tab">
</tbody>
</table>
<a href="add.html">添加用户</a>
<a href="javascript:window.location.reload()">数据刷新</a>
</center>
</body>
</html>
update.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>修改</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
<head>
<script type='text/javascript' src='/TestProject/dwr/interface/us.js'></script>
<script type='text/javascript' src='/TestProject/dwr/engine.js'></script>
<script type='text/javascript' src='/TestProject/dwr/util.js'></script>
<script type='text/javascript' src='common.js'></script>
</head>
<body onload="upOnLoad()">
<center>
<div id="updiv"></div>
</center>
</body>
</html>
welcome.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>welcome.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<a href="./page/main.html">显示</a>
</body>
</html>
以上系统完整的文件内容,环境搭好即可运行
运行环境:mysql+tomcat6+jdk6
spring和hibernate均用的最新版本的
相关推荐
为了运行或理解这个例子程序,需要解压这个文件并查看其内部结构,包括但不限于Java源码、配置文件(如Spring的`applicationContext.xml`、Hibernate的`hibernate.cfg.xml`和DWR的`dwr.xml`)、HTML/JS前端文件...
描述 "ext2+spring+hibernate+dwr做的小系统,很好的一个研究例子。" 暗示这是一个小型的Web应用系统,开发者利用了Spring作为整体的应用框架,负责依赖注入和控制反转;Hibernate作为ORM(对象关系映射)工具,用于...
在这个“struts+spring+hibernate+dwr+分页例子”中,我们将深入探讨这四个框架的集成以及分页功能的实现。 1. **Struts**: Struts 是一个基于MVC(Model-View-Controller)设计模式的Java Web框架。它主要负责处理...
这个小例子将这四个框架整合在一起,用于实现一个登录注册系统,提供了从客户端到服务器端的数据交互、持久化数据管理以及业务逻辑处理的完整解决方案。 首先,让我们逐一解析这四个技术: 1. **Ajax**...
Struts2、Spring、Hibernate和DWR2是四个在Java Web开发中非常关键的技术框架,它们共同构建了一个强大且灵活的后端系统架构。这里,我们将会深入探讨这些技术及其在实际项目中的应用。 首先,Struts2是一个MVC...
标题中的"spring+struts+hibernate+dwr+jstl_example.rar"表明这是一个使用了Spring、Struts、Hibernate、Direct Web Remoting (DWR) 和JSP Standard Tag Library (JSTL)技术的示例项目。这个组合是Java Web开发中...
DWR(Direct Web Remoting)则是一种JavaScript库,它允许在浏览器和服务器之间进行实时的、双向的通信,增强了Web应用的交互性。下面将详细阐述这些技术及其在城市联动功能中的应用。 首先,Struts2作为MVC(Model...
"dwr+spring+hibernate"是一个常见的组合,它将Direct Web Remoting (DWR),Spring框架和Hibernate ORM工具集成了一个高效、灵活的Web应用程序开发解决方案。下面我们将详细探讨这三个组件以及它们在整合中的作用。 ...
DWR 提供了一种在浏览器与服务器之间进行实时通信的技术,EXT 是一个强大的 JavaScript UI 框架,Spring 是一个全面的轻量级应用框架,而 Hibernate 则是 Java 的对象关系映射(ORM)工具。这四个组件的结合可以构建...
在IT行业中,开发高效、可扩展的Web应用是至关重要的,而`DWR (Direct Web Remoting)`、`Spring`和`Hibernate`是三个非常关键的技术组件,它们各自扮演着不同的角色并协同工作以实现这一目标。这个"高效率的dw+...
"dwr+ext+struts2+spring+hibernate整合"就是一个典型的例子,它涵盖了前端展示、后端控制、业务逻辑处理、持久层操作等多个层面的技术。下面将分别详细介绍这些技术以及它们在整合中的作用。 1. DWR (Direct Web ...
这个“Struts2+spring依赖注入+Hibernate+Dwr”的例子,是一个综合性的Web应用程序示例,旨在展示这四个技术如何协同工作。 Struts2作为MVC(模型-视图-控制器)框架,主要负责处理HTTP请求,调度控制流程,并将...
【标题】:“DWR+Hibernate+Spring未完成例子”揭示了这个压缩包中包含的是一个未完成的示例项目,该示例项目整合了Direct Web Remoting (DWR)、Hibernate ORM框架和Spring框架。这三者都是Java Web开发中的重要组件...
### jsp源码OA办公自动化管理系统(Struts1.2+Hibernate3.0+Spring2+DWR)130224 #### 技术栈介绍 本项目是一款基于多种主流Java技术构建的办公自动化系统(OA系统)。该系统采用了JSP作为前端展示层的技术,后端则...
Dwr2、Struts2、Spring2.5和Hibernate3各自扮演着关键的角色,共同实现了分层架构,确保各组件专注于自身的核心功能。 Dwr2,即Direct Web Remoting 2,是一个JavaScript到服务器端Java的远程调用框架,主要用于...
开发者可以通过研究这个项目,学习如何在实际项目中应用SpringMVC、Hibernate、DWR和SmartClient,提升自己的Web开发能力。同时,这也是一个很好的起点,可以在此基础上扩展出更复杂的功能,例如添加更多的业务逻辑...
【标题】"Ext+dwr+spring做的例子程序"是一个整合了三个关键技术的示例应用,主要展示了如何在Java Web开发中结合使用EXTJS(Ext)前端框架、Direct Web Remoting (DWR) 和Spring框架。这三个技术在现代企业级应用...
本项目"DWRSpringHibernateStrutsTest"就是一个典型的例子,它整合了Direct Web Remoting (DWR)、Spring框架、Struts框架以及Hibernate ORM工具,以创建一个登录系统。下面我们将详细探讨这些技术以及它们在项目中的...
EXT JS 3.0版本在这个例子中被使用,它提供了丰富的组件库,包括网格、表格、面板、窗口等,以及强大的布局管理。动态树(Dynamic Tree)是EXT JS中的一个组件,它可以实时更新和扩展,用户可以通过它来交互式地查看...
SSH2 (Struts2 + Spring + Hibernate) 和 DWR (Direct Web Remoting) 是两种常见的Java Web开发技术。在这个小例子中,它们被结合起来用于构建一个医药管理系统。下面将详细介绍这些技术及其在项目中的应用。 **SSH...