成功整合dwr3+spring mvc3+tomcat7

1,tomcat7的context.xml需要修改:
<Context useHttpOnly="false">
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>


2,[servlet]-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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

	<context:annotation-config />
	<!-- 把标记了@Controller注解的类转换为bean -->
	<context:component-scan base-package="com.xkxt.controller" />
	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
		<property name="order" value="0" />
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
	<property name="order" value="1"/>
	</bean> 
	
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
	
	<dwr:configuration />   
   <dwr:controller id="dwrController" debug="true" />
   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="alwaysUseFullPath" value="true"/>
     <property name="order" value="2"/>
     <property name="mappings">
     <props>
       <prop key="/dwr/**/*">dwrController</prop>
     </props>
     </property>
   </bean>  
   <dwr:annotation-scan base-package="com.xkxt.dwr" scanDataTransferObject="true" scanRemoteProxy="true"/>
	
	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />

	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		p:defaultEncoding="utf-8" />

	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/*" />
			<bean class="com.xkxt.filter.LoginInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

</beans>


jsp:
js:
function getDataFromServer(str) {
  dwrService.cancelCourse(str,{
  	callback: getDataFromServerCallBack
  });
}

function getDataFromServerCallBack(obj) {
  alert(obj.msg);
}


java model:
package com.xkxt.dwr.model;

import java.io.Serializable;

import org.directwebremoting.annotations.DataTransferObject;

@DataTransferObject
public class CommonModel implements Serializable {
	private String msg;
	private String flag; //成功与否标志

	
	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}


java service:
package com.xkxt.dwr.service;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;

import com.xkxt.dwr.model.CommonModel;

@RemoteProxy(name="dwrService")
public class DWRService {
	protected final transient Log log = LogFactory.getLog(DWRService.class);
		
	public DWRService() { }
	
	@RemoteMethod
	public CommonModel cancelCourse(String xkkh, HttpSession session) throws Exception {
		log.info("-----dwr test-----" + xkkh);
		CommonModel model = new CommonModel();
		if(xkkh == null || xkkh.trim().length() <=0){
			return model;
		}
		model.setFlag("1");
		model.setMsg("成功!");
		return model;
	}
}


html:
<a href="#" onclick="getDataFromServer('test'); return false;">test</a>

你可能感兴趣的:(java,spring,servlet,DWR,Comet)