﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-VIRGIN FOREST OF JAVA-文章分类-ＡＪＡＸ</title><link>http://www.blogjava.net/RR00/category/19291.html</link><description>不要埋头苦干，要学习，学习，再学习。。。。。
&lt;br&gt;
powered  by &lt;font color='orange'&gt;R.Zeus&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 07:42:33 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 07:42:33 GMT</pubDate><ttl>60</ttl><item><title>ＡＪＡＸ　analyze from IBM develpers</title><link>http://www.blogjava.net/RR00/articles/94458.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Wed, 17 Jan 2007 08:52:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/94458.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/94458.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/94458.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/94458.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/94458.html</trackback:ping><description><![CDATA[
		<p>
				<a name="N100B8">
						<span class="atitle" twffan="done">Implementing the catalog</span>
				</a>
		</p>
		<p>The starting point of a DWR application is writing your server-side object model. In this case, I start by writing a DAO to provide search capabilities on the product catalog datastore. <code>CatalogDAO.java</code> is a simple, stateless class with a zero-argument constructor. Listing 1 shows the signatures of the Java methods that I want to expose to Ajax clients:</p>
		<br />
		<br />
		<a name="listing1">
				<b>Listing 1. The CatalogDAO methods to expose via DWR</b>
		</a>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="code-outline">
										<pre class="displaycode">/**
 * Returns a list of items in the catalog that have 
 *  names or descriptions matching the search expression
 * @param expression Text to search for in item names 
 *  and descriptions 
 * @return list of all matching items
 */
public List&lt;Item&gt; findItems(String expression);

/**
 * Returns the Item corresponding to a given Item ID
 * @param id The ID code of the item
 * @return the matching Item
 */
public Item getItem(String id);
</pre>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<p>Next, I need to configure DWR, telling it that Ajax clients should be able to construct a <code>CatalogDAO</code> and call these methods. I do this with the dwr.xml config file shown in Listing 2:</p>
		<br />
		<br />
		<a name="listing2">
				<b>Listing 2. Config to expose CatalogDAO methods</b>
		</a>
		<br />
		<table cellspacing="0" cellpadding="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="code-outline">
										<pre class="displaycode">&lt;!DOCTYPE dwr PUBLIC
  "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
  "http://www.getahead.ltd.uk/dwr/dwr10.dtd"&gt;
&lt;dwr&gt;
  &lt;allow&gt;
    &lt;create creator="new" javascript="catalog"&gt;
      &lt;param name="class" 
        value="developerworks.ajax.store.CatalogDAO"/&gt;
      &lt;include method="getItem"/&gt; 
      &lt;include method="findItems"/&gt; 
    &lt;/create&gt; 
    &lt;convert converter="bean" 
      match="developerworks.ajax.store.Item"&gt;
      &lt;param name="include" 
        value="id,name,description,formattedPrice"/&gt;
    &lt;/convert&gt;
  &lt;/allow&gt;
&lt;/dwr&gt;
</pre>
								</td>
						</tr>
				</tbody>
		</table>
		<br />
		<p>The root element of the dwr.xml document is <code>dwr</code>. Inside this element is the <code>allow</code> element, which specifies the classes that DWR will remote. The two child elements of <code>allow</code> are <code>create</code> and <code>convert</code>. </p>
		<p>
				<a name="N100F5">
						<span class="smalltitle" twffan="done">
								<strong>
										<font face="Arial">The create element</font>
								</strong>
						</span>
				</a>
		</p>
		<p>The <code>create</code> element tells DWR that a server-side class should be exposed to Ajax requests and defines how DWR should obtain an instance of that class to remote. The <code>creator</code> attribute here is set to the value <code>new</code>, meaning that DWR should call the class's default constructor to obtain an instance. Other possibilities are to create an instance through a fragment of script using the Bean Scripting Framework (BSF), or to obtain an instance via integration with the IOC container, Spring. By default, when an Ajax request to DWR invokes a <code>creator</code>, the instantiated object is placed in page scope and therefore is no longer available after the request completes. In the case of the stateless <code>CatalogDAO</code>, this is fine.</p>
		<p>The <code>javascript</code> attribute of <code>create</code> specifies the name by which the object will be accessible from JavaScript code. Nested within the <code>create</code> element, a <code>param</code> element specifies the Java class that the <code>creator</code> will create. Finally, <code>include</code> elements specify the names of the methods that should be exposed. Explicitly stating the methods to expose is good practice to avoid accidentally allowing access to potentially harmful functionality -- if this element is omitted, all of the class's methods will be exposed to remote calls. Alternately, you can use <code>exclude</code> elements to specify only those methods you wish to prevent access to.</p>
		<p>
				<a name="N10131">
						<span class="smalltitle" twffan="done">
								<strong>
										<font face="Arial">The convert element</font>
								</strong>
						</span>
				</a>
		</p>
		<p>While <code>creator</code>s are concerned with exposing classes and their methods for Web remoting, <code>convertor</code>s are concerned with the parameters and return types of those methods. The role of the <code>convert</code> element is to tell DWR how to convert datatypes between their server-side Java object representation and serialized JavaScript representation, and vice versa. </p>
		<p>DWR automatically mediates simple data types between Java and JavaScript representations. These types include Java primitives, along with their respective class representations, as well as Strings and Dates, arrays, and collection types. DWR can also convert JavaBeans into JavaScript representations, but for security reasons, doing so requires explicit configuration.</p>
		<p>The <code>convert</code> element in <a href="http://www-128.ibm.com/developerworks/java/library/j-ajax3/#listing2"><font color="#996699">Listing 2</font></a> tells DWR to use its reflection-based bean convertor for the <code>Item</code>s returned by the exposed methods of <code>CatalogDAO</code> and specifies which of the <code>Item</code>'s members should be included in the serialization. The members are specified using the JavaBean naming convention, so DWR will call the corresponding <code>get</code> methods. In this case, I'm omitting the numerical <code>price</code> field and instead including the <code>formattedPrice</code> field, which is currency-formatted ready for display.</p>
		<p>At this point, I'm ready to deploy my dwr.xml to my Web application's <code>WEB-INF</code> directory, where the DWR servlet will pick it up. Before proceeding, however, it's a good idea to ensure that everything is working as expected.</p>
<img src ="http://www.blogjava.net/RR00/aggbug/94458.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2007-01-17 16:52 <a href="http://www.blogjava.net/RR00/articles/94458.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>