生活随笔
收集整理的這篇文章主要介紹了
配置整合DWR3.0和Spring2.5使用annotation注解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里使用 DWR3.rc1, Spring2.5 and Spring MVC
?
在Spring2.5中,使用了許多annotation, 新版本的DWR也支持annotation了, 下面看一下配置過程
?
1. 先寫一個Controller
?
Java代碼
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"; ?? ????} ?? }?? 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代碼
<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>?? <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代碼
<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>?? <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
?
OK 運行測試一下吧
總結
以上是生活随笔為你收集整理的配置整合DWR3.0和Spring2.5使用annotation注解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。