配置整合DWR3.0和Spring2.5使用annotation注解

配置整合DWR3.0和Spring2.5使用annotation注解


这里使用 DWR3.rc1, Spring2.5 and Spring MVC



在Spring2.5中,使用了许多annotation, 新版本的DWR也支持annotation了, 下面看一下配置过程



1. 先写一个Controller


Java代码 复制代码

1. package com.myapp.web.controller;
2.
3. import javax.servlet.http.HttpServletRequest;
4. import org.directwebremoting.annotations.RemoteMethod;
5. import org.directwebremoting.annotations.RemoteProxy;
6. import org.springframework.stereotype.Controller;
7. import org.springframework.web.bind.annotation.RequestMapping;
8.
9. @Controller
10. @RemoteProxy
11. public class UserController {
12.
13. @RemoteMethod
14. public String getUserName(int id) {
15. System.out.println("user id is " + id);
16. return "UserName: " + id;
17. }
18.
19. @RequestMapping("/user/add.do")
20. public String addUser(HttpServletRequest request) {
21. System.out.println("this is action method");
22. return "/user/list.jsp";
23. }
24. }

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的配置


Xml代码 复制代码

1. <servlet>
2. <servlet-name>action</servlet-name>
3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4. <load-on-startup>1</load-on-startup>
5. </servlet>
6.
7. <context-param>
8. <param-name>contextConfigLocation</param-name>
9. <param-value>/WEB-INF/springconfig/*.xml</param-value>
10. </context-param>
11.
12. <servlet-mapping>
13. <servlet-name>action</servlet-name>
14. <url-pattern>*.do</url-pattern>
15. </servlet-mapping>
16.
17. <servlet-mapping>
18. <servlet-name>action</servlet-name>
19. <url-pattern>/dwr/*</url-pattern>
20. </servlet-mapping>
21.
22. <listener>
23. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24. </listener>

<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


Xml代码 复制代码

1. <beans xmlns="http://www.springframework.org/schema/beans"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans
6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7. http://www.springframework.org/schema/context
8. http://www.springframework.org/schema/context/spring-context-2.5.xsd
9. http://www.directwebremoting.org/schema/spring-dwr
10. http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
11. default-autowire="byName">
12.
13. <context:annotation-config />
14.
15. <!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace -->
16. <dwr:configuration />
17. <dwr:annotation-config />
18. <dwr:url-mapping />
19.
20. <!-- 部署项目时, 请把debug设为false -->
21. <dwr:controller id="dwrController" debug="true" />
22.
23. <!-- 多个包名用逗号隔开, 但不能有空格 -->
24. <context:component-scan base-package="com.myapp.web.controller" />
25.
26. <!-- order值越小, 优先级越高 -->
27. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
28. <property name="order" value="1" />
29. </bean>
30. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
31. <property name="order" value="2" />
32. </bean>
33.
34. </beans>

<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->

<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



OK 运行测试一下吧
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值