springMVC3集成DWR3

springMVC集成DWR:
参考:
http://www.iteye.com/topic/209008
http://stevenfeng.iteye.com/blog/816010


集成DWR之前,先做Spring MVC和Hibernate整合的例子: http://panyongzheng.iteye.com/blog/1228193
然后在这个基础之上整合DWR:

1.到http://directwebremoting.org/dwr/downloads/index.html下载最新的dwr.jar,这里是使用dwr3.然后放到WEB-INF/lib下面去。
同时找到commons-logging.jar,DwrSpringAnnotations.jar这两个包,并放到lib下面去,去掉没必要的包:去掉cglib-2.2.jar,
保留com.springsource.net.sf.cglib-2.2.0.jar.

下面的的代码是js-> controller->dwr-->service-->dao.....

2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name></display-name>

<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>springhibernate</servlet-name> 
        <servlet-class> 
            org.springframework.web.servlet.DispatcherServlet 
        </servlet-class>
     </servlet> 
    <servlet-mapping> 
        <servlet-name>springhibernate</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
        <servlet-name>springhibernate</servlet-name> 
        <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
        <servlet-name>springhibernate</servlet-name> 
        <url-pattern>/dwr/*</url-pattern> 
    </servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<servlet-mapping> 
        <servlet-name>springhibernate</servlet-name> 
        <url-pattern>/dwr/*</url-pattern> 
</servlet-mapping>
这个是不能少的


3.修改springhibernate-servlet.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"    
      
    xsi:schemaLocation=" http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
  

3.建立webControllerContext.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"  
    xmlns:dwra="http://www.directwebremoting.org/schema/spring-dwr-annotations"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="  
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
            http://www.directwebremoting.org/schema/spring-dwr  
            http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd  
            http://www.directwebremoting.org/schema/spring-dwr-annotations  
            http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd  
    		http://www.springframework.org/schema/mvc    
    		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
      
    <mvc:annotation-driven />  
    <context:component-scan base-package="com.controller,com.service.impl,com.pojo,com.dao.impl,com.dwr" />
    
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>  
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
      
    <dwr:configuration>  
        <dwr:convert class="com.pojo.User" type="bean" />  
        <dwr:signatures>  
            <![CDATA[ 
                import java.util.Map; 
                ]]>  
        </dwr:signatures>  
    </dwr:configuration>  
  
    <dwr:controller id="dwrController" debug="true" />  
  
    <!--  New DWR capabilities-->  
    <dwra:url-mapping />  
    <dwra:annotation-config />  
</beans>
   

假如不用<dwr:convert class="com.pojo.User" type="bean" />对转换对象进行扫描,
那就用<dwr:annotation-scan base-package="com.asl.cityu.entity" scanDataTransferObject="true"/>进行一次扫描,必需的放在<dwr:configuration/>外面。

把xml头部定义
http://www.directwebremoting.org/schema/spring-dwr   
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
改成:
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd

4.修改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" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans       
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
    http://www.springframework.org/schema/context       
    http://www.springframework.org/schema/context/spring-context-3.0.xsd       
    http://www.springframework.org/schema/tx       
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
    http://www.springframework.org/schema/jdbc       
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"  
    default-autowire="byName" default-lazy-init="true">


	<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/test"></property>
		<property name="username" value="root"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.pojo.User</value>
			</list>
		</property>
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<import resource="webControllerContext.xml"/>
	
</beans>


要是不用<property name="annotatedClasses">
<list>
<value>com.pojo.User</value>
</list>
</property>
的话  就要hibernate自己扫描自己的东西
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="com.asl.jwaf.sysconfig.model">
......
</bean>

 

5.建立WebRoot/pages/ajax/ajax-add-page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">

  	<script type='text/javascript' src="/mySpringMVCHibernate/dwr/engine.js"></script>
  	<script type='text/javascript' src="/mySpringMVCHibernate/dwr/util.js"></script>
	<script type="text/javascript" src="/mySpringMVCHibernate/dwr/interface/dwrService.js"></script>
	<script type="text/javascript" src="pages/ajax/ajax_add_page.js"></script>
	
	<title>Spring MVC - DWR Integration Tutorial</title>
</head>
<body>
<div style="border: 1px solid #ccc; width: 250px;">
	Add Two Numbers: <br/>
	<input id="inputNumber1" type="text" size="5"> +
	<input id="inputNumber2" type="text" size="5">
	<input type="submit" value="Add" onclick="add()" /> <br/>
	Sum: <span id="sum">(Result will be shown here)</span>
</div>
	
	<form action="#">
		Name:<input type="text" id="name" name="name">
		Password:<input type="text" id="password" name="password">
		<input type="button" value="Save" onclick="saveUser()">
	</form>

</body>
</html>


6.建立WebRoot/pages/ajax/ajax_add_page.js文件:

function add(){
    // Retrieve value of text inputs
    var operand1 = dwr.util.getValue("inputNumber1");
    var operand2 = dwr.util.getValue("inputNumber2");
    
    // Pass two numbers, a callback function, and error function
    dwrService.add(operand1, operand2, {
        callback: handleAddSuccess,
        errorHandler: handleAddError
    });
}

// data contains the returned value
function handleAddSuccess(data){
    // Assigns data to result id
    dwr.util.setValue("sum", data);
}

function handleAddError(){
    // Show a popup message
    alert("We can't add those values!");
}


saveUser = function(){
	var name = document.getElementById("name").value;
	var password = document.getElementById("password").value;
	var obj={
		name:name,
		password:password
	};
	
	alert(obj.name);
	
	dwrService.saveUser(obj,{callback:function(data){
		alert("return data="+data);
	},errorHandler:function(data){
		
	}});
}




7.建立controller
Hello.java
---------------------------------------
  

猜你喜欢

转载自panyongzheng.iteye.com/blog/1233213