DWR3与spring集成(不使用springMVC,但使用注解的实例)

来源:互联网 发布:炸微信群软件下载 编辑:程序博客网 时间:2024/06/06 12:47
web.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>      <!DOCTYPE web-app PUBLIC          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"          "http://java.sun.com/dtd/web-app_2_3.dtd">            <web-app id="dwr_dev">              <listener>           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>         </listener>         <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>             classpath:dwr3SampleAppSpring3AnnotNonSpringMVC.xml           </param-value>         </context-param>               <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>  

spring的bean配置文件
    <?xml version="1.0" encoding="UTF-8"?>      <beans xmlns="http://www.springframework.org/schema/beans"             xmlns:context="http://www.springframework.org/schema/context"             xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd             http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">                 <dwr:configuration />            <dwr:annotation-scan base-package="org.uk.ltd.dwr.dev" scanDataTransferObject="true" scanRemoteProxy="true"/>         </beans>  

java文件
    package org.uk.ltd.dwr.dev.model;            import org.directwebremoting.annotations.DataTransferObject;            @DataTransferObject      public class Address {          private String street;          private String street2;          private String city;          private String state;                    public String getStreet() {              return street;          }          public void setStreet(String street) {              this.street = street;          }             public String getStreet2() {              return street2;          }             public void setStreet2(String street2) {              this.street2 = street2;          }          public String getCity() {              return city;          }             public void setCity(String city) {              this.city = city;          }             public String getState() {              return state;          }             public void setState(String state) {              this.state = state;          }         }  

    package org.uk.ltd.dwr.dev.service;            import org.directwebremoting.annotations.RemoteMethod;      import org.directwebremoting.annotations.RemoteProxy;      import org.uk.ltd.dwr.dev.model.Address;            @RemoteProxy(name="dwrService")      public class DWRService {                    public DWRService() { }                    @RemoteMethod          public Address getAddress() throws Exception {              Address address = new Address();                      address.setStreet("2245 NW Overlook Drive");              address.setCity("Portland");              address.setState("Oregon");              return address;          }                    @RemoteMethod          public void printAddress() {              System.out.println("Printing");          }      }  

    <html>          <head>              <title>DWR Dev</title>              <script type="text/javascript" src="/dwr3SampleAppSpring3AnnotNonSpringMVC/dwr/engine.js"></script>              <script type="text/javascript" src="/dwr3SampleAppSpring3AnnotNonSpringMVC/dwr/util.js"></script>              <script type="text/javascript" src="/dwr3SampleAppSpring3AnnotNonSpringMVC/dwr/interface/dwrService.js"></script>                 <script>                  function getDataFromServer() {                    dwrService.getAddress({                      callback: getDataFromServerCallBack                    });                  }                  function getDataFromServerCallBack(dataFromServer) {                    alert(dwr.util.toDescriptiveString(dataFromServer, 3));                  }              </script>          </head>          <body>              <h3> 3.x/Spring 3.x with Annotations and Spring MVC</h3>              <a href="#" onclick="getDataFromServer(); return false;">Retrieve test data</a><br/>          </body>      </html>  

0 0