`
sunxboy
  • 浏览: 2886118 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

简单使用dwr(转)

阅读更多

 1、载入dwr的jar文件
              下载地址:http://www.cnweblog.com/Files/jimmy/dwr.rar    (将后缀名改为jar)

2、在web.xml中配制

 <servlet>
     <servlet-name>dwr-invoker</servlet-name> 
     <servlet-class>uk.ltd.getahead.dwr.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> 

 
3、在WEB-INF下建立dwr.xml和dwr20.dtd两个文件
复制以下内容到dwr20.dtd文件中,保存:

<?xml version="1.0" encoding="UTF-8"?>
<!--  This is the DTD for DWR v2.x  -->

<!--
Top level conviguration element.
-->
<!ELEMENT dwr (
    (init?), (allow?), (signatures?)
) >

<!--
A list of all the classes to configure as part of dwr at startup time.
-->
<!ELEMENT init (
    (creator | converter)*
) >

<!--
Define a new method of creating objects for use by Javascript.
We don't just allow access to any object and some may need special code to
get a reference to them.
-->
<!ELEMENT creator EMPTY >
<!--
@attr id The unique name by which create elements refer to us.
@attr class The fully qualified name of a class that implements Creator.
-->
<!ATTLIST creator
    id ID #REQUIRED
    class CDATA #REQUIRED
>

<!--
Define a new way of converting between javascript objects and java objects.
Many classes can have default conversion mechanisms but some require more
custom conversion
-->
<!ELEMENT converter EMPTY >
<!--
@attr id The unique name by which convert elements refer to us.
@attr class The fully qualified name of a class that implements Converter.
-->
<!ATTLIST converter
    id ID #REQUIRED
    class CDATA #REQUIRED
>

<!--
Security: we must define which classes we are allowed to access because a
free-for-all will be very dangerous.
-->
<!ELEMENT allow (
    (create | convert | filter)*
) >

<!--
Allow the creation of a class, and give it a name in javascript land.
A reference to a creator is required as are some parameters specific to each
creator that define the objects it allows creation of.
It would be nice to make the creator and IDREF rather than a CDATA, since it
refers to an element defined elsewhere, however we allow multiple dwr.xml
files and we might refer to one in another file.
-->
<!ELEMENT create (
    (param | include | exclude | auth | filter)*
) >
<!--
@attr creator The id of the creator to use
@attr javascript The name of the object to export to the browser
@attr scope The scope of the created variable. The default is page.
-->
<!ATTLIST create
    creator CDATA #REQUIRED
    javascript CDATA #REQUIRED
    scope (application | session | script | request | page) #IMPLIED
>

<!--
A filter is a way to insert procesing tasks at various points during the
processing of an Ajax call. See org.directwebremoting.AjaxFilter
-->
<!ELEMENT filter (
    (param)*
) >
<!--
@attr class The class name to use to filter requests
-->
<!ATTLIST filter
    class CDATA #REQUIRED
>

<!--
Some elements (currently only create although there is no hard reason why
convert elements should not be the same) need customization in ways that we
can't predict now, and this seems like the only way to do it.
-->
<!ELEMENT param (#PCDATA) >
<!--
@attr name The name of the parameter to this creator
@attr value The value to set to the names parameter
-->
<!ATTLIST param
    name CDATA #REQUIRED
    value CDATA #IMPLIED
>

<!--
A creator can allow and disallow access to the methods of the class that it
contains. A Creator should specify EITHER a list of include elements (which
implies that the default policy is denial) OR a list of exclude elements
(which implies that the default policy is to allow access)
-->
<!ELEMENT include EMPTY >
<!--
@attr method The method to include
-->
<!ATTLIST include
    method CDATA #IMPLIED
>

<!--
See the include element
-->
<!ELEMENT exclude EMPTY >
<!--
@attr method The method to exclude
-->
<!ATTLIST exclude
    method CDATA #IMPLIED
>

<!--
The auth element allows you to specify that the user of a given method must be
authenticated using J2EE security and authorized under a certain role.
-->
<!ELEMENT auth EMPTY >
<!--
@attr method The method to add role requirements to
@attr role The role required to execute the given method
-->
<!ATTLIST auth
    method CDATA #REQUIRED
    role CDATA #REQUIRED
>

<!--
Allow conversion of a class between Java and Javascript.
A convert element uses a previously defined converter and gives a class match
pattern (which can end with *) to define the classes it allows conversion of
It would be nice to make the converter and IDREF rather than a CDATA, since it
refers to an element defined elsewhere, however we allow multiple dwr.xml
files and we might refer to one in another file.
-->
<!ELEMENT convert (
    (param)*
) >
<!--
@attr converter The id of the converter to use
@attr match A class name to match for conversion
@attr javascript The optional classname for the parameter
-->
<!ATTLIST convert
    converter CDATA #REQUIRED
    match CDATA #REQUIRED
    javascript CDATA #IMPLIED
>

<!--
If we are marshalling to collections, we need to be able to specify extra
type information to converters that are unable to tell from reflection what to
do. This element contains some Java method definitions
-->
<!ELEMENT signatures (#PCDATA) >

以下是dwr.xml的文件内容详解:

 

<!DOCTYPE dwr PUBLIC 
     "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" 
     "dwr20.dtd">
<dwr>
  <!-- without allow, DWR isn't allowed to do anything -->
  <allow>   
    <create creator="new" javascript="js的名字">
      <param name="class" value="类的地址"></param>
    </create>  

    <!--举例-->
    <create creator="new" javascript="NewsTypeAjax">
      <param name="class" value="com.jetsum.pbcbank.news.ajax.NewsTypeAjax"></param>
    </create>  
    
    <!-- 整合Spring -->
    <create creator="spring" javascript="vbaoCommon">
      <param name="beanName" value="vbaoCommonService"/>
      <include method="checkAuthcode" />
   </create>	
  </allow>

  <signatures>
    <![CDATA[
    import java.util.List;
    ]]>
  </signatures>

</dwr>
 

 

4、Ajax类的写法很简单,就和一般的类写法一样:

package com.jetsum.pbcbank.news.ajax;

import com.jetsum.pbcbank.news.dao.TypeDao;

public class NewsTypeAjax {

    public NewsTypeAjax() {
    }
   
    public boolean checkNewsTypeExist(String typename)
    {
        boolean result = false;
        TypeDao dao = new TypeDao();
        if(dao.newsTypeExist(typename))
            result = true;
        return result;
    }
}
 

 

 

5、页面上调用写好的dwr:

<!--在导入自定义的dwr之间必须先导入/dwr/engine.js和dwr/util.js否则会报错-->
<script language="javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/util.js"></script>

<!--导入自定义的dwr文件,即dwr.xml中所配制的js名,前面加上/dwr/interface/路径-->
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/interface/NewsTypeAjax.js"></script>

<script type="text/javascript">
<!--举例-->
function typeExist(){
<!-- 1、获取页面数据-->
    var typename=document.getElementById('newtypename').value;
    typename=typename.replace(/(^\s*)| (\s*$)/g, "");
    if(typename.length==0){
        alert('请输入新闻类型名称');
            document.getElementById('newtypename').focus();
            return false;
    }
<!-- 2、设置为异步的,一般情况下为异步-->
    DWREngine.setAsync(false);

<!-- 3、调用写好的ajax类中的方法,有两个参数,第一个是传入方法的数据,第二个是回调函数,flag为调用的方法所返回的值。-->
    NewsTypeAjax.checkNewsTypeExist(typename,function (flag){
        if(flag==true)
        {
            alert("该新闻类别已经存在!");
            return false;
        }
        else
        {
            add();
        }
    });
</script>
 

 

 

 

 

分享到:
评论

相关推荐

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

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

    dwr初步,DWR的简单实用。

    Direct Web Remoting (DWR) 是一种开源Java库,它允许在Web应用程序中实现JavaScript与...DWR不仅可以用于简单的数据获取,还可以支持更复杂的交互,如异步数据更新、文件上传等,极大地增强了Web应用的功能和性能。

    Dwr例子带注释

    DWR通过AJAX技术提供了一种简单的方法来更新网页的部分内容,而无需刷新整个页面,提高了用户体验。这个"TestDwr"压缩包文件提供的例子是一个简单的DWR应用示例,适合初学者理解和学习。 1. **DWR基本概念**: - ...

    DWR 实用例子小集

    **DWR(Direct Web Remoting)实用例子小集** DWR(Direct Web Remoting)是一种在Web应用程序中实现Ajax(Asynchronous JavaScript and XML)技术的Java库。它允许JavaScript在客户端与服务器端进行直接交互,使得...

    dwr.jar dwrUtil常见方法 dwr详细资料

    1. **DWR框架概述**:DWR允许Web应用程序在客户端和服务器之间交换数据和调用方法,提供了丰富的API和配置选项,使得前后端交互更加简单。 2. **DWRUtil类**:DWRUtil是DWR提供的一系列实用工具函数的集合,包括但...

    DWR 很实用的例子

    **DWR(Direct Web Remoting)实用案例** DWR 是一个开源 Java 库,它允许在浏览器和服务器之间进行实时的、双向的通信,无需使用插件或 Flash。这一技术极大地简化了 AJAX 应用程序的开发,因为它提供了一种简单的...

    Ajax学习——DWR的参考书和实用案例学习总结

    总结来说,DWR作为Ajax开发的利器,通过其简单易用的API和丰富的功能,可以帮助开发者快速构建动态、交互性强的Web应用。结合参考书和实际案例,深入学习DWR,不仅可以提升技能,也能更好地理解和应用Ajax技术。

    DWR中文文档--简单实用的小型Ajax框架

    DWR作为一种简单实用的小型Ajax框架,通过简化前后端之间的通信流程,提高了开发效率和用户体验。通过本文档的学习,你可以了解到DWR的基本使用方法及其与各种Java Web框架的整合技巧,从而更好地利用DWR完成项目...

    Practical.DWR.2.Projects

    《实用DWR 2项目》一书由Frank W. Zammetti撰写,是关于DWR(Direct Web Remoting)框架的深入指南。本书通过六个完整的Web 2.0应用程序实例,详细介绍了如何利用DWR这一在Ajax领域备受瞩目的库进行设计与构建。 ##...

    最简单实用的Ajax框架么,DWR

    DWR简化了客户端JavaScript与服务器端Java对象之间的交互,提供了简单易用的API,使得开发人员可以像调用本地方法一样调用服务器上的方法。 DWR的核心功能包括: 1. **自动处理JavaScript与Java之间的类型转换**:...

    自己写的Quartz+dwr发异步请求的例子简单实用

    自己写的Quartz例子简单实用。包含所有jar包,部属到tomcat下就可运行。tomcat窗口启动后每10秒调一次execute()。另外还用到了DWR,在jsp中点submit就会发送异步请求回调rollBack方法。简单明了,很适合初学者!

    dwr教程+dwr.jar+util.js+engine.js

    Direct Web Remoting (DWR) 是一个开源Java库,它允许Web应用程序在客户端JavaScript和服务器端Java之间进行安全、简单的异步通信。这个教程包含了DWR的核心组件和使用示例,帮助开发者理解并掌握DWR的技术特点和...

    dwr入门 dwr学习资料

    DWR的核心功能是提供了一个简单的方法来调用服务器上的Java方法,并将结果直接返回到客户端的JavaScript中。 本套DWR学习资料旨在帮助初学者快速掌握DWR的基本概念和使用技巧。通过实例和详细的说明,你可以系统地...

    模拟dwr写的小例子

    在这个"模拟DWR写的小例子"中,我们将探讨DWR的基本原理、核心功能以及如何通过一个简单的实例来理解和应用DWR。 DWR的核心思想是提供了一种安全、高效的方法,使得JavaScript可以调用服务器上的Java方法,就像是...

    j2ee使用dwr进行后台交互

    5. **小demo**:这个项目可能包含了一个简单的示例,演示了如何在J2EE环境中集成DWR,以及如何使用DWR进行后台交互的基本步骤,这对于初学者理解和学习DWR非常有帮助。 6. **标签“dwr”**:这个标签明确了项目的...

    AJAX DWR教程

    DWR提供了一系列实用工具(如DWR Util)来帮助处理HTML表单、SELECT元素、TABLE等。例如,`DWRUtil.setValue`可以方便地设置表单元素的值,`DWRUtil.fillSelect`用于填充SELECT选项,`DWRUtil.update`可以更新指定的...

    Spring 2中整合DWR 2

    6. **测试DWR集成**:完成以上步骤后,可以通过编写简单的客户端脚本来测试DWR是否能够正常工作。例如,在HTML页面中调用服务器端的方法。 #### 四、解决XML验证错误 根据题目中的描述,在实践中可能会遇到XML验证...

    dwr3实现的无刷新文件上传

    总之,"dwr3实现的无刷新文件上传"是一个实用的教程,可以帮助开发者理解DWR3如何简化Ajax开发,特别是文件上传这一常见任务。通过学习和实践这个项目,开发者可以提升自己的技能,并将这些知识应用到更复杂的Web...

Global site tag (gtag.js) - Google Analytics