1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
4
<title>Simple Text Generation Demo</title>
5
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
6
<script type='text/javascript' src='../tabs/tabs.js'> </script>
7
<script type='text/javascript' src='../dwr/engine.js'> </script>
8
<script type='text/javascript' src='../dwr/util.js'> </script>
9
<script type='text/javascript' src='../dwr/interface/Demo.js'> </script>
10
<script type="text/javascript" src='index.js'> </script>
11
<link rel="stylesheet" type="text/css" href="../tabs/tabs.css" />
12
<link rel="stylesheet" type="text/css" href="../generic.css" />
14
<body onload="dwr.util.useLoadingMessage();Tabs.init('tabList', 'tabContents');">
15
<div id="page-title">[
16
<a href="http://getahead.org/dwr/">DWR Website</a> |
17
<a href="..">Web Application Index</a>
20
<h1>Dynamically Updating Text</h1>
22
<p>This is a simple demonstration of how to dynamically update a web-page with
23
text fetched from a web server.</p>
26
<li><a href="#" tabId="demoDiv">Demo</a></li>
27
<li><a href="#" tabId="explainDiv">How it works</a></li>
28
<li><a href="#" tabId="sourceDiv">Source</a></li>
31
<div id="tabContents">
37
<input type="text" id="demoName" value="Joe"/>
38
<input value="Send" type="button" onclick="update()"/>
40
Reply: <span id="demoReply" style="background:#eeffdd; padding-left:4px; padding-right:4px;"></span>
46
<p>When you click on the "Send" button the browser calls the onclick event,
47
which calls the <code>update()</code> function:</p>
51
var name = dwr.util.getValue("demoName");
52
Demo.sayHello(name, loadinfo);
56
<p><code>dwr.util.getValue()</code> is a utility to get the value of any
57
element, in this case an input field, but it could be a div or a select
60
<p>DWR is asynchronous due to the way Javascript works so it won't halt the web
61
browser while we are waiting for the background HTTP request to return.
62
So the parameter <code>loadinfo</code> names a function to be called when the
63
call has returned.</p>
65
<p>On the server, DWR calls the <code>Demo.sayHello()</code> Java method:</p>
68
public String sayHello(String name) {
69
return "Hello, " + name;
73
<p>When this method returns, DWR calls <code>loadinfo()</code> function which
74
moves the text to the reply span:</p>
77
function loadinfo(data) {
78
dwr.util.setValue("demoReply", data);
82
<p><code>dwr.util.setValue()</code> is a utility that takes the data you pass in
83
the second parameter and works out how to fit it to go into the HTML element
84
specified by id in the first parameter. This function is one of several neat
85
Javascript utilities that make working with DWR much easier.</p>
87
<p>We could simplify things by writing the 2 Javascript functions together like
92
var name = dwr.util.getValue("demoName");
93
Demo.sayHello(name, function(data) {
94
dwr.util.setValue("demoReply", data);
99
<p>And that's it. In effect we have written much less than 10 lines of code to
100
get data from the server, and display it in the browser.</p>
106
<h2>HTML source:</h2>
110
<input type="text" id="demoName"/>
111
<input value="Send" type="button" onclick="update()"/>
113
Reply: <span id="demoReply"></span>
117
<h2>Javascript source:</h2>
120
var name = dwr.util.getValue("demoName");
121
Demo.sayHello(name, function(data) {
122
dwr.util.setValue("demoReply", data);
127
<h2>Java source:</h2>
129
package org.getahead.dwrdemo.simpletext;
132
public String sayHello(String name) {
133
return "Hello, " + name;
140
<?xml version="1.0" encoding="UTF-8"?>
141
<!DOCTYPE dwr PUBLIC
142
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
143
"http://getahead.org/dwr/dwr20.dtd">
147
<create creator="new" javascript="Demo">
148
<param name="class" value="org.getahead.dwrdemo.simpletext.Demo"/>