<?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>duckranger.com</title>
	<atom:link href="http://duckranger.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://duckranger.com</link>
	<description>JUST DOING IT</description>
	<lastBuildDate>Wed, 18 Jan 2012 07:42:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Fancybox problems? Make sure you validate!</title>
		<link>http://duckranger.com/2012/01/fancybox-problems-make-sure-you-validate/</link>
		<comments>http://duckranger.com/2012/01/fancybox-problems-make-sure-you-validate/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 07:42:35 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Be nice to your users]]></category>
		<category><![CDATA[javaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=862</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/66576488@N00/4693634492/" title="Not-So-Easy Like Friday Morning" target="_blank"><img src="http://farm5.static.flickr.com/4016/4693634492_42df0ff855_m.jpg" alt="Not-So-Easy Like Friday Morning" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/66576488@N00/4693634492/" title="Or Hiltch" target="_blank">Or Hiltch</a></div>
</p><p>This took a whole day for me. Hopefully I can save you the pain. I added quite a few possible search terms here for maximum coverage, so the text may get a bit repetitive. I mean &#8211; when I tried to look for a solution, I didn&#8217;t even know what to look for &#8211; so I tried googling for every symptom I saw, to no avail.The deal was this: my page has a very large form, &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/66576488@N00/4693634492/" title="Not-So-Easy Like Friday Morning" target="_blank"><img src="http://farm5.static.flickr.com/4016/4693634492_42df0ff855_m.jpg" alt="Not-So-Easy Like Friday Morning" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/66576488@N00/4693634492/" title="Or Hiltch" target="_blank">Or Hiltch</a></div>
<p>This took a whole day for me. Hopefully I can save you the pain. I added quite a few possible search terms here for maximum coverage, so the text may get a bit repetitive. I mean &#8211; when I tried to look for a solution, I didn&#8217;t even know what to look for &#8211; so I tried googling for every symptom I saw, to no avail.<br/>The deal was this: my page has a very large form, which is logically separated to 3 parts.<br/>One part is mandatory, and the user must fill in all the fields on it. However, the other two parts are completely optional, and it is quite possible that some users won&#8217;t even bother with one or both of them (See the uber useful diagram below)<br />
<span id="more-862"></span><br />
<div id="attachment_863" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2012/01/form.png"><img src="http://duckranger.com/wp-content/uploads/2012/01/form-300x171.png" alt="" title="My form" width="300" height="171" class="size-medium wp-image-863" /></a><p class="wp-caption-text">Form with mandatory and optional parts</p></div></p>
<p>Normally, you&#8217;ll split this form into a wizard type of thing, but again &#8211; some users may not use the optional parts at all, and anyway &#8211; we don&#8217;t do wizards.</p>
<p>Because of the form&#8217;s size, I didn&#8217;t want the optional parts to appear alongside the main part on the page. This would have made the page huge. So, I came up with the brilliant idea to have them hidden (wrapped with a &lt;div&gt; element with style=&#8221;display:none&#8221;) and display them with <a href="http://fancybox.net/">fancybox</a>. (See the next beautifully decorated diagram!)<br />
<div id="attachment_869" class="wp-caption aligncenter" style="width: 253px"><a href="http://duckranger.com/wp-content/uploads/2012/01/form1.png"><img src="http://duckranger.com/wp-content/uploads/2012/01/form1-243x300.png" alt="" title="Form with hidden div" width="243" height="300" class="size-medium wp-image-869" /></a><p class="wp-caption-text">Form with hidden div</p></div></p>
<p>So the idea is pretty simple. The user interacts with the mandatory part, and if they need one (or both) of the optional parts, they can click the links that read &#8216;Open optional part&#8230;&#8217;. These links have inline hrefs to the optional form parts which are inside the hidden div. (Each of these optional parts is contained in a div of its own, to make it easier for fancybox).</p>
<p>In document.ready() &#8211; I fancybox both these links. Which means that when they are clicked, their reference will open inside fancybox. This actually worked quite well and I was very happy.<br />
Until I tried to save the data, and it wasn&#8217;t there. At least not all of it. </p>
<h3>First issue: Form fields missing when submitted</h3>
<p>Since I knew I wasn&#8217;t getting all of the form fields in my controller, I wanted to see what the browser was sending back on submit. Using firebug&#8217;s net tab, I got a little surprise.<br />
if the user clicked on one of the links to display an optional part, the entire set of fields contained in the optional part was missing from the POST request.<br />
The mandatory fields were always sent. But with regards to the optional parts &#8211; only the fields that were a part of an optional part that was <b>not</b> displayed (using fancybox) were sent back on submit.</p>
<p>Now, this is an outrageous behaviour. Theoretically, only disabled fields should be omitted from a submitted form, but none of my fields was being disabled.</p>
<p>Just to make sure, I changed the visibility of the containing &lt;div&gt; element, so that my optional parts showed all the time, and tried to submit the form. Everything was fine. But when I clicked on one of the links to display them (with fancybox) &#8211; I got the same behaviour. </p>
<p>Using jquery&#8217;s <a href="http://api.jquery.com/serializeArray/">serailizeArray()</a> showed that the fields were all there on page load, but disappeared right after the optional parts were fancybox&#8217;d.</p>
<h3>Second issue: Fancybox ignores custom configuration</h3>
<p>Alongside the issue of the missing fields, I wanted to manually control the size of fancybox&#8217;s overlay for one of the optional parts. This is normally quite easy:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a#openOptionalOne&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fancybox</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">'width'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">900</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">'height'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">800</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">'autoScale'</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #3366CC;">'autoDimensions'</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>The only thing you have to remember is that if you want to manually set height and width, you have to give <b>both</b> height and width. Also, you have to set both autoScale and autoDimensions to false.<br />
However, fancybox didn&#8217;t care too much about my directives and refused to change its default rendering. I should have taken this as a warning, but since my form fields were disappearing on submit &#8211; I decided I have more urgent things to solve.</p>
<p>A middle-of-the-road moral here:</p>
<blockquote><p>When Fancybox doesn&#8217;t do what you know it should do &#8211; there&#8217;s something wrong. Fix this first.</p></blockquote>
<p>I was on a role with my form-issues, and disregarded this warning. Oh well.<br />
Now, at this point, I could have just decided to go for a workaround: Use a wizard, make each of the optional parts a form unto itself, or just decide to display the entire form on one page (there are other visual tricks to use, but this is in the next post).</p>
<p>But no. I was on a mission to find out why fancybox removes my fields away. Seriously. Workarounds are good, but if I have the time &#8211; I&#8217;d rather get down to the bottom of it.</p>
<h3>Could fancybox have changed my DOM?</h3>
<p>A friend suggested that fancybox may have changed the DOM somehow. So I console.log&#8217;d document.body.innerHTML before fancybox and after fancybox (without changing any of the optional form fields).<br />
Copied them into a couple of variables and compared:</p>
<div class="codecolorer-container python mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># -*- coding: iso-8859-15 -*-</span><br />
orig<span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'''<br />
&lt;div...............................<br />
'''</span><br />
<br />
postfancy <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'''<br />
&lt;div..............................<br />
'''</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> x<span style="color: #66cc66;">,</span> y <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: #66cc66;">,</span> orig<span style="color: #66cc66;">,</span> postfancy<span style="color: black;">&#41;</span>:<br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x<span style="color: #66cc66;">!=</span>y<span style="color: black;">&#41;</span>:<br />
&nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'ahoy there!'</span><br />
&nbsp; <span style="color: #ff7700;font-weight:bold;">break</span></div></div>
<p>Both were the same. So no visible mucking around (still, there could be issues with the DOM being manipulated, but it&#8217;s not that simple).</p>
<h3>Third issue: Fancybox ignores custom configuration (I know&#8230; again)</h3>
<p>To try and see what&#8217;s going on, I thought I&#8217;d hook on to fancybox&#8217;s onCleanup event. The onCleanup event gets called just before fancybox closes. I wanted to use it to display my form&#8217;s content- but as happened before, fancybox ignored me completely.<br />
Now, second time you get the same warning &#8211; this is time to pay some serious attention. </p>
<p>First thing to try &#8211; ensure I&#8217;m on the latest version of fancybox (note &#8211; I know that there are no compatibility issues, because fancybox works perfectly on another page, where the entire form is contained within it).</p>
<p>Indeed, I had v1.3.1 and 1.3.4 was out. (fancybox v2 is great, but the license has changed so I can&#8217;t use it).</p>
<p>But upgrading did not change behaviour, so I downgraded back (this is important, I think. While it is usually good to upgrade, don&#8217;t blindly do it if it didn&#8217;t solve your issue. If a library upgrade did not affect the defect at hand &#8211; might as well reverse it and try solving the issue first &#8211; then go with the upgrade. Any upgrade can introduce new bugs that may obscure your initial problem).</p>
<p>Just to make sure I wasn&#8217;t losing my mind, I changed the default configuration options inside fancybox&#8217;s javascript (you can do it too &#8211; they are right at the bottom of the file).<br />
This worked &#8211; I could hook onto the onComplete event or change the width and height. This meant to me that the issues I was having of fancybox ignoring my configuration or my form fields disappearing weren&#8217;t likely to stem from fancybox, but rather from my code. </p>
<h3>When all else fails &#8211; RTFM</h3>
<p>So, it&#8217;s off to the <a href="http://fancybox.net/howto">how-to page on fancybox&#8217;s website</a>.<br />
Now, here&#8217;s where I failed&#8230;</p>
<div id="attachment_879" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2012/01/howto.png"><img src="http://duckranger.com/wp-content/uploads/2012/01/howto-300x155.png" alt="" title="Valid doctype" width="300" height="155" class="size-medium wp-image-879" /></a><p class="wp-caption-text">Fancybox requires a valid doctype</p></div>
<p>See, the problem is that while my document indeed had a valid DOCTYPE, it did not <a href="http://validator.w3.org/">validate</a> accordingly.<br />
Sifting through the generated code, I found that the end &lt;/form&gt; tag was badly placed. In essence I had the following structure:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">..<br />
<span style="color: #009900;">&lt;form...&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;input....&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;input...&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span>...&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
..</div></div>
<p>See the problem? my form closed before a contained div element closed. While this works well (and has been for at least a year&#8217;s worth of browsers upgrade), when Fancybox was thrown into the mix it simply refused to accept this sorry state of affairs.</p>
<h3>Fix your pages&#8230;</h3>
<p>To confirm my suspicion and mock my HTML abilities, the issue was gone once the page validated completely, and I am now the proud owner of a form that has hidden optional parts that appear inside a fancybox wrapper when you click on links. Hooray!!</p>
<div id="attachment_869" class="wp-caption aligncenter" style="width: 253px"><a href="http://duckranger.com/wp-content/uploads/2012/01/form1.png"><img src="http://duckranger.com/wp-content/uploads/2012/01/form1-243x300.png" alt="" title="Form with hidden div" width="243" height="300" class="size-medium wp-image-869" /></a><p class="wp-caption-text">Form with hidden div</p></div>
<img src="http://duckranger.com/?ak_action=api_record_view&id=862&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2012/01/fancybox-problems-make-sure-you-validate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Double Submit Prevention, Disabled buttons, Firefox and the Back Button</title>
		<link>http://duckranger.com/2012/01/double-submit-prevention-disabled-buttons-firefox-and-the-back-button/</link>
		<comments>http://duckranger.com/2012/01/double-submit-prevention-disabled-buttons-firefox-and-the-back-button/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 01:22:11 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Be nice to your users]]></category>
		<category><![CDATA[javaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[back button]]></category>
		<category><![CDATA[disabled]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=836</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/99843959@N00/1361277704/" title="Ainara portafotos" target="_blank"><img src="http://farm2.static.flickr.com/1394/1361277704_4e3f622421_m.jpg" alt="Ainara portafotos" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/99843959@N00/1361277704/" title="Mr. Theklan" target="_blank">Mr. Theklan</a></div>
</p><p>Double submit is when a user submits a form on your web page twice (well, technically they can do it more than twice, but you can&#8217;t call it a double submit&#8230;)<br />
It usually happens when your user double-clicks your Submit button instead of clicking it, but may also happen in two other popular scenarios: your buttons do not provide the user with a visual feedback of being clicked (e.g. when you use an image as your &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/99843959@N00/1361277704/" title="Ainara portafotos" target="_blank"><img src="http://farm2.static.flickr.com/1394/1361277704_4e3f622421_m.jpg" alt="Ainara portafotos" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/99843959@N00/1361277704/" title="Mr. Theklan" target="_blank">Mr. Theklan</a></div>
<p>Double submit is when a user submits a form on your web page twice (well, technically they can do it more than twice, but you can&#8217;t call it a double submit&#8230;)<br />
It usually happens when your user double-clicks your Submit button instead of clicking it, but may also happen in two other popular scenarios: your buttons do not provide the user with a visual feedback of being clicked (e.g. when you use an image as your submit button), or when your application takes too long to process the submitted form, and the user impatiently submits it again.<br />
<span id="more-836"></span><br />
Looking around the web you can find quite a lot of discussions around double submission handling and prevention. </p>
<p>One of the most popular ways of preventing double-submit is to disable the submit button in its onclick event, and then submit the form:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;no&quot;</span> &nbsp;</span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.disabled=true;document.myform.submit();&quot;</span>&gt;</span>Submit<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span></div></div>
<p>This works for most cases, and because the button is disabled, your user can&#8217;t click it a second time. </p>
<h3>The back button issue</h3>
<p>Let&#8217;s pretend that after the user submitted your form, they are redirected to a new page, that displays some information related to the submitted form. The user reviews the information and decides to go back to the form and submit it again. Your user clicks the browser&#8217;s back button, and resubmits the form with new data. Good?</p>
<p>Yes, unless your user is on Firefox.</p>
<p>In Firefox, the disabled button will remain disabled after you go back, and your user won&#8217;t be able to click it. They&#8217;ll have to refresh the page in order to do so, but then they&#8217;ll lose everything they&#8217;ve already entered in the form, and that&#8217;s lame.</p>
<p>The reason this happens, and it is important to understand that, is that Firefox just reloads the page from its cache, and in the cache, the button is in a disabled state.</p>
<p>See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=439810">this</a> or <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=443289">this</a> for the open bugs on Firefox&#8217;s Jira (there are others, even one when the developers say this is the intended behavior)</p>
<h3>What can I do on Firefox?</h3>
<p>There are a few suggestions out there:</p>
<ol>
<li>Tell the browser to not cache your page using <a href="http://stackoverflow.com/questions/1341089/using-meta-tags-to-turn-off-caching-in-all-browsers">META cache directives</a>.<br/>While this works &#8211; it is a huge overkill (especially where the other, innocent browsers, are concerned&#8230;)</li>
<p><br/></p>
<li>Add <b>autocomplete=&#8221;off&#8221;</b> to your form element.<br/>While this works too, it has the added problem of clearing out the values of fields in your form, which is not very nice to your users.</li>
<p><br/></p>
<li>Use Jquery&#8217;s ready() function to enable the button.<br/>This &#8220;should work&#8221;. But remember &#8211; Firefox loads the page from the cache. The ready() event will not be fired (try it with the following code on Firefox and chrome to see the difference. Note that you&#8217;ll also need to create success.php or a similar file for the action url)
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Duck vs Firefox round 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$(document).ready(function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('ready');<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});<br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Round 1!!!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myform&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;success.php&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textInput&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nono&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.disabled=true;document.myform.submit();&quot;</span>&gt;</span>Submit<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></div>
</li>
<li>Use the body&#8217;s tag onload=&#8221;" attribute.<br/>This doesn&#8217;t work for the same reason as the previous issue &#8211; the load event is never fired when the page is read from Firefox&#8217;s cache.</li>
</ol>
<h3>Solution, part I</h3>
<p>So, the problem is that Firefox loads the page from its cache, and doesn&#8217;t process it the way a normal page is processed, and therefore &#8211; the onload/ready events are not raised.<br />
There is, however, one event that does get raised: this is the <a href="https://developer.mozilla.org/en/Using_Firefox_1.5_caching#pageshow_event">pageshow</a> event.</p>
<p>The <b>pageshow</b> event is raised by Firefox whenever a page is shown, <b>regardless of whether it is loaded from the cache or not.</b><br />
You could use it like this:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">..<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> onpageshow<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;document.getElementById('nono').disabled=false&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myform&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;success.php&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textInput&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nono&quot;</span> &nbsp;<span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.disabled=true;document.myform.submit();&quot;</span>&gt;</span>Submit<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
..</div></div>
<p>You don&#8217;t have to add a selector to Firefox, because other browsers (Except Safari which uses it in the same way) will just ignore it, as they never raise the event.</p>
<p>If you want to make sure that the code only runs when it is indeed a page loaded from the cache (and not when the user actually accessed your server), then you could add the following condition:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> onpageshow<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (event.persisted) {document.getElementById('nono').disabled=false}&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myform&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;success.php&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;textInput&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nono&quot;</span> &nbsp;<span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.disabled=true;document.myform.submit();&quot;</span>&gt;</span>Submit<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">button</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></div></div>
<p><b>event.persisted</b> will only be true when the page was loaded from persistent storage (the browser&#8217;s cache).</b></p>
<h3>But I am using a page template!</h3>
<p>In most frameworks, you use some sort of a page template. This has two implications on using <b>onpageshow</b>:</p>
<ol>
<li>Adding onpageshow to your body tag will add it to all pages, and most of them will not have a button with the relevant id.</li>
<li>You may not know in advance what are the ids of the disabled elements you wish to re-enable.</li>
</ol>
<p>There is a possible solution to this problem as well:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> disableMe<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; el.<span style="color: #660066;">markDisabled</span> <span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">true</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//This is an Internet Explorer issue. addEventListener wasn't supported before IE9, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//and it breaks your page on IE8 and below...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pageshow'</span><span style="color: #339933;">,</span> pageShowHandler<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> pageShowHandler<span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">persisted</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> arrElements <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Assume we only care about re-enabling buttons</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>arrElements.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> btn <span style="color: #339933;">=</span> arrElements<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>btn.<span style="color: #660066;">markDisabled</span> <span style="color: #339933;">&amp;&amp;</span> btn.<span style="color: #660066;">markDisabled</span> <span style="color: #339933;">==</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btn.<span style="color: #660066;">markDisabled</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btn.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span style="color: #660066;">removeEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pageshow'</span><span style="color: #339933;">,</span>pageShowHandler<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In here, I defined two javascript functions. The first one, disableMe() is used to disable an element like so:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">..<br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">button</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;disableMe(this);document.myform.submit();&quot;</span>&gt;</span><br />
..</div></div>
<p>The disableMe() function sets disabled=true on the element, and also sets &#8216;markDisabled&#8217; to true. Note that markDisabled isn&#8217;t a real attribute name, and we just add it to the element as a marker. The reason is that you might not want to willy-nilly enable all of the disabled button elements when the user clicks the back-button, as some of them may be disabled because of other reasons (e.g. because the user hasn&#8217;t checked a specific checkbox).</p>
<p>The last thing disableMe() does is register the pageShowHandler function as an event listener for the pageshow event. This way, the event is only handeled when it is actually needed. Neat, eh? only pages that require pageshow handling because we manually disabled a button, actually receive it. (If you wanted to, you could test for Firefox here, and not do anything if the user&#8217;s browser is different).</p>
<p>Now, pageShowHandler is called by Firefox when the pageshow event is fired. Note that you have to define event as a parameter to the function, otherwise you won&#8217;t have it.<br />
If the handler is called when the page is loaded from cache, pageShowHandler will locate all button elements on the page.<br />
This is where the marker attribute comes into play. Note that the function tests for its existence and value &#8211; so it only enables buttons that were disabled by the disableMe() function.</p>
<p>The last thing pageShowHandler does is to remove itself from the page. It will get re-attached by disableMe() if needs be.</p>
<p>That&#8217;s it! </p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=836&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2012/01/double-submit-prevention-disabled-buttons-firefox-and-the-back-button/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically select a database with CakePHP 1.3</title>
		<link>http://duckranger.com/2011/12/dynamically-select-a-database-with-cakephp-1-3/</link>
		<comments>http://duckranger.com/2011/12/dynamically-select-a-database-with-cakephp-1-3/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 02:24:04 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=816</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/8340348@N07/3164026640/" title="phew,that bread gave me wind?" target="_blank"><img src="http://farm4.static.flickr.com/3100/3164026640_332010fe5c_m.jpg" alt="phew,that bread gave me wind?" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" title="Attribution-NonCommercial-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/8340348@N07/3164026640/" title="johnb/Derbys/uk" target="_blank">johnb/Derbys/uk</a></div>
</p><p>In one of my projects, I have several customers all served from the same CakePHP application. This is by no means unique, and is a very well understood pattern of maintaining co-exiting websites. In my set up, each of the customers has their own website, with their own data and design, but there is only one copy of the actual application behind all those websites. This is, of course, a fundamental attribute of selling SaaS.</p>
<p>From a &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/8340348@N07/3164026640/" title="phew,that bread gave me wind?" target="_blank"><img src="http://farm4.static.flickr.com/3100/3164026640_332010fe5c_m.jpg" alt="phew,that bread gave me wind?" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" title="Attribution-NonCommercial-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/8340348@N07/3164026640/" title="johnb/Derbys/uk" target="_blank">johnb/Derbys/uk</a></div>
<p>In one of my projects, I have several customers all served from the same CakePHP application. This is by no means unique, and is a very well understood pattern of maintaining co-exiting websites. In my set up, each of the customers has their own website, with their own data and design, but there is only one copy of the actual application behind all those websites. This is, of course, a fundamental attribute of selling SaaS.</p>
<p>From a database point of view, there are three options of managing such a set up:</p>
<ol>
<li>Add a customer identifier column to all your tables</li>
<li>Store a separate set of tables for each customer (normally using a table prefix)</li>
<li>Use a separate database for each customer</li>
</ol>
<p>Each of these options require that your application can identify the relevant customer (not necessarily the user!)  on each request. This is very naturally achieved by using the domain name in the request.<br />
So say we have the following layout, where all the following domains map to my application:</p>
<ul>
<li>mygreatcustomer.com.au</li>
<li>anothergoodone.co.nz</li>
<li>someoneelse.org.ag</li>
</ul>
<p>In PHP, I can easily identify what domain was requested, using $_SERVER['SERVER_NAME']<br />
When I can identify the domain (that is &#8211; the relevant customer) it is very easy to use option (1) above &#8211; just use the domain as the customer identifier column.<br />
But what if you don&#8217;t want to use this option? What if you want to use option (2)?</p>
<p>CakePHP v1.2 let you do that. You could set the table prefix on the fly (in database.php):</p>
<pre class="brush: php; title: ; notranslate">
//in database.php
var $default = array(
 'driver' =&gt; 'mysql',
 'persistent' =&gt; false,
 'host' =&gt; 'kiwihost',
 'login' =&gt; 'mate',
 'password' =&gt; 'secure_as',
 'database' =&gt; 'mydatabase',
 'prefix' =&gt; '',
);

function __construct() {
   $host = $_SERVER['SERVER_NAME'];
   $host = str_replace(&quot;www.&quot;, &quot;&quot;, $host);   //remove www part
   $host = str_replace(&quot;.&quot;, &quot;_&quot;, $host);     //a period is illegal in mysql table names
   $this-&gt;default[&quot;prefix&quot;] = $host.&quot;_&quot;;     //so www.anothergoodone.co.nz translates into the prefix anothergoodone_co_nz_
}
</pre>
<p>But &#8211; the ability to do this dynamically has been removed in Cake 1.3, as table prefixes are now stored in the temporary model files. To fix this, you&#8217;d need to refresh those files on each new request, which makes the whole point of using those files moot. If you used option (1) to begin with &#8211; you&#8217;re in luck. But, if not, you might want to consider moving to option (3) &#8211; separate databases for separate customers.</p>
<p>From a maintenance point of view, there are many reasons why this isn&#8217;t necessarily the best choice, and I had a long discussion with <a href="http://scalebase.com" target="_blank">Scalebase</a>&#8216;s Liran Zelkha about it &#8211; a discussion that I&#8217;ll post here at a later stage.<br />
Anyway, say you decided you want to split your SQL data along the customer boundary using this method &#8211; let&#8217;s see how you do it in Cake.</p>
<p>First, you&#8217;ll want to split your data up into multiple databases. I suggest you name your databases to the domain name, replacing &#8216;.&#8217; with &#8216;_&#8217;. This way &#8211; the change in the __construct() functionis very elegant.</p>
<p>Second, take the __construct() function shown above, and make this simple change:</p>
<pre class="brush: php; title: ; notranslate">
function __construct() {
   $host = $_SERVER['SERVER_NAME'];
   $host = str_replace(&quot;www.&quot;, &quot;&quot;, $host);  
   $host = str_replace(&quot;.&quot;, &quot;_&quot;, $host);    
   $this-&gt;default[&quot;database&quot;] = $host.&quot;_&quot;;  
}
</pre>
<p>That&#8217;s it, really. Your application now selects the database on the fly, based on the domain part of the HTTP request coming in.</p>
<h2>But what if I don&#8217;t have an HTTP request?</h2>
<p>Sometimes, you don&#8217;t have an HTTP request. In my scenario, this happens with cron jobs.<br />
When you have a large number of customers, each with their own domain, you don&#8217;t always want to duplicate cron jobs just because they need to use a separate database.<br />
Using table prefix &#8211; this was easy. Your cron job (well, a shell in Cake) just needed to cycle through the prefixes one by one. However, this is not as easy when you have<br />
to switch to a different database with every customer, as you don&#8217;t have a request (so no $__SERVER) and database.php isn&#8217;t exposed.</p>
<p>In the old world of Cake 1.2, you might have stored a table of sites in your database (the only one with no prefix!) and used it like so:</p>
<pre class="brush: php; title: ; notranslate">
//This is inside a cake shell
function main() {
    $sites = $this-&gt;Site-&gt;find('all',array('conditions' =&gt; array('Site.active' =&gt; '1') ));
    foreach ($sites as $site) {
        $table_prefix = $site[&quot;Site&quot;][&quot;table_prefix&quot;];
        $this-&gt;setTablePrefix($table_prefix);
        //In the loop body - perform the cron-run for each of the customer websites
    }
}

function setTablePrefix($prefix) {
    //set the prefix on each of the models you use
    $this-&gt;Country-&gt;tablePrefix = $prefix;
    $this-&gt;City-&gt;tablePrefix = $prefix;
    //etc
}
</pre>
<p>However &#8211; this doesn&#8217;t work any more. In Cake 1.3 we are not using the table prefixes but rather a different database, so we have to use a different method:</p>
<pre class="brush: php; title: ; notranslate">
//This is inside a cake shell
function main() {
    $sites = //whichever way you want to store your sites
    foreach ($sites as $site) {
        $newDbConfig = $this-&gt;dbConnect(array('database'=&gt;$site,'datasource'=&gt;'default'));
        $this-&gt;setConnection($newDbConfig);
        //In the loop body - perform the cron-run for each of the customer websites
    }
}

function setConnection($newDbConfig) {
    //set the DB config on each of the models you use
    $this-&gt;Country-&gt;useDbConfig = $newConfig['name'];
    $this-&gt;Country-&gt;cacheQueries = false;
    $this-&gt;City-&gt;useDbConfig = $newConfig['name'];
    $this-&gt;City-&gt;cacheQueries = false;
    //etc
}
</pre>
<img src="http://duckranger.com/?ak_action=api_record_view&id=816&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/12/dynamically-select-a-database-with-cakephp-1-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google appengine and maps tutorial with Java &#8211; Part III</title>
		<link>http://duckranger.com/2011/07/google-appengine-and-maps-tutorial-with-java-part-iii/</link>
		<comments>http://duckranger.com/2011/07/google-appengine-and-maps-tutorial-with-java-part-iii/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 03:15:03 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[appengine]]></category>
		<category><![CDATA[Googlemaps]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[javaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=776</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/14676902@N02/2372045748/" title="Cane en vol" target="_blank"><img src="http://farm4.static.flickr.com/3033/2372045748_68746a31ff_m.jpg" alt="Cane en vol" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" title="Attribution-NonCommercial-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/14676902@N02/2372045748/" title="Free114" target="_blank">Free114</a></div>
</p><p>If you followed part 2 of this tutorial, you have an appengine application that displays a Google map in your browser, and lets you click the map to add markers to it.<br />
While extremely useful on long, cold Winter nights, you probably want more from your application. For example, you&#8217;d probably like to be able to save those markers somewhere, so you can share them with your users.<br />
This part of the tutorial will focus on this &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/14676902@N02/2372045748/" title="Cane en vol" target="_blank"><img src="http://farm4.static.flickr.com/3033/2372045748_68746a31ff_m.jpg" alt="Cane en vol" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" title="Attribution-NonCommercial-ShareAlike License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/14676902@N02/2372045748/" title="Free114" target="_blank">Free114</a></div>
<p>If you followed part 2 of this tutorial, you have an appengine application that displays a Google map in your browser, and lets you click the map to add markers to it.<br />
While extremely useful on long, cold Winter nights, you probably want more from your application. For example, you&#8217;d probably like to be able to save those markers somewhere, so you can share them with your users.<br />
This part of the tutorial will focus on this task. When you finish here, you&#8217;ll be able to add markers to your map, give your marker a title and a description, and save it to the appengine&#8217;s datastore.<br />
<span id="more-776"></span><br />
This is what your application&#8217;s main page is going to look like:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/target.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/target-300x136.png" alt="" title="target" width="300" height="136" class="aligncenter size-medium wp-image-777" /></a></p>
<p>You&#8217;ll notice a few changes from the page you developed in part II. Mainly &#8211; the map is now half the size of the screen, and there&#8217;s something that looks suspiciously like a data-entry form on the right side.<br />
You are absolutely correct. The map on the left half of the page is the same map &#8211; only smaller in size, and I&#8217;ve added a form on the right to capture information about the markers you place on it.</p>
<p>First thing to do is to change the jsp file (DuckMain.jsp) to resemble the changes in the screenshot:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;%@ page contentType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;java&quot;</span>%&gt;</span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Mapping ducks<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;css/main.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://maps.google.com/maps/api/js?sensor=false&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var map;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var initialLocation = new google.maps.LatLng(-44.6895642,169.1320571); <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function init() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var duckOptions = { <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom: 12,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center: initialLocation,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapTypeId: google.maps.MapTypeId.HYBRID<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map = new google.maps.Map(document.getElementById(&quot;map_canvas&quot;), duckOptions); <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var marker = new google.maps.Marker({ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: initialLocation,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map: map<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; google.maps.event.addListener(map, 'click', function(event) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;placeMarker(event.latLng);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function placeMarker(location) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var marker = new google.maps.Marker({<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: location,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map: map<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.setCenter(location);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">onload</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;init()&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Mapping Ducks<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;map_canvas&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;marker_data&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;createForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/new&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Title<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Description<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;description&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Latitude<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;latitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;latitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Longitude<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Address<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Save&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></div>
<p>There are two main things to note here. First &#8211; I&#8217;ve added a CSS file:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;css/main.css&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></div></div>
<p>This is not strictly necessary, but it&#8217;s good practice. the href is relative to your WAR&#8217;s file root, and now would be a good time to create this css file.<br />
Under your <strong>war</strong> directory in Eclipse, create a css folder, and inside create a new file and name it main.css.</p>
<div class="codecolorer-container css mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #cc00cc;">#map_canvas</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">650px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">450px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
<br />
<span style="color: #cc00cc;">#marker_data</span> <span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">450px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">450px</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span><span style="color: #933;">20px</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span><br />
input<span style="color: #00AA00;">,</span> textarea<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span><span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">&#125;</span></div></div>
<p>Granted, this is a very simple stylesheet, but you are free to play with it and change it as you like.<br />
The #map_canvas style changes the size of the map so that it doesn&#8217;t take up the entire paeg anymore, and let&#8217;s you add the form on the right. </p>
<p>The following code inside DuckMain.jsp takes care of displaying the form itself:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;marker_data&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;createForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/new&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Title<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Description<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">cols</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;description&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Latitude<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;latitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;latitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Longitude<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Address<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;longitude&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;66&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Save&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></div>
<p>This form doesn&#8217;t do anything yet, but that&#8217;s alright. All you care about right now is that your application&#8217;s main page displays something similar to the screenshot above.<br />
Right-click your project and run-as a Google web application to verify that it works on your http://localhost:8888/ (or whatever port you use).</p>
<h3>Populating the form</h3>
<p>Now that you have your page set up nicely, you can move on to the next task &#8211; populating the form. Specifically, what you want to do is get the application to auto-populate the longitude and latitude fields when you add a marker to the map. </p>
<p>Why would you want to do that? Well &#8211; that&#8217;s the best way to save a marker&#8217;s data to the data store, and it&#8217;s not something you should expect your users to provide themselves. Additionally, just to be nice (and give your user some sort of feedback that they placed the marker at the right spot on your map) &#8211; we&#8217;ll auto-populate the address field as well, with the address corresponding to the marker just placed.</p>
<p>Doing all this is actually quite simple. There are two changes to make. One to the init() function, and one to the placeMarker() function.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">//inside DuckMain.jsp<br />
.<br />
.<br />
&lt;script type=&quot;text/javascript&quot; src=&quot;http://maps.google.com/maps/api/js?sensor=false&quot;&gt;&lt;/script&gt;<br />
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js&quot;&gt;&lt;/script&gt; //1<br />
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> map<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> geocoder<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//2a</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> initialLocation <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">44.6895642</span><span style="color: #339933;">,</span><span style="color: #CC0000;">169.1320571</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; geocoder <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Geocoder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//2b</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> duckOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center<span style="color: #339933;">:</span> initialLocation<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapTypeId<span style="color: #339933;">:</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">MapTypeId</span>.<span style="color: #660066;">HYBRID</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Map</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;map_canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> duckOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position<span style="color: #339933;">:</span> initialLocation<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeMarker<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">latLng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> placeMarker<span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position<span style="color: #339933;">:</span> location<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//3 - all code until </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#longitude&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#latitude&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; geocoder.<span style="color: #660066;">geocode</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'latLng'</span><span style="color: #339933;">:</span> location<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>results<span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span> <span style="color: #339933;">==</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">GeocoderStatus</span>.<span style="color: #660066;">OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#gaddress&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">formatted_address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Geocode was not successful for the following reason: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//the end of (3)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>Let&#8217;s look at the changes introduced here.</p>
<ol>
<li>Adding jQuery<br />
This line loads jQuery on your page. While not completely necessary, using jQuery allows us to dynamically modify the input fields in the form and populate them with the details of the newly added marker. This is further explained in (3) below.</li>
<li><a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#Geocoder">Geocoder</a> is a Google Maps API v3 class that lets you find a physical address corresponding to a LatLng (latitude and Longitude) object. 2b is where you initialize it so you can later use it at point (3) </li>
<li>Ok, this is where the magic happens.<br />
As you may recall from part 2 of this tutorial, the placeMarker() function is called when the user clicks the map to add a location-marker on it.<br />
While inside this function, the position of the marker is known. It is passed as the <strong>location</strong> argument to the function.<br />
The following lines use jQuery to write the values of the location&#8217;s latitude and longitude values into the fields on the form.</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#longitude&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#latitude&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>They do this by locating the HTML elements with the ids of <strong>longitude</strong> and <strong>latitude</strong> and changing their values to be the longitude and latitude values from the location argument respectively.<br />
These ids correspond to the longitude and latitude input elements in the form.</p>
<p>This leaves us with the Geocoder code:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">geocoder.<span style="color: #660066;">geocode</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'latLng'</span><span style="color: #339933;">:</span> location<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>results<span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span> <span style="color: #339933;">==</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">GeocoderStatus</span>.<span style="color: #660066;">OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#markerAddress&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">formatted_address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Geocode was not successful for the following reason: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This piece of code uses the Geocoder mentioned before, to find the physical address corresponding to the latitude and longitude of the marker just placed on the map.<br />
Geocoder&#8217;s geocode method takes the location argument (which is a LatLng object), and a function definition. The function is called by the broswer on Geocode&#8217;s return from service, and takes two arguments: the results (which is an array) and a status code.<br />
Inside the function, it&#8217;s a good idea to first check the status code. If Geocode succeeded, you&#8217;ll get an OK status. In this case, using jQuery again, the function places the formatted_address returned into the input field whose id is <strong>markerAddress</strong>.<br />
If for some reason Geocoder failed to return an address &#8211; an alert is displayed to the user.
</li>
</ol>
<h3>So far so good?</h3>
<p>It is time to test the application, to ensure that all is working correctly.<br />
Run your application again, and navigate to http://localhost:8888 (or wherever it is your application gets deployed to). It&#8217;s probably a good idea to zoom the map in a little bit, but you don&#8217;t absolutely have to do it.<br />
Click somewhere on the map, and if everything works as expected, you should have a new marker added, and the three form fields (latitude, longitude and address) should get populated with the data corresponding to your marker. For example &#8211; say you placed your marker on 56 Brownston Street, Wanaka (the location of the fantastic <a href="http://www.matterhornsouth.co.nz/">Matterhorn South</a> in New Zealand). You should get something similar to the following screenshot:<br />
<a href="http://duckranger.com/wp-content/uploads/2011/07/matterhorn.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/matterhorn-300x129.png" alt="" title="matterhorn" width="300" height="129" class="aligncenter size-medium wp-image-790" /></a></p>
<h3>Saving data to the data store</h3>
<p>Placing markers on the map and finding their location and address is all very well. But you probably want to do a little bit more with the markers you place. This is where the appengine&#8217;s datastore comes into the game.<br />
This post is not about the datastore, and there&#8217;s a lot to write about it. For the purpose of this simple application, it&#8217;s enough to show how to easily save the markers data there.</p>
<p>I&#8217;ll use the JPA flavor of access, just because it is quite straight-forward. There are only a few files you need to add to the application in order to persist and read your data.</p>
<p>There are a lot of sources on the web for how to do this, I personally like <a href="http://www.vogella.de/blog/2011/01/10/google-app-engine-jpa/">Lars Vogel&#8217;s blog</a>, and used his suggestions here.</p>
<p>First &#8211; create persistence.xml under <strong>src/META-INF</strong> in your project:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence</span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;transactions-optional&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/provider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;datanucleus.NontransactionalRead&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;datanucleus.NontransactionalWrite&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;datanucleus.ConnectionURL&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;appengine&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>This will tell appengine to use JPA for your application. Note &#8211; when using the local version of appengine (that is &#8211; when you deploy your application locally rather than to appengine itself) &#8211; your data store is found in your Eclipse&#8217;s workspace, under WEB-INF, in a folder called <i>appengine-generated</i>. This is good to know, because when you make changes to your models and nothing works anymore &#8211; you need to remember to delete the files under this folder, to let your local appengine environment re-generate the data store with the new models structure.</p>
<p>After you sorted persistence.xml out, you need to add a way to hook into the EntityManager, and a model too:</p>
<p><strong>EntityManagerService</strong></p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.maps</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManagerFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Persistence</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EntityManagerService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> EntityManagerFactory emfInstance <span style="color: #339933;">=</span> Persistence<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;transactions-optional&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> EntityManagerService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> EntityManagerFactory get<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> emfInstance<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The EntityManagerService class is a simple Singleton, that lets you (via the get() method) acquire a handle to an EntityManagerFactory. The factory will be used later to getn EntityManager instance &#8211; where all the good things happen (well, at least as far as the data store is concerned).</p>
<p>Now &#8211; the model. We&#8217;ll name our model class Duck, as the application is really about spotting ducks in the wild (if you haven&#8217;t figured it out yet&#8230;)</p>
<p><strong>Duck.java</strong></p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.maps</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GeneratedValue</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GenerationType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Entity</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Duck <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Id<br />
&nbsp; &nbsp; @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> duckId<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> longitude<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> latitude<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> address<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> title<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//I have ommitted the getters and setters - you have to have them, though.&nbsp; </span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>There&#8217;s not much to this class. It&#8217;s only worth noting that the @Entity annotation tells the appengine that this class is an entity worth persisting, and the duckId plus its annotations &#8211; which designate this field as the primary key for the Duck entity, and tell appengine to generate it itself.</p>
<p>To actually save your ducks (or, the markers we place on the map&#8230;) &#8211; you&#8217;ll need to create one more class: a servlet that listens for the form-submission on DuckMain.jsp, and persists the data to the data store.</p>
<p><strong>NewDuck servlet</strong></p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.maps.servlet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.duckranger.maps.Duck</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.duckranger.maps.EntityManagerService</span><span style="color: #339933;">;</span><br />
<br />
@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serial&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NewDuck <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; EntityManager entityManager <span style="color: #339933;">=</span> EntityManagerService.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Duck duck <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Duck<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; duck.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; duck.<span style="color: #006633;">setDescription</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; duck.<span style="color: #006633;">setLatitude</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span>.<span style="color: #006633;">parseDouble</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;latitude&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; duck.<span style="color: #006633;">setLongitude</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Double</span>.<span style="color: #006633;">parseDouble</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;longitude&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; duck.<span style="color: #006633;">setAddress</span><span style="color: #009900;">&#40;</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entityManager.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>duck<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entityManager.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">sendRedirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/DuckMain.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As promised, this is a very simple servlet, which only has the <i>doPost()</i> method. This is important, because we only expect the servlet to receive an HTTP POST method (via the form submission on DuckMain.jsp)</p>
<p>The servlet acquires an EntityManager instance using our EntityManagerService class. It then creates a new Duck object, and sets its values based on the request parameters it received. (Note that there&#8217;s no validation made on these parameters, you are welcome to add it if you want).</p>
<p>After the new Duck is ready, the servlet tries to save it to the datastore using EntityManager&#8217;s persist() method. After all is done &#8211; the servlet redirects the user back to DuckMain.jsp.</p>
<p>There&#8217;s only one more thing to do in order to tie it all together, and that&#8217;s mapping the NewDuck servlet in web.xml.</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- These are snippets inside web.xml --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NewDuck<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.duckranger.maps.servlet.NewDuck<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
.<br />
.<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NewDuck<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/new<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>The first snippet you add to web.xml define a new servlet in the system: <strong>NewDuck</strong>. Note the servlet-class definition &#8211; it may be different for your code if you used a different package.</p>
<p>The second snippet maps the <strong>NewDuck</strong> servlet to serve the <strong>/new</strong> URL requests. If you look back at DuckMain.jsp, you&#8217;ll see the following line:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;createForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/new&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">accept-charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span></div></div>
<p>This defines the target of the form (that is &#8211; the URL it will try to submit to) as <strong>/new</strong>. Working together, web.xml and DuckMain.jsp tell appengine that when the form on DuckMain.jsp is submitted, it should send the HTTP POST request to the NewDuck servlet.</p>
<h3>Verify that it works</h3>
<p>Try and redeploy your application. Before the last batch of changes, you saw that when you add a marker on the map, the application populates the latitude, longitude and address fields. Do it now, and add a title and description. Click on Save, and if everything works &#8211; you should be redirected to the (empty, and centered on the Wanaka Bakpaka) DuckMain.jsp page.</p>
<h3>Seeing your saved ducks</h3>
<p>If you did this, then it probably all works, but you&#8217;re wondering &#8211; did my application really save the marker&#8217;s data?<br />
Well, let&#8217;s verify that!</p>
<p>To verify that your ducks are saved, you&#8217;ll add one more servlet to your application &#8211; a list servlet:</p>
<p><strong>ListDucks servlet</strong></p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.maps.servlet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.EntityManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.duckranger.maps.Duck</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.duckranger.maps.EntityManagerService</span><span style="color: #339933;">;</span><br />
<br />
@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serial&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ListDucks <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; EntityManager entityManager <span style="color: #339933;">=</span> EntityManagerService.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createEntityManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Duck<span style="color: #339933;">&gt;</span> ducks <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query duckQuery <span style="color: #339933;">=</span> entityManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select d from Duck d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ducks <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Duck<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>duckQuery.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entityManager.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ducks <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;</span> <span style="color: #339933;">!</span>ducks.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ducks:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Duck d <span style="color: #339933;">:</span> ducks<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Duck: &quot;</span><span style="color: #339933;">+</span> d.<span style="color: #006633;">getTitle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; at &quot;</span><span style="color: #339933;">+</span> d.<span style="color: #006633;">getAddress</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No ducks yet.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Quite the simple servlet this one.</p>
<p>Note that, unlike what you did in the <strong>NewDuck</strong> servlet, in ListDucks you are providing a doGet() method. This makes sense, because this servlet only lists data &#8211; it doesn&#8217;t create or change data on the server.</p>
<p>Inside the try block, the servlet tries to retrieve all Ducks from the data store into a local ArrayList object.<br />
After that, if the query returned actual Ducks &#8211; the servlet iterates over the Duck objects, and prints out the title and address the user entered for each Duck.</p>
<p>To see this servlet in action, you&#8217;ll need to go back to web.xml and register it with the application:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- snippets in web.xml --&gt;</span><br />
.<br />
.<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ListDucks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.duckranger.maps.servlet.ListDucks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
.<br />
.<br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ListDucks<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/list<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
.<br />
.</div></div>
<p>As before &#8211; you define the servlet itself, and give it a servlet mapping. Unlike the case of the NewDuck sevlet, the url-pattern for this servlet-mapping does not depend on anything. I chose to define it as <strong>/list</strong> but you can change it to a different mapping if you like.</p>
<p>When you redeploy your application and point your browser at http://localhost:8888/list &#8211; you should see the ducks that are already registered in your application. (Something like the following screenshot)</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/list.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/list-300x110.png" alt="" title="list" width="300" height="110" class="aligncenter size-medium wp-image-796" /></a></p>
<p>And that&#8217;s it for this part of the tutorial. In the next part &#8211; you&#8217;ll see how to load the Ducks from the data store so that you can display them on the map, and add those nice little baloons to them as well.</p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=776&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/07/google-appengine-and-maps-tutorial-with-java-part-iii/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spring MVC 3.0 with STS Tutorial &#8211; Part I</title>
		<link>http://duckranger.com/2011/07/spring-mvc-3-0-with-sts-tutorial-part-i/</link>
		<comments>http://duckranger.com/2011/07/spring-mvc-3-0-with-sts-tutorial-part-i/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 04:48:47 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring MVC]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=741</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/46944516@N00/1301014184/" title="Leadership" target="_blank"><img src="http://farm2.static.flickr.com/1171/1301014184_3786e4d2b8_m.jpg" alt="Leadership" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/46944516@N00/1301014184/" title="pedrosimoes7" target="_blank">pedrosimoes7</a></div>
</p><p>In this part, I&#8217;ll show how to get started with STS (SpringSource Tool Suite) and Spring MVC. We&#8217;ll only use the default project that STS generates, but it&#8217;s a good start to any development effort with Spring MVC you might get into.<br />
STS is the easiest IDE to use when developing Spring, mainly because it bundles together almost everything you need to get started, and knows where to get the rest.<br />
If you don&#8217;t have STS installed, &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/46944516@N00/1301014184/" title="Leadership" target="_blank"><img src="http://farm2.static.flickr.com/1171/1301014184_3786e4d2b8_m.jpg" alt="Leadership" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/46944516@N00/1301014184/" title="pedrosimoes7" target="_blank">pedrosimoes7</a></div>
<p>In this part, I&#8217;ll show how to get started with STS (SpringSource Tool Suite) and Spring MVC. We&#8217;ll only use the default project that STS generates, but it&#8217;s a good start to any development effort with Spring MVC you might get into.<br />
STS is the easiest IDE to use when developing Spring, mainly because it bundles together almost everything you need to get started, and knows where to get the rest.<br />
If you don&#8217;t have STS installed, just download it from SpringSource&#8217;s website <a href="http://www.springsource.com/downloads/sts" target="_blank">here</a> and install it on your machine.<br />
<span id="more-741"></span><br />
When you get STS up and running for the first time, it will ask you (as all Eclipse distributions do) which workspace to use, and then open the welcome screen. You don&#8217;t need the welcome screen. What you want is the dashboard.</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard-300x159.png" alt="" title="dashboard" width="300" height="159" class="aligncenter size-medium wp-image-742" /></a></p>
<p>From the dashboard screen choose Spring Template Project. This will open the following view:<br />
<a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard1.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard1-300x278.png" alt="" title="dashboard" width="300" height="278" class="aligncenter size-medium wp-image-744" /></a></p>
<p>Choose a Spring MVC project.<br />
The first time you do it, STS might ask you to download some extra elements from the Internet. Let it download &#8211; it will not bother you again.<br />
You&#8217;ll need to give your project a name and a top-level package:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard2.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard2-300x278.png" alt="" title="dashboard" width="300" height="278" class="aligncenter size-medium wp-image-745" /></a></p>
<p>Click on finish – and STS will create the project for you. You can run the project by right-clicking it and selecting &#8216;run on server&#8217;. This will open the following dialog:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard3.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard3-243x300.png" alt="" title="dashboard" width="243" height="300" class="aligncenter size-medium wp-image-746" /></a></p>
<p>The Fabric tc server was created for you with STS. Later you might want to change it to Tomcat or another server – but for now this is alright.</p>
<p>Choose the server and click on next – you&#8217;ll see the following:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard4.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard4-243x300.png" alt="" title="dashboard" width="243" height="300" class="aligncenter size-medium wp-image-747" /></a></p>
<p>This dialogue lets you choose which projects you want to deploy on the server. As GoodProject is the only project in the workspace – it will be the only one here, and automatically selected for deployment because you selected it to run on the server.<br />
Click on &#8220;Finish&#8221; to launch the server runtime. STS will ask you if you want to use Spring Insight. You can activate it if you like, but most of the times I tried that – you get an error and the server fails to start. To fix this error you&#8217;ll need to delete the server in STS and create a new one – so you might as well not activate it.</p>
<p>If everything went smooth, STS will open the home page for GoodProject, Which displays an <strong>hello world</strong> message with the current server time.</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard5.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard5-300x176.png" alt="" title="dashboard" width="300" height="176" class="aligncenter size-medium wp-image-749" /></a></p>
<p>Ok, so at this point – you know that you have everything working. Granted – you didn&#8217;t really have to do much – all you did is create a default Spring MVC project in STS and run it.</p>
<p>The New Spring  MVC  project template created all the things you need to have a Spring MVC project running. This comprises a few files, and the rest of this tutorial is dedicated to reviewing them.</p>
<h3>home.jsp</h3>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard6.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard6-300x236.png" alt="" title="dashboard" width="300" height="236" class="aligncenter size-medium wp-image-750" /></a></p>
<p>The first file I want to discuss is the home.jsp file. This file is the default view generated by the new Spring MVC project wizard. Currently, this is the only thing a user will see when they access your application on the server, so it is a good place to start.</p>
<p>home.jsp is a JSP file. In Spring MVC, you don&#8217;t <strong>have</strong> to use JSP as your view technology. Most projects I worked on actually use JSF, but it doesn&#8217;t matter much as far as the Spring framework is concerned.</p>
<p>This is what home.jsp contains:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard7.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard7-300x236.png" alt="" title="dashboard" width="300" height="236" class="aligncenter size-medium wp-image-751" /></a></p>
<p>On the left side of the screenshot you can see the code in the JSP file. On the right side – you see how this code is displayed in a browser.</p>
<p>The first two lines of the generated JSP page are general JSP declarations: setting up the standard tag library, and declaring that the page does not use the HTTP session. Those lines are not important for us right now.</p>
<p>In fact, the entire JSP page is just some plain HTML which should be quite straight-forward to read. The only piece that&#8217;s interesting is the part that reads  ${serverTime}.</p>
<p>As you can see on the right, Spring replaces the ${serverTime} phrase with a date and time string. Don&#8217;t go looking for it through the JSTL reference though – it&#8217;s not a tag. We&#8217;ll touch more on this when we talk about the controller, but for the completeness of the home.jsp discussion – it&#8217;s enough to say that serverTime is a property set by the controller and used by the view. (Just like CakePHP and other MVC frameworks, Spring MVC lets your controllers pass values to use in your views).</p>
<p>That&#8217;s it really for home.jsp. The only other thing worth noting at this point is that it doesn&#8217;t have a link to a css file. That&#8217;s alright.</p>
<p>Ok, so now you know what the home.jsp file contains. It is a simple view, that uses a value given to it by the controller. It is now time to look at this controller and see what it does.</p>
<h3>HomeController.java</h3>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard8.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard8-186x300.png" alt="" title="dashboard" width="186" height="300" class="aligncenter size-medium wp-image-754" /></a></p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.goodproject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.text.DateFormat</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Locale</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Controller</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.ui.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.RequestMapping</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.RequestMethod</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Handles requests for the application home page.<br />
&nbsp;*/</span><br />
@Controller<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HomeController <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>HomeController.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Simply selects the home view to render by returning its name.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; @RequestMapping<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span>, method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> home<span style="color: #009900;">&#40;</span><span style="color: #003399;">Locale</span> locale, Model model<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome home! the client locale is &quot;</span><span style="color: #339933;">+</span> locale.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">DateFormat</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #003399;">DateFormat</span>.<span style="color: #006633;">getDateTimeInstance</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">DateFormat</span>.<span style="color: #006633;">LONG</span>, <span style="color: #003399;">DateFormat</span>.<span style="color: #006633;">LONG</span>, locale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> formattedDate <span style="color: #339933;">=</span> dateFormat.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serverTime&quot;</span>, formattedDate <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;home&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The first thing to note about the Controller is that it is really just a simple class. In Spring MVC 3 – you don&#8217;t need to make the controller extend or implement anything. However, you have to annotate it with the @Controller annotation.  (line 17)</p>
<p>This annotation lets Spring know that this class is actually a controller, and set it up correctly at deployment time.<br />
Line 20 shows you how to acquire a handle to a logger. This is a normal slf4j logger that Spring provides you with. Don&#8217;t fret about it.</p>
<p>Line 26 is where the interesting part begins.  The generated controller has only one method – the home() method. There&#8217;s no mandated relationship between the name of the controller (HomeController) and the name of the method, or its return value (which we&#8217;ll get to soon) – Inside this class – all of them are more or less arbitrary. The return value could be any other String (however – you need to make sure it can be matched to a view. We&#8217;ll see in a bit how the “home” return value is mapped to the home.jsp).</p>
<p>The method home() is annotated with a @RequestMapping annotation. This tells Spring that this method handles requests coming in from the user. The @RequestMapping tells Spring that this method should be used to serve requests coming in to the &#8216;/&#8217; context (or the home/root context for the application), but – it is restricted to HTTP GET requests only.<br />
This restriction isn&#8217;t very important for the default home.jsp page, as there&#8217;s no way to change the server&#8217;s data status with this page. However, sometimes you&#8217;ll want to restrict specific methods to server only POST requests (so, for example, to make sure the user fills out a form rather than craft a GET request to your website) – and that&#8217;s where this option comes in handy.</p>
<p>The home method generated by the template takes two arguments:  A locale and a Model.  Note that this is not set in stone. Just as the name of the method is flexible – so are the arguments and even the access modifier (it&#8217;s true. Try removing the Locale argument or changing the method to be private – your application will still deploy). </p>
<p>To understand why this works – we need to delve deep into the way Spring-MVC does things, and it&#8217;s probably a good subject for a different blog post altogether. For now it is enough to understand that because we don&#8217;t actually call this method directly (the container calls it for us when the relevant request comes in) – then it is the container&#8217;s responsibility to match the call it makes to the method&#8217;s signature. If we define a locale – the container (using the DispatcherServlet and its adapters) will find the locale sent by the HTTP headers, and pass the home() method a matching Locale object. If we were to change the method signature to only take a model – well, the Spring container will not do that (you&#8217;ll still be able to find the Locale, by the way, but if you need it inside the method – might as well add it to the signature).</p>
<p>So, that&#8217;s settled then, for now. Let&#8217;s agree that there are a multitude of possible method signatures that are able to serve as controller methods.<br />
The first thing the home() method does (line 27) is log the Locale it discovered. You should be able to see this logged in the server&#8217;s console window inside STS:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard9.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard9-300x159.png" alt="" title="dashboard" width="300" height="159" class="aligncenter size-medium wp-image-755" /></a></p>
<p>As you can see, the last line in the server log is the INFO log message, detailing the locale I used to access the home() method.<br />
Lines 29-32 create a formatted date String. This String is actually what the home.jsp page displays using the ${serverTime} directive we saw before. How does it do that?<br />
Well – line 34 actually takes care of that:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serverTime&quot;</span>, formattedDate <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Spring-MCV is (surprise?) an MVC framework. We&#8217;ve already seen the view (home.jsp) and the controller (HomeController.java) &#8211; surely you&#8217;d expect Spring-MVC to provide a model as well.</p>
<p>Remember home()&#8217;s method signature – it had a Model object inside it. This Model is supplied by the container to the controller method. The method uses this model to communicate with the view.<br />
Model is a Spring interface:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">*</span> Copyright <span style="color: #cc66cc;">2002</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2009</span> the original author or authors.<br />
<br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.springframework.ui</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* Java-5-specific interface that defines a holder for model attributes.<br />
&nbsp;* Primarily designed for adding attributes to the model.<br />
&nbsp;* Allows for accessing the overall model as a &lt;code&gt;java.util.Map&lt;/code&gt;.<br />
&nbsp;*<br />
&nbsp;* @author Juergen Hoeller<br />
&nbsp;* @since 2.5.1<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Model <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Add the supplied attribute under the supplied name.<br />
&nbsp; &nbsp; &nbsp;* @param attributeName the name of the model attribute (never &lt;code&gt;null&lt;/code&gt;)<br />
&nbsp; &nbsp; &nbsp;* @param attributeValue the model attribute value (can be &lt;code&gt;null&lt;/code&gt;)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Model addAttribute<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> attributeName, <span style="color: #003399;">Object</span> attributeValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Add the supplied attribute to this &lt;code&gt;Map&lt;/code&gt; using a<br />
&nbsp; &nbsp; &nbsp;* {@link org.springframework.core.Conventions#getVariableName generated name}.<br />
&nbsp; &nbsp; &nbsp;* &lt;p&gt;&lt;emphasis&gt;Note: Empty {@link java.util.Collection Collections} are not added to<br />
&nbsp; &nbsp; &nbsp;* the model when using this method because we cannot correctly determine<br />
&nbsp; &nbsp; &nbsp;* the true convention name. View code should check for &lt;code&gt;null&lt;/code&gt; rather<br />
&nbsp; &nbsp; &nbsp;* than for empty collections as is already done by JSTL tags.&lt;/emphasis&gt;<br />
&nbsp; &nbsp; &nbsp;* @param attributeValue the model attribute value (never &lt;code&gt;null&lt;/code&gt;)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Model addAttribute<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> attributeValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Copy all attributes in the supplied &lt;code&gt;Collection&lt;/code&gt; into this<br />
&nbsp; &nbsp; &nbsp;* &lt;code&gt;Map&lt;/code&gt;, using attribute name generation for each element.<br />
&nbsp; &nbsp; &nbsp;* @see #addAttribute(Object)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Model addAllAttributes<span style="color: #009900;">&#40;</span>Collection<span style="color: #339933;">&lt;?&gt;</span> attributeValues<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Copy all attributes in the supplied &lt;code&gt;Map&lt;/code&gt; into this &lt;code&gt;Map&lt;/code&gt;.<br />
&nbsp; &nbsp; &nbsp;* @see #addAttribute(String, Object)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Model addAllAttributes<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, <span style="color: #339933;">?&gt;</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Copy all attributes in the supplied &lt;code&gt;Map&lt;/code&gt; into this &lt;code&gt;Map&lt;/code&gt;,<br />
&nbsp; &nbsp; &nbsp;* with existing objects of the same name taking precedence (i.e. not getting<br />
&nbsp; &nbsp; &nbsp;* replaced).<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Model mergeAttributes<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, <span style="color: #339933;">?&gt;</span> attributes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Does this model contain an attribute of the given name?<br />
&nbsp; &nbsp; &nbsp;* @param attributeName the name of the model attribute (never &lt;code&gt;null&lt;/code&gt;)<br />
&nbsp; &nbsp; &nbsp;* @return whether this model contains a corresponding attribute<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> containsAttribute<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> attributeName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* Return the current set of model attributes as a Map.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> asMap<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>As you can see, the Model interface as far as the code is concerned, is a Map of attributes. The home() method uses the Model in line 34 and adds an attribute to it, using “serverName” as the key, and the formatted date string as the value.<br />
This model is available to the home.jsp view we discussed earlier. In the view, you can use the ${&#8230;} notation to ask Spring to replace the attribute&#8217;s key with its value.</p>
<p>The JSP is processed on the server side before being sent to the client&#8217;s browser. During this processing, the server finds the ${serverTime} directive, and replaces it with the Model value corresponding to the serverTime key.<br />
You can verify that this is what actually happens by trying to change the key to a non-existent one. You don&#8217;t have to redeploy the application if you only change a JSP. Just refresh the page, and see for yourself:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard10.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard10-300x159.png" alt="" title="dashboard" width="300" height="159" class="aligncenter size-medium wp-image-757" /></a></p>
<p>If you change ${serverTime} to something else  &#8211; e.g. ${noTime} – the JSP processor will not find the key in the Model, and will silently fail – the time string is still there on the Model, but it isn&#8217;t found, and isn&#8217;t displayed on the page. Now change the bad value back to ${serverTime} and refresh the page – the time String should re-appear.</p>
<p>The last line in the home() method (line 36) returns a String to the caller: &#8220;home&#8221;. This String is used by the<br />
container to decide which view it is supposed to render for the user, using the model.</p>
<p>So, to sum up so far: you define a controller using the @Controller annotation, and inside this controller you define a method that handles a specific request (an HTTP GET of the &#8216;/&#8217; context).<br />
Because of these annotations, when a request comes in to this URL, the Spring container knows that it needs to serve it using this controller and the specific method inside it.</p>
<p>The method does some work, and then returns a String. This String (&#8220;home&#8221;) lets the container know where to go to after the controller finished processing the request. In the case of the generated code, the container will use this String to decide which view (JSP file) to render back to the client.<br />
Let&#8217;s see how this works then – the servlet-context.xml file.</p>
<h3>servlet-context.xml</h3>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard11.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard11-186x300.png" alt="" title="dashboard" width="186" height="300" class="aligncenter size-medium wp-image-758" /></a></p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/mvc&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">xmlns:beans</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span><br />
<span style="color: #009900;">&nbsp; &nbsp; <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd</span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><br />
<span style="color: #009900;">&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Enables the Spring MVC @Controller programming model --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;annotation-driven</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources</span> <span style="color: #000066;">mapping</span>=<span style="color: #ff0000;">&quot;/resources/**&quot;</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;/resources/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">&lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.jsk&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;com.duckranger.goodproject&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>servlet-context.xml is used by Spring-MVC to configure navigation, beans and view resolvers. The file name does not have to be servlet-context, but this is the default configuration and it’s good enough for this tutorial.<br />
The full picture is this: Spring-MVC uses a special servlet called DispatcherServlet to control and serve all requests to your application. To integrate with the servlet container, this DispatcherServlet is configured in the normal web.xml file like so:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>appServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/spring/appServlet/servlet-context.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>appServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>As you can see, the servlet is configured very much like any other servlet you’d configure in a normal web application. It is given a default name: appServlet, and it is configured to serve all requests coming to the ‘/’ (root) context.<br />
The org.springframework.web.servlet.DispatcherServlet is configured with an init-param that points to the location of its configuration file. This file Is the servlet-context.xml.</p>
<p>When the application is deployed, your servlet container scans web.xml for (among other things) servlets to load. It finds the DispatcherServlet with the highest priority for load-on-startup (load-on-startup tells the server container in what order it should initialize the servlets it encounters. The lower the number – the earlier the initialization will happen. The lowest meaningful value here is 1).</p>
<p>When the DispatcherServlet is initialized by the servlet container, it looks at its init-param, and finds its contextConfigLocation, which points to the servlet-context.xml file. As I mentioned before – you may change the name (or the location) of this file. As long as you configure it correctly in your web.xml – you should be fine.<br />
Ok, back to servlet-context.xml:</p>
<p>There are a few important things to note in this configuration file:</p>
<ol>
<li>The &lt;annotation-driven /> element.<br/><br />
This element tells the DispatcherServlet that it shouldn&#8217;t expect controllers to be configured in XML, rather – it will get its components via another method. This method can be found further down the file: &lt;context:component-scan base-package=&#8221;com.duckranger.goodproject&#8221; /></li>
<li>&lt;context:component-scan base-package=&#8221;com.duckranger.goodproject&#8221; /> tells the Spring container where it can find components. In this case, the directive tells Spring to look for components in the com.duckranger.goodproject package. Spring will scan this package and will register as a component  any class it finds annotated with the relevant annotations (e.g. @Controller).<br />
So, together with &lt;annotation-driven/> &#8211; these two directives ensure that Spring finds the HomeController class and registers it as a controller in the Spring-MVC application.</li>
<li>The final element worth noticing in this file is the definition of a ViewResolver:<br/>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">&lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;</span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>A ViewResolver is another Spring interface, which is responsible for directing a client to the right view in the application.</li>
</ol>
<h4>A bit about view resolvers</h4>
<p>Generally, a controller method should not return just a String. It should actually return a ModelAndView object, which contains both the model we discussed earlier, and the view to resolve (or rather – a way to find the view).<br />
However, as you can see, in our generated Spring-MVC project, the controller method returns only a String, which contains the view name. This is alright, as long as we tell the container how it should resolve the view and serve the correct page to the client. </p>
<p>The InternalResourceViewResolver does exactly that. It is configured as a bean, and since it is the only view resolver configured, the Spring container knows that this is the one it should use. In essence, the InternalResourceViewResolver receives the result of the controller method (the String returned) – and is then in charge of mapping this result to an actual view.</p>
<p>In the definition above, we define two properties on this resolver bean: a prefix and a suffix. The InternalResourceViewResolver concatenates the prefix, controller-return-value and suffix to come up with the actual view name to display. In our case, this will be /WEB-INF/views/home.jsp.</p>
<p>Using this information, it finds the right JSP page to load, processes it using the model object (same Model object in the home() method) – and sends a response back to the client.<br />
You can verify that this is the way the InternalResourceViewResolver works by changing a few values and watching the result. Try the following:</p>
<p>Change the return value of the home() method to be &#8220;duckranger&#8221;, save the HomeController class, wait a few seconds until you see the server has redeployed (It will echo <strong>INFO: Reloading Context with name [/GoodProject] is completed in the console</strong>) – and refresh the STS browser page – it should show the error:</p>
<p><a href="http://duckranger.com/wp-content/uploads/2011/07/dashboard12.png"><img src="http://duckranger.com/wp-content/uploads/2011/07/dashboard12-300x159.png" alt="" title="dashboard" width="300" height="159" class="aligncenter size-medium wp-image-766" /></a></p>
<p>Which makes sense, as there&#8217;s no duckranger.jsp page.</p>
<p>You can also try and change the prefix or suffix in servlet-context.xml (you may have to manually redeploy for this change to take effect, though). If you change the suffix from value=&#8221;.jsp&#8221; to value=&#8221;.duck&#8221; and try to access your application, you&#8217;ll find that the server complains.<br />
In the server log (STS console) you’ll see the following:<br />
<strong>WARN : org.springframework.web.servlet.PageNotFound &#8211; No mapping found for HTTP request with URI [/GoodProject/WEB-INF/views/home.duck] in DispatcherServlet with name &#8216;appServlet&#8217;</strong></p>
<p>So, now that we&#8217;re satisfied that the view resolver indeed resolves view – there are only a couple of files left in the project – the log4j.xml and pom.xml configuration files. These are your maven build and log4j configuration files, which are  not strictly a part of the Spring-MVC and are beyond the scope of this tutorial. We don&#8217;t care much for them right now.</p>
<h3>Part I Summary</h3>
<p>In this part youjust went through the real basics of Spring MVC 3.0. Set up the STS development environment, created the template Spring MVC project, and investigated the files that got created.<br />
Generally speaking, the control structure of a Spring-MVC application is quite simple:</p>
<ol>
<li>
You start by hooking up Spring&#8217;s DispatcherServlet as the main entry point to your servlet container. This is done in web.xml, like you define any other servlet, with the exception of specifying a configuration file in DispatcherServlet&#8217;s init-param.</li>
<li>
The DispatcherServlet&#8217;s configuration file tells the Spring container where it can find its controllers, and configures a view resolver to handle the values returned from the controller methods, and resolve them into the right views to display.</li>
<li>
Once you have this sorted, all that&#8217;s left to get a simple application up and running is to create a simple controller and a simple view.</li>
<li>
The DispatcherServlet finds the controller by its @Controller annotation, and by its being part of the relevant package configured with the component-scan element in its configuration file.</li>
<li>
Using the @RequestMapping annotation, the DispatcherServlet known which controller method it needs to call when a request comes in to the &#8216;/&#8217; context.<br />
The DispatcherServlet calls this method &#8211; home() – and gives it a Model to use for passing data to the view.</li>
<li>
The home() method then returns a String – &#8220;home&#8221; – that&#8217;s used by the view resolver to resolve into a JSP page to render and return to the client.</li>
</ol>
<p>That&#8217;s it for today&#8230; In the next part of the tutorial, I&#8217;ll talk about some more things we can do with Spring MVC.</p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=741&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/07/spring-mvc-3-0-with-sts-tutorial-part-i/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Google appengine and maps tutorial with Java &#8211; Part II</title>
		<link>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-ii/</link>
		<comments>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-ii/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 00:42:18 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[appengine]]></category>
		<category><![CDATA[Googlemaps]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[javaScript]]></category>
		<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=668</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/79471640@N00/485201047/" title="The ugly duckling" target="_blank"><img src="http://farm1.static.flickr.com/194/485201047_4215550cf6_m.jpg" alt="The ugly duckling" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc/2.0/" title="Attribution-NonCommercial License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/79471640@N00/485201047/" title="pasma" target="_blank">pasma</a></div>
</p><p>Google’s <a href="http://appengine.google.com">appengine </a>is the Google cloud offering. It is highly scalable, and provides a free entry level cloud application platform. However, it is not very trivial to develop for and deploy on.<br />
Google maps API version 3 is the current (June 2011) officially supported version of integrating with Google maps. The API allows you to display and manipulate maps in may ways.</p>
<p>The full tutorial provides an introduction to developing and deploying a simple maps application to &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/79471640@N00/485201047/" title="The ugly duckling" target="_blank"><img src="http://farm1.static.flickr.com/194/485201047_4215550cf6_m.jpg" alt="The ugly duckling" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc/2.0/" title="Attribution-NonCommercial License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/79471640@N00/485201047/" title="pasma" target="_blank">pasma</a></div>
<p>Google’s <a href="http://appengine.google.com">appengine </a>is the Google cloud offering. It is highly scalable, and provides a free entry level cloud application platform. However, it is not very trivial to develop for and deploy on.<br />
Google maps API version 3 is the current (June 2011) officially supported version of integrating with Google maps. The API allows you to display and manipulate maps in may ways.</p>
<p>The full tutorial provides an introduction to developing and deploying a simple maps application to the appengine using Eclipse. Part 2 walks you through creating a simple project displaying a clickable map using the googlemaps API v3.<br />
<span id="more-668"></span><br />
In the <a href="http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-1/">previous part of this tutorial</a>, we set up Eclipse to work with a local appengine-like environment. In this part, we&#8217;ll create a small application that displays a map on the screen, and lets the user interact with it by clicking the map and adding data to it.</p>
<h2>Start with a fresh project</h2>
<p>If you followed the first part of the tutorial, you have a working appengine project. We used it to verify that everything works. However, we are not going to use it in this second part. Close the project and create a new one (Using <b>File->New->Web application project</b>). Give your project a name and package &#8211; but do not select to use GWT or generate any code.<br />
<div id="attachment_674" class="wp-caption aligncenter" style="width: 252px"><a href="http://duckranger.com/wp-content/uploads/2011/06/no-generate.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/no-generate-242x300.png" alt="Plain vanilla appengine project" title="Plain vanilla appengine project" width="242" height="300" class="size-medium wp-image-674" /></a><p class="wp-caption-text">Plain vanilla appengine project</p></div></p>
<p>This should generate a plain vanilla project that&#8217;s appengine ready, but has only a little bit of code in it. The following Project explorer screenshot should resemble yours:<br />
<div id="attachment_677" class="wp-caption aligncenter" style="width: 210px"><a href="http://duckranger.com/wp-content/uploads/2011/06/pexplore.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/pexplore-200x300.png" alt="Project Explorer - default project/" title="Project Explorer - default project/" width="200" height="300" class="size-medium wp-image-677" /></a><p class="wp-caption-text">Project Explorer - default project/</p></div></p>
<h2>What did the plugin generate?</h2>
<p>The plugin generated for you the blueprint required for an appengine project. You can actually run the project you just created (right click the project and select <b>Run as->Web application</b>). If you followed the previous part of the tutorial you&#8217;ll notice a small difference: this time Eclipse does not open an Development mode view. All you see is the console telling you it started a local server:<br />
<div id="attachment_681" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/console.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/console-300x58.png" alt="Appengine console view" title="Appengine console view" width="300" height="58" class="size-medium wp-image-681" /></a><p class="wp-caption-text">Appengine console view</p></div></p>
<p>Open a browser and point it at <a href="http://localhost:8888">http://localhost:8888</a> (your console might show a different url) &#8211; and you should see an <b>Hello App Engine!</b> page that lists one available servlet. Click it, and you&#8217;ll see the default application:<br />
<div id="attachment_682" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/def.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/def-300x65.png" alt="Default servlet" title="Default servlet" width="300" height="65" class="size-medium wp-image-682" /></a><p class="wp-caption-text">Default servlet</p></div></p>
<h2>What&#8217;s in my generated code?</h2>
<p>If you look into the code generated for you, it isn&#8217;t very interesting. The Duckranger_MapsServlet class (the actual name depends on the project name you entered when you created the project) &#8211; contains the following code:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.maps</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span><br />
<br />
@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;serial&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Duckranger_MapsServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resp.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello, world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>All it really is a simple Servlet that doesn&#8217;t do anything (except serve the request).</p>
<p>The other simple file the plugin generated is your WEB-INF/web.xml:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">xmlns:web</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span><br />
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee</span><br />
<span style="color: #009900;">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Duckranger_Maps<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.duckranger.maps.Duckranger_MapsServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Duckranger_Maps<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/duckranger_maps<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>index.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>Again, nothing too complex. Just a servlet definition and mapping for your servlet.</p>
<h2>Clean up and add a map</h2>
<p>For the sake of our little demo application, we&#8217;ll make things simpler. You noticed that when you open your local appengine environment, the server greets you with a little menu &#8211; telling you which applications are available. This is nice when you are developing a few apps concurrently, but to be honest, it gets old quite quickly. Let&#8217;s remove it.<br />
Open your WEB-INF/web.xml file, delete the servlet and servlet-mapping elements. This will remove the reference to the automatically generated servlet (Duckranger_maps). Delete that servlet as well from your src folder.</p>
<p>All that you should have left now is the welcome-file-list element. Let&#8217;s change the welcome-file to something else:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>DuckMain.jsp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>This tells the appengine to find the DuckMain.jsp file and serve it when the user navigates to your root context.<br />
<b>Important: don&#8217;t forget the .jsp suffix here, otherwise you&#8217;ll get a 403 error when you try to access your application.</b>.<br />
You&#8217;ll also need to create this file, in the root of your war folder. The following jsp contains a special div with the <b>map_canvas</b> id. You will use this div to display the Google map.</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;%@ page contentType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;java&quot;</span>%&gt;</span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Mapping ducks<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://maps.google.com/maps/api/js?sensor=false&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span> //1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var map;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var initialLocation = new google.maps.LatLng(-44.6895642,169.1320571); //2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function init() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var duckOptions = { //3<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom: 12,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center: initialLocation,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapTypeId: google.maps.MapTypeId.HYBRID<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map = new google.maps.Map(document.getElementById(&quot;map_canvas&quot;), duckOptions); //4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var marker = new google.maps.Marker({ //5<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: initialLocation, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map: map<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> &nbsp;<span style="color: #000066;">onload</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;init()&quot;</span>&gt;</span> //6<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Mapping Ducks<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;map_canvas&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:100%;height:800px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span> //7<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></div></div>
<p>If you run this code on your server and browse to the application (normally <a href="http://localhost:8888">http://localhost:8888</a>) &#8211; you&#8217;ll see a huge google map centred on Wanaka, New Zealand, more specifically, on the cozy Wanaka Bakapaka.</p>
<p>Let&#8217;s go over the comments in the source above:</p>
<ol>
<li>This line tells your page to load scripts and other resources associated with Google maps (api v3). Notice the <b>?sensor=false</b> parameter. This tells the Google javascript code that your device will not provide its location. On some devices that have GPS abilities (e.g. an Android phone) &#8211; you could have this parameter set to true. In v3 of the Google maps API this parameter is mandatory.</li>
<li>As we don&#8217;t use a sensor to provide a location, we&#8217;ll supply a default starting location ourselves.<br />
The location is a <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng">google.maps.LatLng</a> object, which holds information about the latitude and longitude values of a point on the map. In the example above, the <b>initialLocation</b> object is created to point to 117 Lakeside Road, Wanaka, on New Zealand&#8217;s South island &#8211; which is the address for <a href="http://www.wanakabakpaka.co.nz/">Wanaka Bakpaka</a>.</li>
<li><b>duckOptions</b> is a javaScript variable we use to hold the map options. We define the zoom level (higher is more detailed), the map&#8217;s center location, and the type of map. Note that you <b>must</b> define the map type in v3 of the API (you didn&#8217;t have to in v2).</li>
<li>We create a new map, and tell it to load itself in the div with the <b>map_canvas</b> id. We also pass the duckOptions variable we created earlier, to tell the map how to create itself.</li>
<li>Using a <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker">google.maps.Marker</a> object, we place a default marker on Wanaka Bakapaka. Later in this tutorial we&#8217;ll see how to change the icon for the marker, add information pop-ups, and place markers ourselves.</li>
<li>This is how we tell the browser to run the init() function when it loads our web page. If we don&#8217;t specify this &#8211; our map won&#8217;t load (of course there are other ways to achieve this, e.g. jQuery&#8217;s ready() function, but this is not the case here).</li>
<li>Remember in (4) we told the map to load itself in a div with the id <b>map_canvas</b>? well &#8211; here it is.</li>
</ol>
<h2>Click to add a marker</h2>
<p>The last thing we&#8217;ll do in part 2 of the tutorial, is let the user add a marker on the map.<br />
To accomplish this, the map needs to be able to listen to mouse click events. Add the following code to the end of the init() method in DuckMain.jsp:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> map<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> initialLocation <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">44.6895642</span><span style="color: #339933;">,</span><span style="color: #CC0000;">169.1320571</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> duckOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom<span style="color: #339933;">:</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center<span style="color: #339933;">:</span> initialLocation<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mapTypeId<span style="color: #339933;">:</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">MapTypeId</span>.<span style="color: #660066;">HYBRID</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Map</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;map_canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> duckOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//4</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position<span style="color: #339933;">:</span> initialLocation<span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Code to add</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeMarker<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">latLng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></div></div>
<p>The last three lines create a listener. The listener needs three parameters: the object to add itself to (in this case, the map), the trigger to listen to (in this case, this is a mouse click), and a function to execute when the listener is triggered.<br />
The function will receive an event object, which contains the data exposed by the event.</p>
<p>In our code, we told the listener to execute the placeMarker function. We need to add it now.<br />
<b>Note: add the function outside the scope of the init() function.</b></p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> placeMarker<span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; position<span style="color: #339933;">:</span> location<span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Redeploy your application and navigate to it in the browser. The map should load as usual, centred on Wanaka Bakpaka. Now try clicking anywhere, and if you got everything correctly &#8211; you should see a new marker appear on the map.<br />
The last line in the <b>placeMarker</b> function re-centers the map on your new marker. You can comment it out if you are not interested in this behavior.<br />
<div id="attachment_698" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/maps.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/maps-300x165.png" alt="Map with two markers" title="Map with two markers" width="300" height="165" class="size-medium wp-image-698" /></a><p class="wp-caption-text">Map with two markers</p></div><br />
That&#8217;s it for part 2. In the next part we&#8217;ll see how to add save those markers to appengine&#8217;s data store.</p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=668&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-ii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google appengine and maps tutorial with Java &#8211; Part I</title>
		<link>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-1/</link>
		<comments>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-1/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 02:43:18 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[appengine]]></category>
		<category><![CDATA[Googlemaps]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=652</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/30036930@N03/3163752268/" title="03/365 Splishy splashy *Explored*" target="_blank"><img src="http://farm4.static.flickr.com/3111/3163752268_89243ae90d_m.jpg" alt="03/365 Splishy splashy *Explored*" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/30036930@N03/3163752268/" title="-mrsraggle-" target="_blank">-mrsraggle-</a></div>
</p><p>Google&#8217;s <a href="http://appengine.google.com">appengine</a> is the Google cloud offering. It is highly scalable, and provides a free entry level cloud application platform. However, it is not very trivial to develop for and deploy on.<br />
Google maps API version 3 is the current (June 2011) officially supported version of integrating with Google maps. The API allows you to display and manipulate maps in many ways.</p>
<p>The full tutorial provides an introduction to developing and deploying a simple maps application to &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/30036930@N03/3163752268/" title="03/365 Splishy splashy *Explored*" target="_blank"><img src="http://farm4.static.flickr.com/3111/3163752268_89243ae90d_m.jpg" alt="03/365 Splishy splashy *Explored*" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/30036930@N03/3163752268/" title="-mrsraggle-" target="_blank">-mrsraggle-</a></div>
<p>Google&#8217;s <a href="http://appengine.google.com">appengine</a> is the Google cloud offering. It is highly scalable, and provides a free entry level cloud application platform. However, it is not very trivial to develop for and deploy on.<br />
Google maps API version 3 is the current (June 2011) officially supported version of integrating with Google maps. The API allows you to display and manipulate maps in many ways.</p>
<p>The full tutorial provides an introduction to developing and deploying a simple maps application to the appengine using Eclipse. <a href="http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-1/">Part 1</a> walked you through setting up the development environment and testing that everything works.<br />
<span id="more-652"></span></p>
<h2>Register to appengine</h2>
<p>If you aren&#8217;t already registered to the appengine, do it before anything else. Registration is very simple &#8211; all you need is a Google (Gmail) email address and a cell phone. Log in to the appengine and follow the instructions for registration.<br />
Note that you don&#8217;t actually have to register with the appengine to develop an application. The Eclipse plugin contains a local deployment environment you can use. But, since it&#8217;s free and easy &#8211; you might as well do it now.</p>
<h2>Set up Eclipse</h2>
<p>The easiest way to develop a Java application for appengine is with Eclipse. You&#8217;ll need the right version &#8211; The <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/heliossr2">Eclipse IDE for Java EE developers</a>.<br />
Once you&#8217;ve installed it, you need to install the Google appengine plugin. Installing a new plugin is slightly different on different Eclipse releases, but if you&#8217;re using a new-ish Eclipse release (e.g. Helios) &#8211; you can do it by selecting <b>Help -> Install new software&#8230;</b> from the top menu.<br />
You should see the following dialog:<div id="attachment_653" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/addplugin.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/addplugin-300x272.png" alt="Eclipse Install new software dialog" title="Eclipse Install new software dialog" width="300" height="272" class="size-medium wp-image-653" /></a><p class="wp-caption-text">Eclipse Install new software dialog</p></div><br />
Click on the <b>Add</b> button, and enter <b>http://dl.google.com/eclipse/plugin/3.6</b> in the location field of the next dialog.<br />
The rest of the process for plugin installation is pretty straightforward. Just install everything you see, and restart Eclipse when it asks you to do that.</p>
<p>Once you&#8217;ve restarted Eclipse, you should see the appengine&#8217;s plugin buttons in your toolbar:<div id="attachment_656" class="wp-caption aligncenter" style="width: 262px"><a href="http://duckranger.com/wp-content/uploads/2011/06/pluginbuttons.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/pluginbuttons.png" alt="Appengine plugin buttons" title="Appengine plugin buttons" width="252" height="50" class="size-full wp-image-656" /></a><p class="wp-caption-text">Appengine plugin buttons</p></div></p>
<h2>Create your first appengine project</h2>
<p>To verify that everything works, we&#8217;ll first create and test a default appengine project.<br />
Create a new appengine project by selecting <b>File -> New -> Web application project</b> (The Web application project should have the little google icon next to it). Alternatively, you can create a new web project from the appengine button drop-down on the toolbar.<br />
Give your project a name and specify a package. Leave everything else to its default values, and click <b>Finish</b>.<br />
<div id="attachment_672" class="wp-caption aligncenter" style="width: 252px"><a href="http://duckranger.com/wp-content/uploads/2011/06/Untitled.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/Untitled-242x300.png" alt="Eclipse new project dialog" title="Eclipse new project dialog" width="242" height="300" class="size-medium wp-image-672" /></a><p class="wp-caption-text">Eclipse new project dialog</p></div><br />
If everything is set up correctly, you should be able to run your project. Right click your project in Eclipse&#8217;s project explorer and select <b>Run as -> Web application</b> (The web application run configuration should have the google appengine logo next to it).<br />
Eclipse will start a local appengine deployment environment and deploy your project on it. Eclipse should open the Development mode view like this:<div id="attachment_660" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/dr-sorted.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/dr-sorted-300x53.png" alt="Eclipse Development Mode view" title="Eclipse Development Mode view" width="300" height="53" class="size-medium wp-image-660" /></a><p class="wp-caption-text">Eclipse Development Mode view</p></div><br />
Double-click the url shown in the view, and your default browser should open and try to display it. You might encounter a request to download a GWT add-on:<br />
<div id="attachment_662" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/gwt-plugin.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/gwt-plugin-300x108.png" alt="Google Webkit development plugin" title="Google Webkit development plugin" width="300" height="108" class="size-medium wp-image-662" /></a><p class="wp-caption-text">Google Webkit development plugin</p></div>.</p>
<p>Once you&#8217;ve sorted all that, you should see the following page (well, unless Google changes the plugin&#8217;s default generated application code).<br />
<div id="attachment_664" class="wp-caption aligncenter" style="width: 310px"><a href="http://duckranger.com/wp-content/uploads/2011/06/allgood.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/allgood-300x138.png" alt="Web application success" title="Web application success" width="300" height="138" class="size-medium wp-image-664" /></a><p class="wp-caption-text">Web application successful deployment</p></div></p>
<p>Try and enter your name and click on <b>Send</b>. If everything is alright &#8211; you should receive some notification from the server telling you which browser it thinks you are using.</p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=652&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/06/google-appengine-and-maps-tutorial-with-java-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android login with actionGo</title>
		<link>http://duckranger.com/2011/06/android-login-with-actiongo/</link>
		<comments>http://duckranger.com/2011/06/android-login-with-actiongo/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 01:35:25 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[actionGo]]></category>
		<category><![CDATA[actionNext]]></category>
		<category><![CDATA[login]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=643</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/23322134@N02/2631659122/" title="Disney - Three Caballero Donald Duck (Explored)" target="_blank"><img src="http://farm4.static.flickr.com/3066/2631659122_41b13d0a03_m.jpg" alt="Disney - Three Caballero Donald Duck (Explored)" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/23322134@N02/2631659122/" title="Express Monorail" target="_blank">Express Monorail</a></div>
</p><p>Coding a login screen is pretty simple in Android, and there&#8217;s quite extensive documentation on how to do screen development. However, there&#8217;s one neat little trick I wanted to use: the &#8216;next&#8217; and &#8216;go&#8217; buttons on the soft keyboard. These buttons are actions that let you move automatically from the login text input to the password text input, and then from the password text input, you can &#8216;click the login button&#8217;.<br />
This is documented, but it &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/23322134@N02/2631659122/" title="Disney - Three Caballero Donald Duck (Explored)" target="_blank"><img src="http://farm4.static.flickr.com/3066/2631659122_41b13d0a03_m.jpg" alt="Disney - Three Caballero Donald Duck (Explored)" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/23322134@N02/2631659122/" title="Express Monorail" target="_blank">Express Monorail</a></div>
<p>Coding a login screen is pretty simple in Android, and there&#8217;s quite extensive documentation on how to do screen development. However, there&#8217;s one neat little trick I wanted to use: the &#8216;next&#8217; and &#8216;go&#8217; buttons on the soft keyboard. These buttons are actions that let you move automatically from the login text input to the password text input, and then from the password text input, you can &#8216;click the login button&#8217;.<br />
This is documented, but it isn&#8217;t very easy to find how to implement, so I thought I&#8217;d write it down.</p>
<p>The following shows what I mean. On the left, you see the soft keyboard with the <strong>Next</strong> button. Clicking it will move the cursor and focus to the password field.<br />
on the right, the screen focus is in the password screen. Clicking on the soft keyboard&#8217;s <strong>Go</strong> button will mimic the action of the user clicking on the login button.<br />
<a href="http://duckranger.com/wp-content/uploads/2011/06/duck_login_go.png"><img src="http://duckranger.com/wp-content/uploads/2011/06/duck_login_go.png" alt="" title="Login screens" width="687" height="424" class="aligncenter size-full wp-image-644" /></a></p>
<h3>The login screen definition</h2>
<p>This is the loginscreen.xml file I use under /layout</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TableLayout</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span> <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:stretchColumns</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> <span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;20sp&quot;</span> <span style="color: #000066;">android:layout_margin</span>=<span style="color: #ff0000;">&quot;10dp&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span> <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;26dip&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:gravity</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/usernameText&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:textColor</span>=<span style="color: #ff0000;">&quot;#4FAF20&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EditText</span> <span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;20sp&quot;</span> <span style="color: #000066;">android:layout_margin</span>=<span style="color: #ff0000;">&quot;10dp&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/usernameText&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:gravity</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">android:singleLine</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;26dip&quot;</span> <span style="color: #000066;">android:textColor</span>=<span style="color: #ff0000;">&quot;@android:color/white&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;#4FAF20&quot;</span> <span style="color: #000066;">android:imeOptions</span>=<span style="color: #ff0000;">&quot;actionNext&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/EditText<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span> <span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;20sp&quot;</span> <span style="color: #000066;">android:layout_margin</span>=<span style="color: #ff0000;">&quot;10dp&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span> <span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;26dip&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:gravity</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/passwordText&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:textColor</span>=<span style="color: #ff0000;">&quot;#4FAF20&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EditText</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;26sp&quot;</span> <span style="color: #000066;">android:layout_weight</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;20sp&quot;</span> <span style="color: #000066;">android:layout_margin</span>=<span style="color: #ff0000;">&quot;10dp&quot;</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/passwordText&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:gravity</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">android:textColor</span>=<span style="color: #ff0000;">&quot;@android:color/white&quot;</span> <span style="color: #000066;">android:singleLine</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:background</span>=<span style="color: #ff0000;">&quot;#4FAF20&quot;</span> <span style="color: #000066;">android:password</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">android:imeOptions</span>=<span style="color: #ff0000;">&quot;actionGo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextView</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">android:layout_width</span>=<span style="color: #ff0000;">&quot;match_parent&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:layout_height</span>=<span style="color: #ff0000;">&quot;wrap_content&quot;</span> <span style="color: #000066;">android:text</span>=<span style="color: #ff0000;">&quot;@string/buttonLogin&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">android:textSize</span>=<span style="color: #ff0000;">&quot;20sp&quot;</span> <span style="color: #000066;">android:id</span>=<span style="color: #ff0000;">&quot;@+id/buttonLogin&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TableRow<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TableLayout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>The two important things to notice are the <strong>android:imeOptions</strong> declarations on the user name and password fields.</p>
<p>On the user name EditText element, I define <strong>android:imeOptions=&#8221;actionNext&#8221;</strong>. This tells the keyboard that it should show a Next button, which then knows to move on to the next input field on the screen (namely, the password EditText).</p>
<p>On the password EditText element, I define <strong>android:imeOptions=&#8221;actionGo&#8221;</strong>. This instructs the keyboard to show a Go button.</p>
<p>The first IME option declaration works without further tinkering. However, the actionGo does not automatically knows what to do (It really only fires an &#8216;Enter&#8217; KeyEvent).</p>
<p>There are two things you need to do in order for the Go action to &#8216;click the login button&#8217;.</p>
<p>The first thing is that the password EditText element needs to be configured as a single-line input. Otherwise, the &#8216;Enter&#8217; key event will behave as a new-line. This is configured in the xml above with the <strong>android:singleLine=&#8221;true&#8221;</strong> attribute.</p>
<p>The second thing to do is to tell the LoginScreen Activity what it needs to do when the user clicked the Go button on the soft keyboard. This we do in code:</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//LoginScreenActivity.java</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.KeyEvent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnKeyListener</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.EditText</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginScreenActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> OnClickListener <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;LoginScreenActivity&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; EditText usernameText<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; EditText passwordText<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">Button</span> loginButton<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">loginscreen</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; usernameText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">usernameText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; passwordText <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>EditText<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">passwordText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loginButton &nbsp;<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> &nbsp; findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">buttonLogin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loginButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//This is the relevant code</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; passwordText.<span style="color: #006633;">setOnKeyListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnKeyListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onKey<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> view, <span style="color: #000066; font-weight: bold;">int</span> keyCode, <span style="color: #003399;">KeyEvent</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>keyCode <span style="color: #339933;">==</span> <span style="color: #003399;">KeyEvent</span>.<span style="color: #006633;">KEYCODE_ENTER</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick<span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> view<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;login onClick&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//note: this login verification needs to be separated to another thread.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> LoginService<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>usernameText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,passwordText.<span style="color: #006633;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onSuccessfulLogin<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Intent accountsIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>getBaseContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, AccountList.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult<span style="color: #009900;">&#40;</span>accountsIntent, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Everything here is pretty normal for an activity.<br />
Note that it is not best-practice-ish to have the activity implement the OnClickListener interface, but for brevity sake &#8211; I did it here, and I attach the activity itself as the listener to the login button&#8217;s onclick.<br />
When the login button is clicked, the activity creates a login service and calls its execute() method to test the login credentials supplied.</p>
<p>The important piece of code is the the line that sets an onClickListener on the passwordText element.<br />
Inside the listener&#8217;s onKey method, we test for the key the user pressed. Remember that the <strong>Go</strong> button generates an Enter keypress. This corresponds to the <strong>KeyEvent.KEYCODE_ENTER</strong> constant. </p>
<p>When the listener identifies that the Enter key was pressed, it fires the activity&#8217;s onClick() method &#8211; as if the login button itself was clicked.</p>
<p>That&#8217;s it, really.</p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=643&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/06/android-login-with-actiongo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JAXB without a schema</title>
		<link>http://duckranger.com/2011/06/jaxb-without-a-schema/</link>
		<comments>http://duckranger.com/2011/06/jaxb-without-a-schema/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 04:34:04 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JAXB]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=626</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/12187843@N07/3605272991/" title="~ Duck Talk ~" target="_blank"><img src="http://farm4.static.flickr.com/3649/3605272991_21387647ee_m.jpg" alt="~ Duck Talk ~" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/12187843@N07/3605272991/" title="ViaMoi" target="_blank">ViaMoi</a></div>
</p><p>Working on a new project, I had a few chats to people and realized that many developers assume that to work with JAXB you need to have an XML schema.<br />
This is not true. If you have the schema, go ahead and use it, but if you only have the XML files you want to parse, JAXB is perfectly capable of unmarshalling your XML into objects without the schema and the binding.<br />
The only problem is &#8211; &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/12187843@N07/3605272991/" title="~ Duck Talk ~" target="_blank"><img src="http://farm4.static.flickr.com/3649/3605272991_21387647ee_m.jpg" alt="~ Duck Talk ~" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/12187843@N07/3605272991/" title="ViaMoi" target="_blank">ViaMoi</a></div>
<p>Working on a new project, I had a few chats to people and realized that many developers assume that to work with JAXB you need to have an XML schema.<br />
This is not true. If you have the schema, go ahead and use it, but if you only have the XML files you want to parse, JAXB is perfectly capable of unmarshalling your XML into objects without the schema and the binding.<br />
The only problem is &#8211; you&#8217;ll have to code the binding classes yourself. However &#8211; this isn&#8217;t always a bad thing. Coding the classes yourself lets you take some good shortcuts, disregard elements you are not interested in, and control behavior better.<br />
I would like to add that you usually end up manually editing the generated classes anyway &#8211; so if your XML files are relatively simple &#8211; might as well do it the old fashioned way.<br />
<span id="more-626"></span></p>
<h2>An example</h2>
<p>Let&#8217;s consider the following XML document:</p>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;user<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Duck Ranger<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;accountNumber<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/accountNumber<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>duckr<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/username<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;COMMAND<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>list<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/COMMAND<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;superUser<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>false<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/superUser<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PROGRAMS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;program<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>processor.exe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/products/processor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/program<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;program<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>preprocessor.exe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/products/preprocessor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/program<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PROGRAMS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lastLogin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>21-10-2010<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lastLogin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/user<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<p>A very simple XML file. right?<br />
To parse it using JAXB, we need to tell the unmarshaller which classes are present. The rule of thumb is simple: If an element contains other elements &#8211; it is a class. However, if it is a leaf-element &#8211; it will be a member of the containing class.</p>
<p>Following this rule, it is obvious that <strong>User</strong> is a class. It contains everything else. However, <strong>name</strong> is not a class &#8211; it will be a member of the <strong>User</strong> class. </p>
<p>So how will it look like in code?</p>
<h3>Program.java</h3>
<p>The inner-most class describes the program element.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.jaxb</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlElement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.adapters.CollapsedStringAdapter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//The XMLRootElement annotation tells JAXB what is the </span><br />
<span style="color: #666666; font-style: italic;">//element local name it needs to look for.</span><br />
@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;program&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Program <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> executable<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> directory<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//The adapter tells JAXB to collapse white space. This is usually </span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//quite good to use for Strings.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//The XmlElement annotation tells JAXB what's the </span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//actual name of the element inside &lt;program&gt;</span><br />
&nbsp; &nbsp; @XmlJavaTypeAdapter<span style="color: #009900;">&#40;</span>CollapsedStringAdapter.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;executable&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getExecutable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> executable<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setExecutable<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> executable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">executable</span> <span style="color: #339933;">=</span> executable<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; @XmlJavaTypeAdapter<span style="color: #009900;">&#40;</span>CollapsedStringAdapter.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;directory&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDirectory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> directory<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDirectory<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> directory<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">directory</span> <span style="color: #339933;">=</span> directory<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Program [&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>executable <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;executable=&quot;</span> <span style="color: #339933;">+</span> executable <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>directory <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;directory=&quot;</span> <span style="color: #339933;">+</span> directory <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>There&#8217;s really not much to this class. It is the inner-most level we are getting to inside the sample XML document. Let&#8217;s move on to the next class:</p>
<h3>Programs.java</h3>
<p>Programs is just a container class for Program elements.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.jaxb</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlElement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">//Note that the element is all up-case. The Java class does not</span><br />
<span style="color: #666666; font-style: italic;">//have to have exactly the same name as the element. It is</span><br />
<span style="color: #666666; font-style: italic;">//enough that you create the right mapping. JAXB automatic</span><br />
<span style="color: #666666; font-style: italic;">//bindings will not do that for you.</span><br />
@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PROGRAMS&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Programs <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Program<span style="color: #339933;">&gt;</span> programs<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;program&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Program<span style="color: #339933;">&gt;</span> getPrograms<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">==</span>programs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; programs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Program<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> programs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPrograms<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Program<span style="color: #339933;">&gt;</span> programs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">programs</span> <span style="color: #339933;">=</span> programs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Programs [&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>programs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;programs=&quot;</span> <span style="color: #339933;">+</span> programs <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>The Programs.java class is a container &#8211; it holds a list of Program objects. This is not absolutely necessary (You could have the User class hold the list of Program objects directly) &#8211; but it is clearer this way.<br />
Note that in the getter annotation I use the name &#8220;program&#8221;. This tells JAXB that the list used here maps to elements<br />
with the name &#8220;program&#8221;.</p>
<h3>User.java</h3>
<p>The User class contains the attributes that are leafs inside the xml file. It also contains a reference to a Programs object, which provides a list of programs.</p>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.jaxb</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlElement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.adapters.CollapsedStringAdapter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter</span><span style="color: #339933;">;</span><br />
<br />
@XmlRootElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> accountNumber<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> userName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> command<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Boolean</span> superUser<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> lastLogin<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> Programs programs<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @XmlJavaTypeAdapter<span style="color: #009900;">&#40;</span>CollapsedStringAdapter.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;accountNumber&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getAccountNumber<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> accountNumber<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAccountNumber<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> accountNumber<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">accountNumber</span> <span style="color: #339933;">=</span> accountNumber<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @XmlJavaTypeAdapter<span style="color: #009900;">&#40;</span>CollapsedStringAdapter.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getUserName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> userName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUserName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userName</span> <span style="color: #339933;">=</span> userName<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//As you can see, the member name does not have to correspond</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//to the actual element name, as long as you have the mapping</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//sorted.</span><br />
&nbsp; &nbsp; @XmlJavaTypeAdapter<span style="color: #009900;">&#40;</span>CollapsedStringAdapter.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;COMMAND&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> command<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCommand<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> command<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">command</span> <span style="color: #339933;">=</span> command<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;superUser&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> getSuperUser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> superUser<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSuperUser<span style="color: #009900;">&#40;</span><span style="color: #003399;">Boolean</span> superUser<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">superUser</span> <span style="color: #339933;">=</span> superUser<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lastLogin&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Date</span> getLastLogin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> lastLogin<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastLogin<span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span> lastLogin<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastLogin</span> <span style="color: #339933;">=</span> lastLogin<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PROGRAMS&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> Programs getPrograms<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> programs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPrograms<span style="color: #009900;">&#40;</span>Programs programs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">programs</span> <span style="color: #339933;">=</span> programs<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;User [&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>name <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;name=&quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>accountNumber <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;accountNumber=&quot;</span> <span style="color: #339933;">+</span> accountNumber<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>userName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;userName=&quot;</span> <span style="color: #339933;">+</span> userName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>command <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;command=&quot;</span> <span style="color: #339933;">+</span> command <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>superUser <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;superUser=&quot;</span> <span style="color: #339933;">+</span> superUser <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>lastLogin <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;lastLogin=&quot;</span> <span style="color: #339933;">+</span> lastLogin <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>programs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;programs=&quot;</span> <span style="color: #339933;">+</span> programs <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></div>
<p>With all the classes set, all that&#8217;s left is to create a driver &#8211; something to unmarshall the XML file:</p>
<h3>Unmarshal.java</h3>
<div class="codecolorer-container java mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.duckranger.jaxb</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileReader</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.JAXBContext</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.Unmarshaller</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Unmarshal <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; JAXBContext context <span style="color: #339933;">=</span> JAXBContext.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span>User.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Unmarshaller unmarshaller <span style="color: #339933;">=</span> context.<span style="color: #006633;">createUnmarshaller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; User user <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>User<span style="color: #009900;">&#41;</span>unmarshaller.<span style="color: #006633;">unmarshal</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./duckranger.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In the simple code above, there are 3 points to notice:</p>
<ol>
<li>The first thing you need to do is create a JAXB context. The simplest way to create it is using the base class for the XML file (as shown above). You can also create it more generic by pointing it to the package.</li>
<li>I then create a <strong>User</strong> object from the file.</li>
<li>Using the sophisticated toString() methods on all classes shown above, the final line in the main method will print the following (for the XML file provided) : </li>
</ol>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">User [name=Duck Ranger, accountNumber=45, userName=duckr, command=list, superUser=false, programs=Programs [programs=[Program [executable=preprocessor.exe, directory=/products/preprocessor], Program [executable=processor.exe, directory=/products/processor]]]]</div></div>
<img src="http://duckranger.com/?ak_action=api_record_view&id=626&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/06/jaxb-without-a-schema/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CakePHP: Keeping login information on error pages</title>
		<link>http://duckranger.com/2011/05/cakephp-keeping-login-information-on-error-pages/</link>
		<comments>http://duckranger.com/2011/05/cakephp-keeping-login-information-on-error-pages/#comments</comments>
		<pubDate>Tue, 03 May 2011 10:41:17 +0000</pubDate>
		<dc:creator>Duck Ranger</dc:creator>
				<category><![CDATA[Be nice to your users]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://duckranger.com/?p=609</guid>
		<description><![CDATA[<p><a href="http://www.flickr.com/photos/66621443@N00/367930471/" title="make way for ducklings" target="_blank"><img src="http://farm1.static.flickr.com/150/367930471_2484c60420_m.jpg" alt="make way for ducklings" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/66621443@N00/367930471/" title="shoothead" target="_blank">shoothead</a></div>
</p><p>This <a href="http://nuts-and-bolts-of-cakephp.com/2008/08/29/dealing-with-errors-in-cakephp/">teknoid blog post</a> describes an excellent method of refining Cake&#8217;s default error handling.<br />
Quick reminder: by default, if your debug level is set to 0, cake will always redirect your user to the error404.ctp under app/views/errors).<br />
<span id="more-609"></span><br />
<b>Edit Jan 5, 2011: The information in this post is for Cake 1.2/1.3. See COTP&#8217;s comment below if you&#8217;re using Cake 2.0 or newer.</b><br />
I recently had an issue with the 404 error page in an application I&#8217;m coding.<br />
The &#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/66621443@N00/367930471/" title="make way for ducklings" target="_blank"><img src="http://farm1.static.flickr.com/150/367930471_2484c60420_m.jpg" alt="make way for ducklings" border="0" class="alignleft"/></a>
<div style="width:260px;float:left;clear:both;font-size:9px !important"><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://duckranger.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" class="alignleft"/></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/66621443@N00/367930471/" title="shoothead" target="_blank">shoothead</a></div>
<p>This <a href="http://nuts-and-bolts-of-cakephp.com/2008/08/29/dealing-with-errors-in-cakephp/">teknoid blog post</a> describes an excellent method of refining Cake&#8217;s default error handling.<br />
Quick reminder: by default, if your debug level is set to 0, cake will always redirect your user to the error404.ctp under app/views/errors).<br />
<span id="more-609"></span><br />
<b>Edit Jan 5, 2011: The information in this post is for Cake 1.2/1.3. See COTP&#8217;s comment below if you&#8217;re using Cake 2.0 or newer.</b><br/><br />
I recently had an issue with the 404 error page in an application I&#8217;m coding.<br />
The application let&#8217;s a user log in to the system, and after a user logs in, there&#8217;s a box at the top right of the screen that displays the user&#8217;s name like this:<br />
<div id="attachment_610" class="wp-caption aligncenter" style="width: 257px"><a href="http://duckranger.com/wp-content/uploads/2011/05/header.png"><img src="http://duckranger.com/wp-content/uploads/2011/05/header.png" alt="My application&#039;s login information box" title="My application&#039;s login information box" width="247" height="44" class="size-full wp-image-610" /></a><p class="wp-caption-text">My application&#039;s login information box</p></div></p>
<p>When I go to a non-existing URL, CakePHP redirects me to the error404.ctp page, as expected. However, as none of my controllers is involved (and especially not my app_controller which sets the logged in user name for default.ctp) &#8211; the login information does not exist, and my 404 page looks like this:<div id="attachment_612" class="wp-caption aligncenter" style="width: 299px"><a href="http://duckranger.com/wp-content/uploads/2011/05/header2.png"><img src="http://duckranger.com/wp-content/uploads/2011/05/header2.png" alt="My application with no login information" title="My application with no login information" width="289" height="93" class="size-full wp-image-612" /></a><p class="wp-caption-text">My application with no login information</p></div></p>
<p>This saddened me to no end. Actually, it saddened my Business Analyst, and I do not like it when he&#8217;s unhappy. I therefore read and re-read the aforementioned <a href="http://nuts-and-bolts-of-cakephp.com/2008/08/29/dealing-with-errors-in-cakephp/">teknoid&#8217;s post</a>, until I found a silly hack that does the trick:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">//File: app/app_error.php<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> AppError <span style="color: #000000; font-weight: bold;">extends</span> ErrorHandler <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$messages</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> _outputMessage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/error404'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">controller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">afterFilter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #339933;">...</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>All app_error.php does is take control of error handling from Cake&#8217;s default. All I do in it is redirect the incoming request to the /error404 path.<br />
In routes.php, I connect /error404 to a specific controller/action pair:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//file: app/config/routes.php</span><br />
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/error404'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bulls'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And in bulls_controller.php &#8211; just add an empty error function:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//file: app/controllers/bulls_controller.php</span><br />
<span style="color: #000000; font-weight: bold;">function</span> error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></div></div>
<p>Last &#8211; add the error.ctp view:</p>
<div class="codecolorer-container php mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//file: app/views/bulls/error.ctp</span><br />
<span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">404</span><span style="color: #339933;">.</span> Oops<span style="color: #339933;">.</span> Dais<span style="color: #339933;">.&lt;/</span>h1<span style="color: #339933;">&gt;</span></div></div>
<p>And that&#8217;s it! Now, when the user enters a non-existing URL, Cake is going to redirect it to the bulls controller. Because of this, I regain the user&#8217;s context in my error page!</p>
<p>(Note, there&#8217;s probably an easier way to do it, but the point is, you can use this technique whenever you set up some data for your default template that you wish was available for your error page as well). </p>
<img src="http://duckranger.com/?ak_action=api_record_view&id=609&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://duckranger.com/2011/05/cakephp-keeping-login-information-on-error-pages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

