DWR全称:Direct Web Remoting.

可以实现通过javascript直接调用java方法.

1.修改web.xml文件 , 加入dwrServlet

    <servlet>

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

        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

       <!-- This should NEVER be present in live -->

       <init-param>

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

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

       </init-param>

       <!-- Remove this unless you want to use active reverse ajax -->

       <init-param>

           <param-name>activeReverseAjaxEnabled</param-name>

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

       </init-param>

       <!--

           By default DWR creates application scope objects when they are first

           used. This creates them when the app-server is started

       -->

       <init-param>

           <param-name>initApplicationScopeCreatorsAtStartup</param-name>

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

       </init-param>

       <!--

           This enables full streaming mode. It's probably better to leave this

           out if you are running across the internet

       -->

       <init-param>

           <param-name>maxWaitAfterWrite</param-name>

           <param-value>-1</param-value>

       </init-param>

       <!--

           For more information on these parameters, see: -

           http://getahead.org/dwr/server/servlet -

           http://getahead.org/dwr/reverse-ajax/configuration

       -->

       <load-on-startup>1</load-on-startup>

    </servlet>

 

    <servlet-mapping>

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

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

    </servlet-mapping>

 

2.WEB-INF下加入drw.xml文件,文件内容如下:

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

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

 

<dwr>

 

  <allow>

    <!-- creator属性为new,表示需要new来创建对象实例,javascript的值是自定义的,对应页面中引用的js脚本对象的名字(xxx.js)-->

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

  <!-- param定义要发布的java -->

      <param name="class" value="com.test.Test"/>

</create>

  <!-- convert在交给Spring管理时需要用上

     <convert converter="bean" match="com.domain.Book"/>

      -->

  </allow>

</dwr>

<allow>标签中包括可以暴露给javascript访问的东西。

<create>标签中指定javascript中可以访问的java类,并定义DWR应当如何获得要进行远程的类的实例。

creator="new"属性指定java类实例的生成方式,new意味着DWR应当调用类的默认构

造函数来获得实例,其他的还有spring方式,通过与IOC容器Spring进行集成来获  得实例等等。

javascript=" testClass "属性指定javascript代码访问对象时使用的名称。

<param>标签指定要公开给javascript的java类名。

<include>标签指定要公开给javascript的方法。不指定的话就公开所有方法。

<exclude>标签指定要防止被访问的方法。 



3.在页面中导入js脚本

<!-- 第一个js脚本名就是在dwr.xml文件中自定义的那个名字 -->

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

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

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

       <script type='text/javascript'>

//////调用发布的java类中的ss方法,第一个参数是java方法中需要的参数,第二个是在页面中需要回调的参数名

//////会自动把java方法的返回值返回给回调函数的参数

       function ff(){

                  TT.ss("123456",han);

              }

              var han=function(mm){ ///mmjava类的ss方法的返回值

                  alert(mm);

              }

              ////调用java类中的findList方法,返回一个List,自动传给回调函数的参数

              function xx(){

                  TT.findList(hh);

              }

              var hh=function(objList){

                  for(var i=0;i<objList.length;i++)

                  {

                     var book=objList[i];

                     alert(book.id+"-----"+book.name);

                  }

              }

              ////////调用java类中的findMap方法,返回一个Map,自动传给回调函数的参数/////////

              function xxx(){

                  TT.findMap(gg);

              }

              var gg=function(ms){

                  for(var m in ms){

                     var bean=ms[m];

                     alert(bean.id+"==============="+bean.name);

                  }

              }

              /////直接通过jsjava类中的对象赋值/////

              function objtest(){

                  var book={id:null,name:null};

                  DWRUtil.getValues(book);

                  TT.pri(book,null);

              }

              //调用DWR的函数

              function cc(){

                  alert(DWRUtil.getValue("name"));

              }

        </script>

 

 

 

    <body>

       This is my JSP page.

       <br>

       <input type="text" name="id"/> <br/>

       <input type="text" name="name"/> <br/>

      

       <input type="button" value="test" onclick="objtest()"/>

       <hr/>

       <input type="button" value="tt" onclick="cc()"/>

    </body>