DWR通過Annotation與spring整合


          ??????DWR 2.0 增加了一個很有趣的新特性,Annotation,因此可以擺脫了dwr.xml里面的配置.同時也可以方便的和spring整合.

          ? 從官方網站下載dwr.jar包。然后將它放在你webapp的WEB-INF/lib目錄下。 修改web.xml

          ?????? beans.xml是spring的配置文件.org.springframework.web.context.ContextLoaderListener是一個監聽器.

          < ?init-param? > ?
          ?????????????
          < ?param-name? > ?classes? </ ?param-name? > ?
          ?????????????
          < ?param-value? > ?
          ???????com.spring.User,com.beans.Book
          ?????????????
          </ ?param-value? > ?
          ?????????
          </ ?init-param? > ?

          param-value:參數為被注解的類.?

          編寫相關類??????????
          ??com.beans.Book:

          @DataTransferObject(converter=BeanConverter.class)
          public ? class ?Book?{
          ????@RemoteProperty
          ????
          private ?String?name;
          ????@RemoteProperty
          ????
          private ?String?author;

          ????
          public ?String?getAuthor()?{
          ????????
          return ?author;
          ????}

          ????
          public ? void ?setAuthor(String?author)?{
          ????????
          this .author? = ?author;
          ????}

          ????
          public ?String?getName()?{
          ????????
          return ?name;
          ????}

          ????
          public ? void ?setName(String?name)?{
          ????????
          this .name? = ?name;
          ????}
          }
          ????????@DataTransferObject: 標注在客戶端和服務器之間轉換類.對應dwr.xml中的<convert>標簽.
          ???????????????注解的源代碼:
          ?????????????????????
          @Target(ElementType.TYPE)
          @Retention(RetentionPolicy.RUNTIME)
          public?@interface?DataTransferObject
          {
          ????
          /**
          ?????*?Converter?that?converts?instance?of?the?class?(default:?bean?converter).
          ?????
          */
          ????Class
          <??extends?Converter>?converter()?default?BeanConverter.class;

          ????
          /**
          ?????*?Parameters?for?the?converter.
          ?????
          */
          ????Param[]?params()?
          default?{};
          }
          ?????????關于annotation可以看這篇文章,java元數據
          ????????@RemoteProperty :標注在類中需要轉換的屬性.
          ????????????源代碼:
          ??????????????????
          @Target({?ElementType.FIELD,?ElementType.METHOD?})
          @Retention(RetentionPolicy.RUNTIME)
          public?@interface?RemoteProperty
          {
          }

          ???????如果使用dwr.xml配置,可以這樣:
          ??????
          <convert?converter="bean"?match="com.beans.Book">
          ??
          <param?name="include"?value="name,?author"/>
          </convert>

          ?User:
          @RemoteProxy(name="user",creator=SpringCreator.class,
          ????????creatorParams
          ={
          ????????????@Param(name
          ="beanName",value="user")
          ????????????})
          @DataTransferObject(converter
          =BeanConverter.class)
          public?class?User?{
          ????@RemoteProperty?
          ????
          private?String?welcome;
          ????@RemoteProperty?
          ????
          private?String?username;
          ????@RemoteProperty?
          ????
          private?String?address;
          ????@RemoteProperty?
          ????
          private?List<Book>?books;
          ????@RemoteProperty
          ????
          private?int?age;

          ????
          public?String?getAddress()?{
          ????????
          return?address;
          ????}

          ????
          public?void?setAddress(String?address)?{
          ????????
          this.address?=?address;
          ????}

          ????
          public?int?getAge()?{
          ????????
          return?age;
          ????}

          ????
          public?void?setAge(int?age)?{
          ????????
          this.age?=?age;
          ????}
          ????@RemoteMethod
          ????
          public?String?getUsername()?{
          ????????
          return?username;
          ????}

          ????
          public?void?setUsername(String?username)?{
          ????????
          this.username?=?username;
          ????}

          ????
          public?String?getWelcome()?{
          ????????
          return?welcome;
          ????}

          ????
          public?void?setWelcome(String?welcome)?{
          ????????
          this.welcome?=?welcome;
          ????}
          ????@RemoteMethod
          ????
          public?List<Book>?getBooks()?{
          ????????
          return?books;
          ????}
          ????
          ????
          public?void?setBooks(List<Book>?books)?{
          ????????
          this.books?=?books;
          ????}
          ????@RemoteMethod
          ????
          public?User?getUser(String?welcome)?{
          ????????
          this.welcome?=?welcome;
          ????????
          return?this;
          ????}

          }
          ????? @RemoteProxy:標注要給遠程調用的類.
          ???????????????RemoteProxy的name設置創造出來的對象的名字,creator指定使用那種創造器,例子中使用SpringCreator.creatorParams指定創造器的其他參數.不同的創造器參數是不同的.
          ??????@RemoteMethod:標注給遠程調用的方法

          4.修改beans.xml
          ??????

          <bean?id="remote"?class="com.spring.Remote"></bean>
          ????
          <bean?id="user"?class="com.spring.User"?
          ????????????p:username
          ="windfree"?p:address="anhui?hefei"?p:age="25">
          ????????
          <property?name="books">
          ????????????
          <list>
          ????????????????
          <ref?bean="C"/>
          ????????????????
          <ref?bean="java"/>
          ????????????
          </list>
          ????????
          </property>
          ????
          </bean>
          ????
          <bean?id="java"?class="com.beans.Book"?p:name="java"?p:author="mypure"></bean>
          ????
          <bean?id="C"?class="com.beans.Book"?p:name="C"?p:author="zgliu"></bean>
          ???其中p為spring2.0中提供的標簽.
          5.html頁面
          ??????
          <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
          <html>
          ????
          <head>
          ????????
          <title>SpringUserInfo.html</title>
          ????????
          <script?type='text/javascript'?src='/DWRExample/dwr/interface/user.js'></script>
          ????????
          <script?type='text/javascript'?src='/DWRExample/dwr/engine.js'></script>
          ????????
          <script?type='text/javascript'?src='/DWRExample/dwr/util.js'></script>
          ????????
          <script?type="text/javascript">
          ????????
          function?test(){
          ????????????user.getUser(
          "Hello",callback);???
          ????????}
          ????????
          var?cellFuncs?=?[
          ??????????????
          function(data)?{?return?data.name;?},
          ??????????????
          function(data)?{?return?data.author;?},
          ????????????];
          ????????
          function?callback(user){
          ????????????
          //alert(user.books)
          ?????????????DWRUtil.setValue('result',"歡迎你!"+"???姓名:"+user.username+",年齡:"+user.age+",住址:"+user.address);???
          ?????????????DWRUtil.addRows('tableInfo',user.books,cellFuncs,
          ?????????????????????{?escapeHtml:
          false?,
          ???????????????????rowCreator:
          function(options)?{
          ????????????????????????
          var?row?=?document.createElement("tr");
          ????????????????????????
          var?index?=?options.rowIndex?*?50;
          ????????????????????????row.style.color?
          =?"rgb("?+?index?+?",0,0)";
          ????????????????????????
          return?row;
          ??????????????????????},
          ??????????????????cellCreator:
          function(options)?{
          ????????????????????
          var?td?=?document.createElement("td");
          ????????????????????
          var?index?=?255?-?(options.rowIndex?*?50);
          ????????????????????td.style.backgroundColor?
          =?"rgb("?+?index?+?",255,255)";
          ????????????????????td.style.fontWeight?
          =?"bold";
          ????????????????????
          return?td;
          ??????????????????}
          ?????????????})
          ????????}
          ????
          </script>


          ????
          </head>

          ????
          <body>
          ????????
          &nbsp;&nbsp;&nbsp;?&nbsp;&nbsp;
          ????????
          <input?id="jbutton"?type="button"?value="取得信息"?onclick="test()"?/>
          ????????
          <br>
          ????????
          <div?id="result"></div><br>
          ????????
          <table?border="1">
          ????????????
          <thead><tr>
          ????????????????
          <th>書名</th><th>姓名</th>????????????????
          ????????????
          </tr></thead>
          ????????????
          <tbody?id="tableInfo">
          ????????????
          </tbody>
          ????????
          </table>
          ????
          </body>
          </html>
          ??????其中使用了util.js中的一些函數.
          posted on 2008-02-01 16:10 windfree 閱讀(1382) 評論(0)  編輯  收藏 所屬分類: dwr

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 隆尧县| 和静县| 滨州市| 乌海市| 筠连县| 横山县| 大兴区| 井陉县| 微博| 伽师县| 大姚县| 上虞市| 宜兴市| 大同县| 南岸区| 余干县| 福建省| 廊坊市| 达州市| 伊通| 宿迁市| 丁青县| 宣武区| 莱阳市| 六枝特区| 长子县| 鸡东县| 罗定市| 柳州市| 会泽县| 兴仁县| 临高县| 南岸区| 广州市| 贵港市| 阿拉善左旗| 临洮县| 六盘水市| 崇阳县| 孝义市| 乐都县|