让Dwr和Spring-MVC通过Annotation方式共同工作

引用
SPRING2.X后引入了@Controller的方式标注Controller,大大简化了原来的XML方式的MVC配置,不过传统的 DWR配置方式,虽然可以通过SpringCreator的方式将BEAN暴露给AJAX使用,但是仍然逃不出繁杂的XML配置,通过 SPRINGannotation方式,可以简化 DWRSPRING之间的配置. 

web.xml 
Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%3C!--%20Spring%20--%3E%0A%09%3Cservlet%3E%0A%09%09%3Cservlet-name%3Espring%3C%2Fservlet-name%3E%0A%09%09%3Cservlet-class%3E%0A%09%09%09org.springframework.web.servlet.DispatcherServlet%0A%09%09%3C%2Fservlet-class%3E%0A%09%09%3Cinit-param%3E%0A%09%09%09%3Cparam-name%3EcontextConfigLocation%3C%2Fparam-name%3E%0A%09%09%09%3Cparam-value%3E%0A%09%09%09%09classpath%3Aspring%2FwebControllerContext.xml%0A%09%09%09%3C%2Fparam-value%3E%0A%09%09%3C%2Finit-param%3E%0A%09%09%3Cload-on-startup%3E1%3C%2Fload-on-startup%3E%0A%09%3C%2Fservlet%3E%0A%09%3Cservlet-mapping%3E%0A%09%09%3Cservlet-name%3Espring%3C%2Fservlet-name%3E%0A%09%09%3Curl-pattern%3E*.htm%3C%2Furl-pattern%3E%0A%09%3C%2Fservlet-mapping%3E%0A%09%3Cservlet-mapping%3E%0A%09%09%3Cservlet-name%3Espring%3C%2Fservlet-name%3E%0A%09%09%3Curl-pattern%3E%2Fdwr%2F*%3C%2Furl-pattern%3E%0A%09%3C%2Fservlet-mapping%3E%0A"></embed>
  1. <!-- Spring -->  
  2.     <servlet>  
  3.         <servlet-name>spring</servlet-name>  
  4.         <servlet-class>  
  5.             org.springframework.web.servlet.DispatcherServlet  
  6.         </servlet-class>  
  7.         <init-param>  
  8.             <param-name>contextConfigLocation</param-name>  
  9.             <param-value>  
  10.                 classpath:spring/webControllerContext.xml  
  11.             </param-value>  
  12.         </init-param>  
  13.         <load-on-startup>1</load-on-startup>  
  14.     </servlet>  
  15.     <servlet-mapping>  
  16.         <servlet-name>spring</servlet-name>  
  17.         <url-pattern>*.htm</url-pattern>  
  18.     </servlet-mapping>  
  19.     <servlet-mapping>  
  20.         <servlet-name>spring</servlet-name>  
  21.         <url-pattern>/dwr/*</url-pattern>  
  22.     </servlet-mapping>  

将DispatcherServlet配置成管理htm和 dwr 

webControllerContext.xml 
Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%20xmlns%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans%22%0A%09xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%0A%09xmlns%3Ap%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Fp%22%0A%09xmlns%3Aaop%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop%22%0A%09xmlns%3Acontext%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext%22%0A%09xmlns%3Ajee%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Fjee%22%0A%09xmlns%3Atx%3D%22http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx%22%0A%09xmlns%3Adwr%3D%22http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr%22%0A%09xmlns%3Adwra%3D%22http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr-annotations%22%0A%09xsi%3AschemaLocation%3D%22%0A%09%09%09http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop%20http%3A%2F%2Fwww.springframework.org%2Fschema%2Faop%2Fspring-aop-2.5.xsd%0A%09%09%09http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans%20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans%2Fspring-beans-2.5.xsd%0A%09%09%09http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext%20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fcontext%2Fspring-context-2.5.xsd%0A%09%09%09http%3A%2F%2Fwww.springframework.org%2Fschema%2Fjee%20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fjee%2Fspring-jee-2.5.xsd%0A%09%09%09http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx%20http%3A%2F%2Fwww.springframework.org%2Fschema%2Ftx%2Fspring-tx-2.5.xsd%0A%09%09%09http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr%0A%20%20%20%20%20%20%20%20%20%20%20%20http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr-2.0.xsd%0A%20%20%20%20%20%20%20%20%20%20%20%20http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr-annotations%0A%20%20%20%20%20%20%20%20%20%20%20%20http%3A%2F%2Fwww.directwebremoting.org%2Fschema%2Fspring-dwr-annotations.xsd%22%3E%0A%09%0A%09%3Ccontext%3Aannotation-config%2F%3E%0A%09%0A%09%3Ccontext%3Acomponent-scan%20base-package%3D%22mvc%22%20%2F%3E%0A%0A%09%3Cbean%0A%09%09class%3D%22org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping%22%20%2F%3E%0A%0A%09%3C!--%20%0A%09%09%3Cbean%0A%09%09class%3D%22org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter%22%20%2F%3E%0A%09--%3E%0A%0A%09%3Cbean%0A%09%09class%3D%22org.springframework.web.servlet.view.InternalResourceViewResolver%22%0A%09%09p%3Aprefix%3D%22%22%20p%3Asuffix%3D%22.jsp%22%20%2F%3E%0A%0A%09%3C!--%20DWR%20--%3E%0A%09%3Cdwr%3Aconfiguration%3E%0A%09%09%3Cdwr%3Aconvert%20class%3D%22mvc.User%22%20type%3D%22bean%22%20%2F%3E%0A%09%09%3Cdwr%3Asignatures%3E%0A%09%09%09%3C!%5BCDATA%5B%0A%09%09%09%09import%20java.util.Map%3B%0A%09%09%09%09%5D%5D%3E%0A%09%09%3C%2Fdwr%3Asignatures%3E%0A%09%3C%2Fdwr%3Aconfiguration%3E%0A%0A%09%3Cdwr%3Acontroller%20id%3D%22dwrController%22%20debug%3D%22true%22%20%2F%3E%0A%0A%09%3C!--%20%20New%20DWR%20capabilities--%3E%0A%09%3Cdwra%3Aurl-mapping%20%2F%3E%0A%09%3Cdwra%3Aannotation-config%20%2F%3E%0A%3C%2Fbeans%3E%0A"></embed>
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:jee="http://www.springframework.org/schema/jee"  
  8.     xmlns:tx="http://www.springframework.org/schema/tx"  
  9.     xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"  
  10.     xmlns:dwra="http://www.directwebremoting.org/schema/spring-dwr-annotations"  
  11.     xsi:schemaLocation="  
  12.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  14.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  15.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  
  16.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  17.             http://www.directwebremoting.org/schema/spring-dwr  
  18.             http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd  
  19.             http://www.directwebremoting.org/schema/spring-dwr-annotations  
  20.             http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd">  
  21.       
  22.     <context:annotation-config/>  
  23.       
  24.     <context:component-scan base-package="mvc" />  
  25.   
  26.     <bean  
  27.         class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  28.   
  29.     <!--   
  30.         <bean  
  31.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  32.     -->  
  33.   
  34.     <bean  
  35.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  36.         p:prefix="" p:suffix=".jsp" />  
  37.   
  38.     <!-- DWR -->  
  39.     <dwr:configuration>  
  40.         <dwr:convert class="mvc.User" type="bean" />  
  41.         <dwr:signatures>  
  42.             <![CDATA[ 
  43.                 import java.util.Map; 
  44.                 ]]>  
  45.         </dwr:signatures>  
  46.     </dwr:configuration>  
  47.   
  48.     <dwr:controller id="dwrController" debug="true" />  
  49.   
  50.     <!--  New DWR capabilities-->  
  51.     <dwra:url-mapping />  
  52.     <dwra:annotation-config />  
  53. </beans>  

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%40Controller(%22helloController%22)%0A%40RequestMapping(%22%2FhelloController.htm%22)%0A%40RemoteProxy(creator%20%3D%20SpringCreator.class%2C%20creatorParams%20%3D%20%40Param(name%20%3D%20%22beanName%22%2C%20value%20%3D%20%22helloController%22)%2C%20name%20%3D%20%22dwrHelloController%22)%0Apublic%20class%20Hello%20%7B%0A%09%2F**%0A%09%20*%20User%20Dao%0A%09%20*%2F%0A%09private%20UserDao%20userDao%3B%0A%0A%09%2F**%0A%09%20*%20Employee%20dao%0A%09%20*%2F%0A%09private%20EmployeeDao%20employeeDao%3B%0A%0A%09%40RemoteMethod%0A%09%40RequestMapping%0A%09public%20String%20sayHello()%20%7B%0A%09%09String%20str%20%3D%20%22hello%22%3B%0A%09%09System.out.println(str)%3B%0A%09%09return%20str%3B%0A%09%7D%0A%0A%09%40RemoteMethod%0A%09public%20User%20getUser()%20%7B%0A%09%09User%20user%20%3D%20this.getUserDao().getUser()%3B%0A%09%09user.setName(user.getName()%20%2B%20%22%20_%20%22%0A%09%09%09%09%2B%20this.getEmployeeDao().getEmployeeName())%3B%0A%09%09return%20user%3B%0A%09%7D%0A%0A%09%40RequestMapping(params%20%3D%20%22method%3DsayBye%22)%0A%09public%20ModelAndView%20sayBye()%20%7B%0A%09%09String%20str%20%3D%20%22bye%22%3B%0A%09%09System.out.println(str)%3B%0A%09%09return%20null%3B%0A%09%7D%0A%0A%09%2F%2F...%E5%BF%BD%E7%95%A5set%E5%92%8Cget%0A%7D%0A"></embed>
  1. @Controller("helloController")  
  2. @RequestMapping("/helloController.htm")  
  3. @RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "helloController"), name = "dwrHelloController")  
  4. public class Hello {  
  5.     /** 
  6.      * User Dao 
  7.      */  
  8.     private UserDao userDao;  
  9.   
  10.     /** 
  11.      * Employee dao 
  12.      */  
  13.     private EmployeeDao employeeDao;  
  14.   
  15.     @RemoteMethod  
  16.     @RequestMapping  
  17.     public String sayHello() {  
  18. font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px
分享到:
评论

你可能感兴趣的:(spring,工作,mvc,servlet,DWR)