<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>We Heart Code &#187; ajax</title>
	<atom:link href="http://www.weheartcode.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Thu, 12 May 2011 23:17:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Using Protototype AJAX &amp; JSON with J2EE and Struts</title>
		<link>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/</link>
		<comments>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/#comments</comments>
		<pubDate>Wed, 16 May 2007 20:27:58 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[struts]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/</guid>
		<description><![CDATA[We don't always get to use the latest greatest web framework, but just because you're using a crusty old dinosaur J2EE XML framework from the pits of hell, doesn't mean you can't make girls cry with your AJAX/JSON skills. This tutorial will show how to use prototype to return JSON data to your jsp. Prerequisites [...]]]></description>
			<content:encoded><![CDATA[<p>We don't always get to use the latest greatest web framework, but just because you're using a crusty old dinosaur J2EE XML framework from the pits of hell, doesn't mean you can't make girls cry with your AJAX/JSON skills.</p>
<p>This tutorial will show how to use prototype to return JSON data to your jsp.<br />
<span id="more-18"></span></p>
<h3>Prerequisites</h3>
<p>This tutorial will be Struts-centric, but really is adaptable to any Java/J2EE platform. I'm not going to include any DB Layer specific code since that seems to change so much, but you should know how to take a query result and put it in a hash map.</p>
<h3>The Goal</h3>
<p>Create a Struts Action class that queries the database and returns the result in a JSON object back to a JSP with as little fuss as possible.</p>
<h3>The JSP</h3>
<p>In this example, I want to populate a text box in my form with data returned from an AJAX request in the background. For example, sometimes you want to pull out a person's name based on some internal ID. This saves our user from having to type it in by hand and also eliminates spelling mistakes.</p>
<p>So let's assume we have a text box where the user would type in the id as well as a text box where they would type the full name, the code would look something like this</p>
<div class="syntax_hilite">
<div id="html-5">
<div class="html"><span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"theId"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"theId"</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">&lt;input</span></a> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"fullName"</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"fullName"</span>/<span style="color: #000000; font-weight: bold;">&gt;</span></a></span></div>
</div>
</div>
<p></p>
<p>We'll then have a javascript function that uses <a href="http://www.prototypejs.org">prototype</a> to create an AJAX request that hits a struts action to to the database look up, then populates the fullName text box with the fullName property from the returned JSON object.</p>
<div class="syntax_hilite">
<div id="javascript-6">
<div class="javascript"><span style="color: #003366; font-weight: bold;">function</span> lookup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;html:rewrite page=&quot;/util/namelookup.do&quot;/&gt;'</span>, <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;parameters: <span style="color: #66cc66;">&#123;</span>theId: $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'theId'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span><span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; onSuccess: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>transport, json<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>json.<span style="color: #006600;">executeError</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>json.<span style="color: #006600;">executeError</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'fullName'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">value</span> = json.<span style="color: #006600;">fullName</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p></p>
<h3>The Java</h3>
<p>Our goal here is to do some work and put that work in a JSON object, for me the easiest way is to just query the DB and throw the results into a hashmap or java bean. To do the JSON lifting in this example I use the open source <a href="http://json-lib.sourceforge.net/">json-lib</a>. If an error occurs we'll put error text in a key in the JSON object called executeError so that we can display that in our javascript.</p>
<div class="syntax_hilite">
<div id="java-7">
<div class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameLookupAction <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?q=allinurl%3AAction+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Action</span></a><br />
<span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #66cc66;">&#40;</span>ActionMapping mapping, ActionForm form,<br />
&nbsp; &nbsp; HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a><br />
&nbsp; <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> theId = request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"theId"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Do your database work here to grab the name and populate our HashMap hm</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//In this example I'll just hard-code the name though.</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a> hm = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;hm.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"fullName"</span>,<span style="color: #ff0000;">"Joe Blow"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//each key from our hash map becomes a key in our JSON object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = JSONObject.<span style="color: #006600;">fromObject</span><span style="color: #66cc66;">&#40;</span>hm<span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//Plop it in the header so prototype can grab it.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"X-JSON"</span>, json.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a> hm = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; hm.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"executeError"</span>, <span style="color: #ff0000;">"Couldn't find the Full Name because an error occured."</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; JSONObject json = JSONObject.<span style="color: #006600;">fromObject</span><span style="color: #66cc66;">&#40;</span>hm<span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"X-JSON"</span>, json.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006600;">findForward</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"success"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>So that's all there is to it, as you can see it's pretty simple stuff but quite powerful. I like to create utility classes that return a whole bunch of info from a query, they're quite handy to have around to quickly display information in your web app without having to do a lot of leg work of new windows and new jsp pages, etc.</p>
<h3>Extra struts-config.xml Stuff</h3>
<p>If you're using struts you need to forward to a jsp of some sort, this is just going to be an empty JSP page, the JSON info is stored in the response header, anyway here's what your struts-config.xml entry might look like for this action.</p>
<div class="syntax_hilite">
<div id="xml-8">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/util/namelookup"</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">"request"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"com.yourcompany.NameLookupAction"</span> <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">"false"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"success"</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/WEB-INF/jsp/ajax/empty.jsp"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/action<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts&amp;u=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/&amp;title=Using+Protototype+AJAX+%26%23038%3B+JSON+with+J2EE+and+Struts" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks" alt="Add 'Using Protototype AJAX &#038; JSON with J2EE and Struts' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/05/16/using-protototype-ajax-json-with-j2ee-and-struts/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

