一. 首先准备以下包:
Dwr应用包: dwr.jar
Spring应用的所有包(注意
版本,自行选择)
Hibernate应用的所有包(注意版本,自行选择)
二. 以声明的方式创建
ApplicationContext实例
在web.xml文件中加入以下:
<!-- spring -->
<!-- 以声明的方式创建ApplicationContext实例 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
三. 在web.xml中配置dwr
在web.xml文件中加入以下:
<!-- dwr配置 -->
<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>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
四. 在Spring的application context中创建 SessionFactory
在applicationContext.xml文件中加入以下:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/dwr_s_h">
</property>
<property name="username" value="name"></property>
<property name="password" value="password"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="statement_cache.size">25</prop>
<prop key="jdbc.fetch_size">50</prop>
<prop key="jdbc.batch_size">25</prop>
<prop key="hibernate.use_outer_join">false</prop>
<prop key="jdbc.use_scrollable_resultset">true</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/model/Department.hbm.xml</value>
</list>
</property>
</bean>
注意:我在com.model包中建立了一个最简单的部门类Department.java:
package com.model;
public class Department {
private int did;
private String dname;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
和与其对应的映射文件Department.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">
<hibernate-mapping>
<class name="com.model.Department" table="DEPARTMENT">
<id name="did" type="java.lang.Integer">
<column name="DID" />
<generator class="native"/>
</id>
<property name="dname" type="java.lang.String">
<column name="DNAME" />
</property>
</class>
</hibernate-mapping>
五. 在Spring中注入相关属性
<!-- 注入sessionFactory-->
<bean id="department" class="com.dao.DepartmentImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
</bean>
<!-- 注入department至dwr.xml的bean引用的dao属性-->
<bean id="DepartmentDwr" class="com.dwr.DepartmentDwr">
<property name="dao">
<ref bean="department"></ref>
</property>
</bean>
六. 配置dwr文件
在dwr.xml文件中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<allow>
<!--前台页面将引用EmployeeDwr,将bean生成方式设置为spring生成 -->
<create creator="spring" javascript="DepartmentDwr" >
<param name="beanName" value="DepartmentDwr"/>
</create>
<!—配置转换器 -->
<convert match="com.model.*" converter="bean"/>
</allow>
</dwr>
七. 在前台页面简单测试下,调用如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html><%
String root = request.getContextPath();
%>
<head>
<script type='text/javascript' src='<%=root%>/dwr/interface/DepartmentDwr.js'></script>
<script type='text/javascript' src='<%=root%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=root%>/dwr/util.js'></script>
<script type="text/javascript">
//部门下拉列表
function department(){
var department = document.getElementById("departmentElem");
DepartmentDwr.show_department(function(data){
for(var i=0; i<data.length; i++){
var oOption = document.createElement("option");
oOption.value=data[i].did;
oOption.text=data[i].dname;
department.options.add(oOption);
}
});
}
function chk(obj){
alert("当前值:"+obj.value);
}
</script>
</head>
<body onload="department();">
<select id="departmentElem" onchange="chk(this);">
<option value="0">--未选择--</option>
</select>
</body>
</html>
完结