`
conkeyn
  • 浏览: 1505062 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

DWR 2.0M3 学习笔记

阅读更多

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

第一个 HelloWorld 程序!

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

首先需要配置“ DWR 环境”,那样才能使用 javascript 调用 java 类的代码

<!-- [if !supportLists]-->1、  <!-- [endif]-->添加 dwr.jar [web-app]/WEB-INF/lib 目录中。

<!-- [if !supportLists]-->2、  <!-- [endif]--> [web-app]/WEB-INF 目录下创建 dwr.xml 文件,并写入以下代码:

<? xml version = "1.0" encoding = "UTF-8" ?>

<! DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd" >

< dwr >

            < allow >

               

            </ allow >

</ dwr >

<!-- [if !supportLists]-->3、  <!-- [endif]-->修改 web.xml 文件,内容如下

< servlet >

            < servlet-name > dwr-invoker </ servlet-name >

            < servlet-class >

                org.directwebremoting.servlet.DwrServlet

            </ servlet-class >

            < init-param >

                < param-name > debug </ param-name >

                < param-value > true </ param-value >

            </ init-param >

        </ servlet >

        < servlet-mapping >

            < servlet-name > dwr-invoker </ servlet-name >

            < url-pattern > /dwr/* </ url-pattern >

</ servlet-mapping >

              如果使用 logging 时, commons-logging log4j 是默认的配置,当然也可以使用 HttpServlet.log() 方法。

 

              <init-param>

       <param-name>logLevel</param-name>

                     <param-value>DEBUG</param-value>

</init-param>

有效值为: FATAL, ERROR, WARN (the default), INFO and DEBUG.

这样就把 DWR 环境配置好了!那么开始第一个 HelloWorld 程序

第一步:编写 HelloWorld.java

package com.willmobile.ajaxtm;

      

public class HelloWorld {

              public String sayHelloWorldTo(String name) {

                     return "Hello World " + name + "!";

              }

}

第二步:编辑 dwr.xml 文件,在 <allow> 标签内添加下面代码:

    <create creator="new" javascript="Helloworld" scope="page">

            <param name="class" value="com.willmobile.ajaxtm.HelloWorld" />

    </create>

    为了与javaHelloWorld 类表示区别javascriptHelloworld 的“w ”是小写的

第三步:把web 应用发布到tomcatwebapps 里面, 运行tomcat 服务器, 然后输入以下

 

http://localhost:8080/web-app /dwr/

 

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

把上面的三个<script> 都拷贝起来,并且在web-app 下面创建一个index.jsp 的页面。其代码如下:

<? xml version = "1.0" encoding = "UTF-8" ?>

< html >

< head >

< script type = 'text/javascript' src = '/learnAjax/dwr/interface/Helloworld.js' ></ script >

  < script type = 'text/javascript' src = '/learnAjax/dwr/engine.js' ></ script >

< script type = 'text/javascript' src = '/learnAjax/dwr/util.js' ></ script >

< script type = "text/javascript" >

window.onload = function () {

    Helloworld.sayHelloWorldTo( "JavaTwo 2008" , function (returnStr) {

        $( 'output' ).innerHTML = returnStr;

    } );

}

</ script >

</ head >

< body >

< h2 id = "output" ></ h2 >

</ body >

</ html >

第四步:输入 http://localhost:8080/web-app 即可看到想要的结果了!

 

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <span class="mceItemObject" classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </span> <style> st1\:*{behavior:url(#ieooui) } </style> <![endif]--> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

创建一个<creator><converter> 结合的例子:

    第一步:创建一个Person bean

    package com.my.learn.beanConvertor;

public class Person {

        private String name ;

        private int age ;

        public int getAge() {

            return age ;

        }

        public void setAge( int age) {

            this . age = age;

        }

        public String getName() {

            return name ;

        }

        public void setName(String name) {

            this . name = name;

        }

}

第二步:创建一个Remote bean ,这个做为远程调用的

package com.my.learn.beanConvertor;

public class Remoted {

        private Person p ;

        public void setPerson(Person p) {

            this . p = p;

            System. out .println( "The person's name is: " + p.getName()

                    + " and age is: " + p.getAge()); // 查看运行效果

        }

}

    第三步:在<allow> 标签里注册bean

    < create javascript = "remoted" creator = "new" >

            < param name = "class" value = "com.my.learn.beanConvertor.Remoted" />

    </ create >

    < convert match = "com.my.learn.beanConvertor.Person" converter = "bean" />// 这个转换是为Remoted.setPerson(Person) 转换类型用的。也就是指从javascript 传参数到java 时需要转换

 

    如果使converter = "object" 的话,那么还得需要一个参数,才能与converter = "bean" 达到相的效果。如下:

    < convert match = "com.my.learn.beanConvertor.Person" converter = "object" >

            < param name = "force" value = "true" />

    </ convert >

    第四步:

        输入http://localhost:8080/learndwr/dwr 复制三个<script> 所以定的脚本文件, 并且粘贴到新建的html 网页里头

    <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >

< html >

  < head >

    < script type = 'text/javascript' src = '/learndwr/dwr/interface/remoted.js' ></ script >

  < script type = 'text/javascript' src = '/learndwr/dwr/engine.js' ></ script >

  < script type = 'text/javascript' src = '/learndwr/dwr/util.js' ></ script >

 

    < script type = "text/javascript" >

        var p = { name: "Fred" ,age:21 } ;

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]-->

  • 大小: 18.7 KB
  • 大小: 7.8 KB
  • 大小: 40.6 KB
  • 大小: 8.6 KB
  • 大小: 8.6 KB
  • 大小: 10.3 KB
  • 大小: 14.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics