<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:top="http://www.comtop.org/schema/spring-top" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd http://www.comtop.org/schema/spring-top http://www.comtop.org/schema/top/spring-top.xsd"> <!-- spring 注解 --> <context:component-scan base-package="com.dwr"/> <!-- 这句的作用是表示允许DWR访问Spring的Context 需要加上id--> <dwr:annotation-config id="dwr_as"/> <!-- 扫描加了注解@RemoteProxy & @RemoteMethod 的对象 --> <dwr:annotation-scan scanRemoteProxy="false" base-package="com.dwr" /> <!-- dwr初始化配置 --> <dwr:configuration></dwr:configuration> <import resource="RESTful.xml" /> </beans>
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.4"> <display-name>RestFulServer</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 设置Spring组配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- Spring容器初始化监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</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> </web-app>
3.DWRTest.java
package com.dwr; import org.directwebremoting.annotations.RemoteMethod; import org.directwebremoting.annotations.RemoteProxy; import org.springframework.stereotype.Controller; @Controller @RemoteProxy public class DWRTest { @RemoteMethod public String getResult(String ddd) { return ddd; } }
必须加上@Controller否则页面找不到DWRTest
4.页面配置
<!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"> <title>Insert title here</title> <script language="javascript" src="/RestFulServer/common/jquery1.8.2.js" ></script> <script src="/RestFulServer/dwr/engine.js"></script> <script src="/RestFulServer/dwr/interface/DWRTest.js"></script> <script type="application/javascript"> window.onload = function(){ alert($("#test").text()); DWRTest.getResult("test",function(result){ alert(result); }); } </script> </head> <body> <span id="test">2222222222222222</span> </body> </html>