1. 先寫一個Controller
package com.myapp.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RemoteProxy
public class UserController {
@RemoteMethod
public String getUserName(int id) {
System.out.println("user id is " + id);
return "UserName: " + id;
}
@RequestMapping("/user/add.do")
public String addUser(HttpServletRequest request) {
System.out.println("this is action method");
return "/user/list.jsp";
}
}
@RemoteProxy註解告訴DWR,這個Class是我們想暴露出來的。@RemoteMethod註解告訴DWR去暴露這個指定的方法,只有加了RemoteMethod註解的方法會被暴露,其它的不會。
這裏也可以使用@RemoteProxy(name="userRemote")的方式指定DWR接口的名字
2. 接下來看web.xml的配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springconfig/*.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注意,這裏沒有使用org.directwebremoting.spring.DwrSpringServlet或 org.directwebremoting.servlet.DwrServlet,並且請注意這裏使用spring的dispatcher servlet來映射/dwr/*請求。
3. 最後看針對DispatcherServlet的配置文件action-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
default-autowire="byName">
<context:annotation-config />
<!-- 注意這裏新增加的dwr tag, 爲使其生效,文件頭中要聲明namespace -->
<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<!-- 部署項目時, 請把debug設爲false -->
<dwr:controller id="dwrController" debug="true" />
<!-- 多個包名用逗號隔開, 但不能有空格 -->
<context:component-scan base-package="com.myapp.web.controller" />
<!-- order值越小, 優先級越高 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
</beans>
簡單解釋一下這些配置
<dwr:annotation-config /> 要求DWR在Spring容器中檢查擁有@RemoteProxy 和 @RemoteMethod註解的類。注意它不會去檢查Spring容器之外的類。
<dwr:url-mapping /> 要求DWR將util.js和engine.js映射到dwrController
<dwr:controller id="dwrController" debug="true" /> 定義dwrController
<dwr:configuration /> 此標籤在這個例子中不是必須的,如果你想配置Spring容器之外的類,就需要它了。
最後一件事,DWR的測試頁面 http://localhost:8080/myapp/dwr 在這裏不好用。
請使用 http://localhost:8080/myapp/dwr/test/YOUR-BEAN-NAME 的方式來測試你的DWR接口,
例如這裏使用 http://localhost:8080/myapp/dwr/test/UserController
在spring配置文件最後加上
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="order" value="3" />
<property value="true" name="alwaysUseFullPath"></property>
<property name="mappings">
<props>
<prop key="/dwr/**">dwrController</prop>
</props>
</property>
</bean>