`
conkeyn
  • 浏览: 1518023 次
  • 性别: 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
分享到:
评论

相关推荐

    DWR2.0 中文手册

    `ajax框架:dwr》实战(包括整合).pdf`这本书籍应该包含了实际项目中的应用案例和整合技巧,是学习DWR 2.0实战经验的重要资源。 通过深入学习DWR 2.0,开发者可以构建出交互性强、响应速度快的Web应用,同时降低前后...

    DWR2.0 整合Spring2.0

    《DWR2.0与Spring2.0整合详解》 Direct Web Remoting (DWR) 是一个开源的Java库,它允许在JavaScript和Java之间进行实时的、安全的、跨域的通信,使得Web应用程序可以像桌面应用一样具有丰富的用户交互体验。DWR2.0...

    DWR2.0中文文档

    在**DWR2.0中文文档.pdf**中,读者将逐步学习DWR的安装、配置、使用,以及如何在实际项目中实现各种功能。书中还会通过示例代码和案例分析来帮助读者掌握DWR的精髓,从而在自己的项目中高效地运用这项技术。 总的来...

    dwr2.0 dwr3.0 jar包

    这个主题主要围绕DWR的两个主要版本——DWR 2.0和DWR 3.0,以及它们与jar包的关联。 首先,DWR 2.0是DWR项目的一个早期版本,它引入了核心的远程方法调用(Remote Method Invocation, RMI)功能,使得JavaScript...

    dwr 2.0 的 dtd 和官方网站首页

    DWR 2.0 是DWR的一个版本,提供了许多增强的功能和改进,以适应不断发展的Web开发需求。 DTD(Document Type Definition)是XML文档类型定义,它定义了XML文档的结构和语法规则。对于DWR,DTD文件是用于验证DWR配置...

    dwr2.0学习例子

    这个"**dwr2.0学习例子**"提供了实践DWR 2.0特性的实例,帮助开发者快速理解和应用DWR。 **DWR框架的核心功能:** 1. **动态代理**:DWR允许服务器端的Java对象被自动暴露到客户端的JavaScript中,使得开发者无需...

    DWR3.0.jar、DWR.war和DWR2.0中文说明文档

    这个压缩包包含了DWR的三个关键组件:DWR3.0.jar,DWR的实例war文件,以及DWR2.0的中文说明文档。以下是关于这些组件的详细知识: 1. **DWR3.0.jar**: DWR3.0.jar是DWR框架的核心库,包含所有必需的类和接口,...

    dwr2.0最简单例子实用亲测试

    在“dwr2.0最简单例子实用亲测试”中,我们可以预期这是一个关于如何使用DWR 2.0版本的入门教程,包含了实际可运行的示例代码。下面将详细介绍DWR的基本概念和在这个实例中可能涉及的关键知识点: 1. **DWR的核心...

    Dwr3.0 与 Dwr2.0 区别

    ### DWR 3.0 与 DWR 2.0 的区别 DWR(Direct Web Remoting)是一种用于简化 AJAX 应用程序开发的技术。它允许客户端 JavaScript 直接调用服务器端 Java 方法,从而降低了 AJAX 编程的复杂度。DWR 的最新版本为 3.0...

    dwr2.0 spring 3.0 整合示例

    DWR 2.0 版本是在DWR早期版本的基础上发展起来的,它提供了一系列增强功能,包括更好的性能和更多的API选项。Spring 3.0 是一个广泛使用的Java企业级应用框架,提供了依赖注入、AOP(面向切面编程)以及众多其他功能...

    DWR2.jar + DWR.xml + DWR2.0.dtd + Web.xml

    DWR2.0是DWR的一个版本,提供了一些增强特性和改进。 **DWR2.jar** 是DWR的主要库文件,包含了DWR框架的所有核心组件和实现。这个JAR文件包含了许多类,如`DWRServlet`,它是DWR与HTTP请求交互的核心组件,以及各种...

    简单购物车(dwr2.0)

    【标题】"简单购物车(dwr2.0)"是一个基于Web的购物车应用程序,它利用了Direct Web Remoting (DWR) 2.0框架。DWR是一种JavaScript库,允许在浏览器和服务器之间进行实时、异步的通信,使得前端与后端的数据交换变得...

    dwrTest.rar_dwr 3 jar_dwr jar_dwr2.0 jar

    在"**dwrTest.rar_dwr 3.jar_dwr.jar_dwr2.0.jar**"这个压缩包中,包含了DWR框架不同版本的jar文件,以及一个可能的示例项目,便于用户学习和测试DWR的功能。 **DWR 2.0 和 3.0 的主要区别:** 1. **性能提升**:...

    dwr2.0示例

    学习和掌握DWR 2.0,不仅可以提升我们对Ajax技术的理解,也能帮助我们在开发过程中提高效率,创建出更加优秀的Web应用程序。同时,了解DWR的安全特性有助于我们在实际开发中更好地防范各种安全威胁,确保应用的稳定...

Global site tag (gtag.js) - Google Analytics