-=记录我与java的点滴=-

友情博客

搜索

最新评论

RSS

我的 Blog:
walking 最新的 20 条日志
[工]
[忆]
[品]
[曲]
全站 Blog:
全站最新的 20 条日志

DWR, Direct Web Remoting, easy AJAX for JAVA

 

DWR is a light-weigh for implementing AJAX in JAVA. “DWR reduces development time and the likelihood of errors by providing commonly used functions and removing almost all of the repetitive code normally associated with highly interactive web sites.” (DWR Home)

 

After a while studying, a demo was out.

 

First, I copy all what I need to my tomcat directory. It looks like this:

[TOMCAT-ROOT]\webapps\demo\WEB-INF\lib\dwr.jar

 

Second, we have to configure the TOMCAT to access the dwr.jar as servlets.Modify the web.xml located  [TOMCAT-ROOT]\webapps\demo\WEB-INF\.It will look like below:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app id="dwr">

 

  <display-name>DWR (Direct Web Remoting)</display-name>

  <description>A demo of how to call Java on the server directly from Javascript on the client</description>

 

  <servlet>

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

    <display-name>DWR Servlet</display-name>

    <description>Direct Web Remoter Servlet</description>

    <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>

    <init-param>

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

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

    </init-param>

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

  </servlet>

 

  <servlet-mapping>

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

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

  </servlet-mapping>

 

</web-app>

 

 

Then, I create a dwr.xml at [TOMCAT-ROOT]\webapps\demo\WEB-INF\

 

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

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

 

<dwr>

  <allow>             

    <create creator="new" JavaScript="Parser">

      <param name="class" value="net.rick.dwr.bean.Parser"/>

    </create>

  </allow>

</dwr>

 

JavaScript="Parser" defines that we can get a .js file named Parser.js, which is the core file of our demo JAVA file.

 

Now, we can go on creating the JAVA file. Such as below code:

 

package net.rick.dwr.bean;

 

public class Parser{

            public Parser(){

            }

            public String parse(String str , String message){

                        return "Hello "+ str + "!\n" + message;

            }

}

 

This java file was putted in [TOMCAT-ROOT]\webapps\demo\WEB-INF\classes.

Compile it.

 

We have to create a html file named demo.html to implement the method defined in JAVA class.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

  <title>DWR - Demo Page</title>

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

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

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

 

</head>

 

<body >

Demo for Parser.java

<br>

Pls input your name here:

<input type="text" value="" id="username"/>

<input type="text" value="" id="message"/>

<input type="button" value="Parse" onclick='Parser.parse(sayHello,$("username").value,$("message").value)'/>

<script language="javascript">

var sayHello=function(str){

            DWRUtil.setValue("result",str);// the dwr function to display something in a

                                                   //filed which id properties is the first

                                                   //parameter, here is “result”,

                                                   //and the str is the string we want to display

}

</script>

<span id="result"></span>

 

</body>

</html>

 

Let me talk about these code :

 

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

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

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

 

<script type='text/javascript' src='/dwr/dwr/interface/Parser.js'></script>, You can change Parser.js as what you define in dwr.xml above. That means if you define 

    <create creator="new" JavaScript="ABC">

<param name="class" value="net.rick.dwr.bean.Parser"/>

</create>

 you can change <script type='text/javascript' src='/dwr/dwr/interface/Parser.js'></script> to <script type='text/javascript' src='/dwr/dwr/interface/ABC.js'></script>  .

 

<script type='text/javascript' src='/dwr/dwr/engine.js'></script> is required and <script type='text/javascript' src='/dwr/dwr/util.js'></script> is optional,but, I suggest include it.

 

The code

<input type="button" value="Parse" onclick='Parser.parse(sayHello,$("username").value,$("message").value)'/>

means that the browser call the Parser.parse function when click the button. The first parameter, sayHello is a js function we want to call when Parse.parse was called and return something. The second parameter is the first parameter of the JAVA function, and the third parameter is the second parameter of the JAVA function, etc.Here, somebody will ask “how can I do when I want to call a overloaded method in JAVA class?”, the answer is : NO WAY just forget the overloaded.DWR does not support overload method.

 

Now, everything is ready.

 

Restart TOMCAT.

 

Access http://[your-server-address]/demp/demo.html , What you see ? That is DWR.

 

 

 

 

 

作者:Rick 发表时间:2005-11-9  [所属栏目:] | [返回首页]
日志-1  每页显示-1 

评论(共 条) 我要评论
{CommentTime} | {CommentAuthor} {CommentUrl}
{CommentContent}