<?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>PermGen error</title>
	<atom:link href="http://eggsylife.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://eggsylife.co.uk</link>
	<description>Eggsylife</description>
	<lastBuildDate>Thu, 22 Apr 2010 20:45:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GWT 2, JSONP and Javascript Overlays with JsonpRequestBuilder</title>
		<link>http://eggsylife.co.uk/2010/04/22/gwt-2-jsonp-and-javascript-overlays-with-jsonprequestbuilder/</link>
		<comments>http://eggsylife.co.uk/2010/04/22/gwt-2-jsonp-and-javascript-overlays-with-jsonprequestbuilder/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 12:24:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Google Web Toolkit]]></category>
		<category><![CDATA[cross site requests]]></category>
		<category><![CDATA[gwt 2.0]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[jsonprequestbuilder]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=249</guid>
		<description><![CDATA[In one of my earlier posts I discussed the topic of making cross site requests with Google Web toolkit.
Since that post Google have updated Web toolkit, currently version 2.0.3 and has now made it easier to make cross site requests through the use of their new JsonpRequestBuilder object and the Javascript object overlay types for [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my earlier posts I <a href="http://eggsylife.co.uk/2008/10/10/gwt-and-cross-site-jsonp-in-j2ee-container/">discussed the topic of making cross site requests</a> with <a href="http://eggsylife.co.uk/category/gwt/">Google Web toolkit</a>.</p>
<p>Since that post Google have updated Web toolkit, currently version 2.0.3 and has now made it easier to make cross site requests through the use of their new <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/jsonp/client/JsonpRequestBuilder.html" target="_blank">JsonpRequestBuilder</a> object and the Javascript object overlay types for result encapsulation.</p>
<p>This is a quick tutorial update on how to make use of the object overlay types. The application contacts the <a href="http://www.geonames.org/export/web-services.html" target="_blank">Geonames webservice</a>. For the example I shall use the &#8216;Placename lookup with postalcode (JSON)&#8217; function by making the following call:</p>
<p><a href="http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&#038;country=GB&#038;maxRows=4" target="_blank">http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&#038;country=GB&#038;maxRows=4</a></p>
<p>This returns location details for places matching the <i>M1</i> post code so central Manchester in the United Kingdom.</p>
<p>The results this request return are as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;postalcodes&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;adminCode3&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;BN&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminName3&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Manchester District (B)&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminCode2&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;postalcode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;M1 1AD&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminCode1&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;ENG&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;countryCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;GB&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;lng&quot;</span><span style="color: #339933;">:-</span><span style="color: #CC0000;">2.24521599415295</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;placeName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;City Centre Ward&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;lat&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">53.4838491117407</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminName1&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;England&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;adminCode3&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;BN&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminName3&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Manchester District (B)&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminCode2&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;00&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;postalcode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;M1 1AE&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminCode1&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;ENG&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;countryCode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;GB&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;lng&quot;</span><span style="color: #339933;">:-</span><span style="color: #CC0000;">2.23116928236428</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;placeName&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Ancoats and Clayton Ward&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;lat&quot;</span><span style="color: #339933;">:</span><span style="color: #CC0000;">53.4834816441844</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;adminName1&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;England&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you look at the previous JSON data it seems we have a list of &#8216;<em>postalcodes</em>&#8216; and each postal code has various fields such as adminName3, placeName etc.</p>
<p>Using GWT Javascript object overlays we can represent this structure. Within your GWT client directory create a new class called <strong>PostcodeEntry</strong> and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.JavaScriptObject</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PostcodeEntry <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> PostcodeEntry<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getAdminCode3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		return this.adminCode3;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">void</span> setNumberPlaceHolderId<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> adminCode3<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		this.adminCode3 = adminCode3;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getAdminName3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		return this.adminName3;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">void</span> setAdminName3<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> adminName3<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		this.adminName3 = adminName3;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getPostalcode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		return this.postalcode;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">void</span> setPostalcode<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> postalcode<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		this.postalcode = postalcode;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getPlaceName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		return this.placeName;
	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">void</span> setPlaceName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> placeName<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
		this.placeName = placeName;
	}-*/</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Each PostcodeEntry represents one postalcode as you can see we have the functions for the various properties of a postalcode. Also note we extend the GWT <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JavaScriptObject.html" target="_blank">JavascriptObject</a>. In the above example you&#8217;ll notice I haven&#8217;t implemented every property. This is by choice I have only implemented a few of the properties.</p>
<p>Now we need to build an object that maintains the collection of PostcodeEntry object. Essentially the object that represents the postalcodes array. Create a new Java object called &#8216;<strong>PostalCodes</strong>&#8216;. Add the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.JavaScriptObject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.JsArray</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PostalCodes <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> PostalCodes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">native</span> JsArray<span style="color: #339933;">&lt;</span>PostcodeEntry<span style="color: #339933;">&gt;</span> getPostalCodes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    	return this.postalcodes;
  	}-*/</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The object quite simply has one method called getPostalCodes which you&#8217;ll notice is a collection of PostcodeEntry objects and the native method simply called this.postalcodes which if you remember was the name of the array in the JSON response.</p>
<p>Those two objects wrap up the Geonames JSON response.</p>
<p>Finally we just need to write the code that invokes the service using the <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/jsonp/client/JsonpRequestBuilder.html" target="_blank">JsonpRequestBuilder</a> object.</p>
<p>Locate your GWT entry point and add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.core.client.EntryPoint</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.jsonp.client.JsonpRequestBuilder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.Window</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.rpc.AsyncCallback</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.HorizontalPanel</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.Label</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.RootPanel</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.VerticalPanel</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Entry point classes define &lt;code&gt;onModuleLoad()&lt;/code&gt;.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JsonpExample <span style="color: #000000; font-weight: bold;">implements</span> EntryPoint <span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * This is the entry point method.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onModuleLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #666666; font-style: italic;">// http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&amp;country=GB&amp;maxRows=4</span>
	<span style="color: #666666; font-style: italic;">//</span>
	JsonpRequestBuilder jsonp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonpRequestBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&amp;country=GB&amp;maxRows=4&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	jsonp.<span style="color: #006633;">requestObject</span><span style="color: #009900;">&#40;</span>url, <span style="color: #000000; font-weight: bold;">new</span> AsyncCallback<span style="color: #339933;">&lt;</span>PostalCodes<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    @Override
	    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFailure<span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> caught<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failure:&quot;</span> <span style="color: #339933;">+</span> caught.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            @Override
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSuccess<span style="color: #009900;">&#40;</span>PostalCodes result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                VerticalPanel vp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> VerticalPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getPostalCodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> count <span style="color: #339933;">&lt;</span> result.<span style="color: #006633;">getPostalCodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> count<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			PostcodeEntry entry <span style="color: #339933;">=</span> result.<span style="color: #006633;">getPostalCodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			HorizontalPanel hp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HorizontalPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hp.<span style="color: #006633;">setSpacing</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hp.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Label</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #006633;">getPostalcode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			hp.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Label</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #006633;">getPlaceName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			vp.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>hp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>
               RootPanel.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>vp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
       <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are few important areas of this code to take note.</p>
<p>First of all note the use of the JsonpRequestBuilder object. We simply give this a Url which in this case is the Geoname service url. Those who are familiar with cross site requests will notice that the URL I specified doesn&#8217;t contain the &#8216;callback&#8217; parameter. The JsonpRequestBuilder automatically appends this to the end of the URL for us so we have &#038;callback=gwt_IOSucess appended to the request. It defaults the parameter name to &#8216;callback&#8217; but if the service requires a different parameter name you can change this by calling <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/jsonp/client/JsonpRequestBuilder.html#setCallbackParam%28java.lang.String%29" target="_blank">setCallbackParam</a>.</p>
<p>Next we simply call requestObject that takes an Async callback which is of our Postalcodes type. This clears up why we use object overlays.</p>
<p>Finally the onSuccess method takes in the Postalcodes as an argument and we are then free to pick away at the response and the various postalcode entries in the result.</p>
<p>I hope this has helped people to start making use of the new GWT JSONP functions and object overlays. </p>
<p>You can also <a href="http://eggsylife.co.uk/JsonpExample.zip">download the source code</a> for this example.</p>
<p>Please any comments welcome&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/04/22/gwt-2-jsonp-and-javascript-overlays-with-jsonprequestbuilder/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spring annotation based AOP and intercepting the ball</title>
		<link>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/</link>
		<comments>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 10:13:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[@annotation]]></category>
		<category><![CDATA[@Around]]></category>
		<category><![CDATA[@Aspect]]></category>
		<category><![CDATA[AspectJ]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring aop]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=236</guid>
		<description><![CDATA[Spring AOP and Annotation based AspectJ
The concept
Recently I had to work on a requirement that asked that logs be kept when anyone performs certain actions upon the database.
For example if some chose to remove an item from the database &#8211; the action, who performed it, when it was performed and any extra information should be [...]]]></description>
			<content:encoded><![CDATA[<h2>Spring AOP and Annotation based AspectJ</h2>
<h3>The concept</h3>
<p>Recently I had to work on a requirement that asked that logs be kept when anyone performs certain actions upon the database.</p>
<p>For example if some chose to remove an item from the database &#8211; the action, who performed it, when it was performed and any extra information should be noted in the database.</p>
<p>Implementing this can be performed in a variety of ways. We could have simply told developers to call a function within any database methods to log the action. However Spring AOP provides a better way of doing this.</p>
<h3>The idea</h3>
<p>To make it easier to store the logs I wanted users to simply be able to annotate any methods they felt needed logging in the message.</p>
<p>Then using <a href="http://www.eclipse.org/aspectj/" target="_blank">AspectJ</a> we would have code that would intercept the methods and log the action in the database. All developers would need to do is very simply annotate their methods.</p>
<h3>The Annotation</h3>
<p>First of all I had to create the <a href="http://www.developer.com/article.php/3556176" target="_blank">Java Method annotation</a> that can be placed above the required methods.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.ElementType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Retention</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.RetentionPolicy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.annotation.Target</span><span style="color: #339933;">;</span>
&nbsp;
@Retention<span style="color: #009900;">&#40;</span>RetentionPolicy.<span style="color: #006633;">RUNTIME</span><span style="color: #009900;">&#41;</span>
@Target<span style="color: #009900;">&#40;</span>ElementType.<span style="color: #006633;">METHOD</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> LogAction <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> actionPerformed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The annotation simply has one parameter that allows the developer to specify the action performed such as &#8216;object deleted&#8217; etc.</p>
<h3>The aspect class</h3>
<p>Next was to write the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-ataspectj" target="_blank">Spring aspect class</a> which will perform the actual logging of the action</p>
<p>First of all we write a simple class and add the @Aspect annotation before the class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAspect</pre></div></div>

<p>Next we need to define the Pointcut. As a Spring defintion a Pointcut is:</p>
<p>Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.</p>
<p>I&#8217;m writing a simple point cut that matches any public methods. This is coded like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;execution(public * *(..))&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> anyPublicMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The last part of the Aspect class is the Advice. Spring defines the advice as:</p>
<p>Advice: action taken by an aspect at a particular join point. Different types of advice include &#8220;around,&#8221; &#8220;before&#8221; and &#8220;after&#8221; advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.</p>
<p>I need my advice to handle the &#8220;around&#8221; advice and only methods with my @LogAction annotation</p>
<p>To do this I used the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anyPublicMethod() &amp;&amp; @annotation(logAction)&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> logAction<span style="color: #009900;">&#40;</span>ProceedingJoinPoint pjp, LogAction logAction<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The method uses my anyPublicMethod Pointcut and the @annotation(logAction) to match only public methods with the LogAction annotation specified.</p>
<p>I have also included the ProceedingJoinPoint parameter so I can access the various arguments in the annotated method.</p>
<p>The final Aspect class is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.ProceedingJoinPoint</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Around</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Aspect</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.aspectj.lang.annotation.Pointcut</span><span style="color: #339933;">;</span>
&nbsp;
@Aspect
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAspect<span style="color: #009900;">&#123;</span>
&nbsp;
	@Pointcut<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;execution(public * *(..))&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> anyPublicMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anyPublicMethod() &amp;&amp; @annotation(logAction)&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> logAction<span style="color: #009900;">&#40;</span>ProceedingJoinPoint pjp, LogAction logAction<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Do what you want with the actionperformed</span>
		<span style="color: #003399;">String</span> actionPerformed <span style="color: #339933;">=</span> logAction.<span style="color: #006633;">actionPerformed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Do what you want with the join point arguments</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Object</span> object <span style="color: #339933;">:</span> pjp.<span style="color: #006633;">getArgs</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>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> pjp.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Updating the Spring app config</h3>
<p>To ensure that our @Aspect annotated class is picked up by Spring you simply need to add the following to your app-config.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;aop:aspectj-autoproxy</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myAspect&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;package.to.my.MyAspect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This assumes that you have the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aspectj-support" target="_blank">aop namespace</a> within your app-config beans definition like so</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span>
<span style="color: #009900;">                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
.....
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Setting up intercepted methods</h3>
<p>The final step is to mark the methods we want to be intercepted</p>
<p>This is as easy as doing the following</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@LogAction<span style="color: #009900;">&#40;</span>actionPerformed<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;doSomething&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doSomething<span style="color: #009900;">&#40;</span>MyObject someParameter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now whenever that method is executed we will firstly execute our Aspect code.</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring JS &#8211; Lack of an API or Documentation</title>
		<link>http://eggsylife.co.uk/2010/01/22/spring-js-lack-of-an-api-or-documentation/</link>
		<comments>http://eggsylife.co.uk/2010/01/22/spring-js-lack-of-an-api-or-documentation/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 13:42:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[AjaxEventDecoration]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring js]]></category>
		<category><![CDATA[spring web flow]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=232</guid>
		<description><![CDATA[Hi all,
This is a bit of a call out to the readers of the blog. In particular any Spring developers.
Recently I have been attempting to use Spring Web Flow and in particular it&#8217;s Spring JS implementation to Ajaxify applications I am working on.
However the documentation on Spring 3 and its Javascript support is quite lacking. [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all,</p>
<p>This is a bit of a call out to the readers of the blog. In particular any Spring developers.</p>
<p>Recently I have been attempting to use Spring Web Flow and in particular it&#8217;s Spring JS implementation to Ajaxify applications I am working on.</p>
<p>However the documentation on Spring 3 and its Javascript support is quite lacking. In fact it seems to only consist of:</p>
<p><a href="http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch11s04.html" target="_blank">http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch11s04.html</a></p>
<p>Literally that is all the &#8216;official&#8217; documentation I can find the rest seems to be forum posts by the Spring community.</p>
<p>Can anyone share any experience? Or give any feedback on the framework? Could we (as a community) look to help the development of this documentation?</p>
<p>By the way this shouldn&#8217;t put anyone off using Spring for the web application development. As a whole the framework is very useful and effective!!</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/01/22/spring-js-lack-of-an-api-or-documentation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring 3 RESTful Web Services</title>
		<link>http://eggsylife.co.uk/2010/01/03/spring-3-restful-web-services/</link>
		<comments>http://eggsylife.co.uk/2010/01/03/spring-3-restful-web-services/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 10:00:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring mvc annotations]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=204</guid>
		<description><![CDATA[Step One &#8211; Project Creation with Maven
This tutorial is slightly different to my previous examples. In recent development I have begun to use Maven to manage my project dependencies.
If you are comfortable with creating a Web application project and Maven please skip down to the example code.
First of all you will need to install the [...]]]></description>
			<content:encoded><![CDATA[<h2>Step One &#8211; Project Creation with Maven</h2>
<p>This tutorial is slightly different to my previous examples. In recent development I have begun to use Maven to manage my project dependencies.</p>
<p>If you are comfortable with creating a Web application project and Maven <a href="#restCode">please skip down to the example code.</a></p>
<p>First of all you will need to <a href="http://m2eclipse.sonatype.org/" target="_blank">install the Maven 2 plugin for Eclipse</a>.</p>
<p>After you have installed the plugin create a new &#8216;Dynamic Web Project&#8217; in Eclipse. Call it &#8216;SpringRestfulExample&#8217;.</p>
<p>Click Next. On the next screen we need to edit the src folder and the output folder. First of all remove the current src folder and then click &#8216;Add Folder&#8230;&#8217;</p>
<p>On the Add folder dialog enter the value /src/main/java and click OK.</p>
<p>Then make sure the default output folder reads target/classes. Click next</p>
<p>On the change the Context directory to read /src/main/webapp. Continue on to create the project.</p>
<p>Next we need to introduce Maven. Assuming you have installed the Maven plugin correctly Right click the Project and you should see a Maven menu item.</p>
<p>Highlight Maven and click &#8216;Enable Dependency Management&#8217;.</p>
<p>The next screen will ask you a few Maven options leave everything as it is apart from Packaging &#8211; change this to war. Click Finish.</p>
<p>Now we need to ensure the project uses any Maven dependencies we setup. Right click the Project and choose Properties.</p>
<p>From the Properties menu click &#8216;Java EE Module Dependencies&#8217;. On the right hand pane you will see &#8216;Maven Dependencies&#8217; simply add a tick in that box and click Apply</p>
<h2>Step Two &#8211; Making the Web application work</h2>
<p>Next we&#8217;re going to make sure the web application works OK before we introduce Spring and other requirements.</p>
<p>In your src/main/webapp folder create a file called index.jsp and add some sample content like below:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Spring RESTful Example&lt;/title&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;p&gt;Your WebApplication is up and running....&lt;/p&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>(If you open your web.xml file you&#8217;ll see a list of welcome files we&#8217;re creating the index.jsp of that welcome file list)</p>
<p>I&#8217;ll assume you&#8217;re comfortable with setting up local servers in Eclipse. If you open up your Servers view in Eclipse and add your project to your server, then start you server. You should be able to visit <a href="http://localhost:8080/SpringRestfulExample/" target="_blank">http://localhost:8080/SpringRestfulExample/</a> and view the page you created.</p>
<h2>Step Three &#8211; Introducing Spring</h2>
<p>In the root of your project you&#8217;ll notice that there is a file called pom.xml &#8211; this manages all your Maven dependencies.</p>
<p>Open the file (choose the tab pom.xml in the editor if the file opens up in the Maven editor) and add the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SpringRestfulExample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SpringRestfulExample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.0.1-SNAPSHOT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;org.springframework.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0.0.RELEASE<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/org.springframework.version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;defaultGoal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/defaultGoal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>target<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/main/java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>target/classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/outputDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-expression<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-beans<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-aop<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-context<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-context-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-web<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-webmvc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring-oxm<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${org.springframework.version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.slf4j<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>slf4j-log4j12<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.4.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.tiles<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tiles-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.tiles<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tiles-core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.tiles<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>tiles-jsp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jstl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>taglibs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>standard<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This Maven configuration includes all the required elements for the Spring example we&#8217;re going to be working with. If you need to read up more on Maven and Spring please see the <a name="restCode" href="http://blog.springsource.com/2009/12/02/obtaining-spring-3-artifacts-with-maven/" target="_blank">SpringSource recent blog post</a>.</p>
<h2>Step Four &#8211; Configuring Spring MVC</h2>
<p>Although we&#8217;re going to make an application that serves up RESTful XML we need to integrate Spring Web MVC. First of all we&#8217;ll create some basic Pojo objects that we shall represent.</p>
<p>Right click the src/main/java folder and create a new package called &#8216;com.spring.datasource&#8217;</p>
<p>Within that package create a new class called &#8216;Student&#8217;. Add the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.spring.datasource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Student <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Student<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Student<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id, <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
		<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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the id
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param id the id to set
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the name
	 */</span>
	<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>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param name the name to set
	 */</span>
	<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>
		<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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the same package create a class called &#8216;Classroom&#8217; and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.spring.datasource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlElement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 *
 */</span>
@XmlRootElement<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Classroom <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> classId <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>Student<span style="color: #339933;">&gt;</span> students <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Classroom<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Classroom<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">classId</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
		students <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Student<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Student studentOne <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Student<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #0000ff;">&quot;Student One&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Student studentTwo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Student<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">&quot;Student Two&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		students.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>studentOne<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		students.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>studentTwo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the classId
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getClassId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> classId<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param classId the classId to set
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setClassId<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> classId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">classId</span> <span style="color: #339933;">=</span> classId<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the students
	 */</span>
	@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;student&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> ArrayList<span style="color: #339933;">&lt;</span>Student<span style="color: #339933;">&gt;</span> getStudents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> students<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param students the students to set
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStudents<span style="color: #009900;">&#40;</span>ArrayList<span style="color: #339933;">&lt;</span>Student<span style="color: #339933;">&gt;</span> students<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">students</span> <span style="color: #339933;">=</span> students<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A classroom is just a collection of students &#8211; in this example I have hard coded two students but you can imagine that this may be a datasource. Also note the further use of the @XmlElement annotations.</p>
<p>Now create a new package called &#8216;com.spring.controller&#8217;.</p>
<p>This will contain our Spring MVC controller. Within that package create a new class called &#8216;StudentsController&#8217; and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.spring.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Controller</span><span style="color: #339933;">;</span>
<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>
<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>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.ModelAndView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.spring.datasource.Classroom</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 *
 */</span>
@Controller
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StudentsController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</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> STUDENTS_VIEW_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;studentsView&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	@RequestMapping<span style="color: #009900;">&#40;</span>method<span style="color: #339933;">=</span>RequestMethod.<span style="color: #006633;">GET</span>,value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/students&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> ModelAndView showStudentsPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Classroom classOfStudents <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Classroom<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Class One&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ModelAndView mav <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span>STUDENTS_VIEW_KEY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		mav.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classRoom&quot;</span>, classOfStudents<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> mav<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ll assume you are comfortable with the Spring controller annotations. If not you can have a quick <a href="http://eggsylife.co.uk/category/spring_framework/">lookover my previous Spring blog posts</a>. We use the same controlller for REST requests as well as normal HTML requests. Also as you can see we always return the same view. In essence the controller doesn&#8217;t do anything special or anything different from previous controllers you may have written.</p>
<p>It is within the servlet.xml file configuration that takes over handling the REST requests.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;studentsView&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.xml.MarshallingView&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jaxbMarshaller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- JAXB2 marshaller. Automagically turns beans into xml --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jaxbMarshaller&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<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;classesToBeBound&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spring.datasource.Classroom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spring.datasource.Student<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notice name a bean studentsView which is a <a href="http://static.springsource.org/spring/docs/3.0.0.M2/javadoc-api/index.html?org/springframework/web/servlet/view/xml/MarshallingView.html" target="_blank">Spring Mashalling view</a>. This matches the view our controller returns. We then make use of another great Spring feature called the <a href="http://static.springsource.org/spring/docs/current/api/index.html?org/springframework/web/servlet/view/ContentNegotiatingViewResolver.html" target="_blank">ContentNegotiatingViewResolver</a> this bean allows us to handle XML requests and HTML requests and it basically makes decision on which view resolver to use. The bean is declared like so:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<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;mediaTypes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;html&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<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;viewResolvers&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.UrlBasedViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<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;viewClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The content resolver declares two media types that we can handle (You can add more such as JSON) and then for the two media types we declare two View resolvers. For XML requests we use the BeanNameViewResolver so in practice we use the studentsView bean discussed earlier. Then for normal HTML requests we use the standard UrlBasedViewResolver configured with Tiles2 defintions.</p>
<p>The complete file is as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/beans/spring-beans.xsd</span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context/spring-context.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&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.spring.controller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tilesConfigurer&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<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;definitions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/tiles-defs/templates.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;studentsView&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.xml.MarshallingView&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jaxbMarshaller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- JAXB2 marshaller. Automagically turns beans into xml --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jaxbMarshaller&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<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;classesToBeBound&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spring.datasource.Classroom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.spring.datasource.Student<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        	<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;mediaTypes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;html&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        	<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;viewResolvers&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.UrlBasedViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
					<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;viewClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>To complete the application we should create the required JSP&#8217;s and Tiles configuration. Under the /src/main/webapp create a new folder called &#8216;pages&#8217; and create a file named &#8217;students.jsp&#8217; within it. Add the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&lt;p&gt;Classroom of students&lt;/p&gt;
&nbsp;
&lt;c:forEach var=&quot;student&quot; items=&quot;${classRoom.students}&quot;&gt;
	${student.id} - ${student.name}&lt;br/&gt;
&lt;/c:forEach&gt;</pre></div></div>

<p>Finally we need to create the tiles defintion file the view. Create a new folder called &#8217;tiles-defs&#8217; in the WEB-INF folder and create a file called &#8216;templates.xml&#8217; within it. (Notice this matches the location in our servlet.xml file</p>
<p>Add the following content to the templates.xml file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE tiles-definitions PUBLIC</span>
<span style="color: #00bbdd;">       &quot;-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">       &quot;http://tiles.apache.org/dtds/tiles-config_2_0.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentsView&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/students.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>Step Five &#8211; Updating the web.xml</h2>
<p>Finally we need to configure the web.xml to work with Spring Web MVC. Open your web.xml file (located in the webapp folder) and make sure it matches the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;WebApp_ID&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SpringRestfulExample<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
		<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>SpringRestfulExample<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>
		<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>
		<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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
    		<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>SpringRestfulExample<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>
    		<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>/app/*<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;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
		<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.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>
	<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>
<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></pre></div></div>

<p>That should just about be it. If you start your webserver and visit the page <a href="http://localhost:8080/SpringRestfulExample/app/students" target="_blank">http://localhost:8080/SpringRestfulExample/app/students</a> you should see the JSP page you created with the two students listed.</p>
<p>However if you perform a curl request (mocking an XML request) you should see the XML representation of the classroom:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">curl <span style="color: #339933;">-</span>H <span style="color: #ff0000;">'Accept: application/xml'</span> http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:8080/SpringRestfulExample/app/students</span></pre></div></div>

<h2>Project download</h2>
<p>I have made the project available for download on Google&#8217;s Project hosting. You can get hold of it at <a href="http://code.google.com/p/eggsy-spring3-restful-example/" target="_blank">http://code.google.com/p/eggsy-spring3-restful-example/</a> or via SVN on the URL <strong>http://eggsy-spring3-restful-example.googlecode.com/svn/trunk/</h2>
<h2>Resources</h2>
<p>Spring forum post and help from user Toxic: <a href="http://forum.springsource.org/showthread.php?t=82146" target="_blank">http://forum.springsource.org/showthread.php?t=82146</a></p>
<p>REST in Spring 3 &#8211; Spring source blog : <a href="http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/" target="_blank">http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/</a></p>
<p>Eyal Lupu&#8217;s Java Blog : <a href="http://www.jroller.com/eyallupu/entry/content_negotiation_using_spring_mvc" target="_blank">http://www.jroller.com/eyallupu/entry/content_negotiation_using_spring_mvc</a></p>
<p>SpringSource.org &#8211; RESTful Services : <a href="http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch18s02.html" target="_blank">http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch18s02.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/01/03/spring-3-restful-web-services/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Eclipse Galileo &#8211; Cannot nest src folder</title>
		<link>http://eggsylife.co.uk/2009/12/20/eclipse-galileo-cannot-nest-src-folder/</link>
		<comments>http://eggsylife.co.uk/2009/12/20/eclipse-galileo-cannot-nest-src-folder/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 21:52:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Huh??]]></category>
		<category><![CDATA[cannot nest src folder]]></category>
		<category><![CDATA[eclipse galileo]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[nested src]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=200</guid>
		<description><![CDATA[After recently working on a new web application project, it came to a stage where I had to download the project at home on my personal machine. So of course I logged into our SVN and grabbed a copy.
After getting the project there was an error in the project:

Cannot nest 'Project/src/main/java' inside library 'Project/src

I knew [...]]]></description>
			<content:encoded><![CDATA[<p>After recently working on a new web application project, it came to a stage where I had to download the project at home on my personal machine. So of course I logged into our SVN and grabbed a copy.</p>
<p>After getting the project there was an error in the project:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Cannot nest <span style="color: #0000ff;">'Project/src/main/java'</span> inside library <span style="color: #0000ff;">'Project/src</span></pre></div></div>

<p>I knew previously that this error wasn&#8217;t occuring. As far as the project classpath is concerned there was only one src path which correctly pointed at the folder Project/src/main/java</p>
<p>After searching through the project I found out the issue &#8211; in the .settings folder there is a file named:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">org.<span style="color: #006633;">eclipse</span>.<span style="color: #006633;">wst</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">component</span></pre></div></div>

<p>You should find you have a line similar to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wb-resource</span> <span style="color: #000066;">deploy-path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/classes&quot;</span> <span style="color: #000066;">source-path</span>=<span style="color: #ff0000;">&quot;/src&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>To fix simply remove that line (and if you don&#8217;t have it already, add the line):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wb-resource</span> <span style="color: #000066;">deploy-path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/classes&quot;</span> <span style="color: #000066;">source-path</span>=<span style="color: #ff0000;">&quot;/src/main/java&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/12/20/eclipse-galileo-cannot-nest-src-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Forms, Dynamic Lists and Ajax</title>
		<link>http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/</link>
		<comments>http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 09:11:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax form]]></category>
		<category><![CDATA[dynamic lists]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring form]]></category>
		<category><![CDATA[spring mvc annotations]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=183</guid>
		<description><![CDATA[Spring Forms and Dynamic Binding Lists
Introduction
After my recent posts on Spring forms and annotations &#8211; dynamic lists is logically another step to be discussed.
In the previous examples we use a form backing object &#8211; the form backing objects have tended to be very basic and primitive
Using dynamic lists within these objects makes them slightly more [...]]]></description>
			<content:encoded><![CDATA[<h2>Spring Forms and Dynamic Binding Lists</h2>
<p><strong>Introduction</strong></p>
<p>After my <a href="http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/" target="_blank">recent posts on Spring forms and annotations</a> &#8211; dynamic lists is logically another step to be discussed.</p>
<p>In the previous examples we use a form backing object &#8211; the form backing objects have tended to be very basic and primitive</p>
<p>Using dynamic lists within these objects makes them slightly more complicated. Suppose the following scenario:</p>
<p><strong>Class enrolment example</strong></p>
<ul>
<li>User chooses to create a new class</li>
<li>User types in class name</li>
<li>User also has to specify one (or many) names of students in the class</li>
</ul>
<p>Using that scenario you can imagine a form in which there is a class name field and then below that a simple student name field. When the user clicks add next to the student name &#8211; javascript should simply show another input field for a student name allowing a user to add one or more students.</p>
<p>Eventually when the form gets submitted to the server a list of students will be populated.</p>
<p>However if we don&#8217;t using Spring&#8217;s dynamic lists &#8211; specifically Spring&#8217;s AutoPopulatingList &#8211; we cannot perform such an action as we have out of bounds errors.</p>
<p><strong>Configuring your build environment</strong></p>
<p>Your workspace needs setting up as the previous examples. Using Eclipse create a new project. For your src directory enter the value /WEB-INF/src. For your output directory use the WEB-INF/classes directory.</p>
<p>Add a new lib directory in your WEB-INF folder. Under your WEB-INF directoy you will need the following JAR files (all of which can be obtained from the <a href="http://www.springsource.org/" target="_blank">Spring download</a>). Add the jars to your buildpath.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">commons<span style="color: #339933;">-</span>beanutils.<span style="color: #006633;">jar</span>
commons<span style="color: #339933;">-</span>collections.<span style="color: #006633;">jar</span>
jstl.<span style="color: #006633;">jar</span>
spring<span style="color: #339933;">-</span>core.<span style="color: #006633;">jar</span>
spring<span style="color: #339933;">-</span>webmvc.<span style="color: #006633;">jar</span>
spring.<span style="color: #006633;">jar</span>
standard.<span style="color: #006633;">jar</span>
tiles<span style="color: #339933;">-</span>api<span style="color: #339933;">-</span>2.0.6.<span style="color: #006633;">jar</span>
tiles<span style="color: #339933;">-</span>core<span style="color: #339933;">-</span>2.0.6.<span style="color: #006633;">jar</span>
tiles<span style="color: #339933;">-</span>jsp<span style="color: #339933;">-</span>2.0.6.<span style="color: #006633;">jar</span></pre></div></div>

<p>Next add the Spring servlet.xml to your application. Create a file called dynamiclists-servlet.xml in your WEB-INF folder. Add the following to the file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/beans/spring-beans.xsd</span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context/spring-context.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
&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.annotations.controller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Enables plain Controllers --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tilesConfigurer&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <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;definitions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/tiles-defs/templates.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewResolver&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.UrlBasedViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<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;viewClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next add your web.xml in the same location as your Spring servlet.xml file. Add the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Dynamic Lists Example<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
		<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>dynamiclists<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>
        	<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>
        	<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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
    		<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>dynamiclists<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>
    		<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>*.page<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;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<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></pre></div></div>

<p>Next add a new folder in your WEB-INF directory called tiles-defs and add a file called templates.xml within that. Add the following to the templates.xml file</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE tiles-definitions PUBLIC</span>
<span style="color: #00bbdd;">       &quot;-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">       &quot;http://tiles.apache.org/dtds/tiles-config_2_0.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;form.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/form.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formStudentInsert.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/formStudentInsert.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formResult.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/formResult.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That should be the workspace setup &#8211; next to create our form JSP pages</p>
<p><strong>Form JSP pages and a touch of jQuery</strong></p>
<p>Firstly create a new folder under your application called js. Next <a href="http://docs.jquery.com/Downloading_jQuery" target="_blank">download jQuery</a> and place the jQuery download within the folder.</p>
<p>Under your project create a new folder names pages and create a file named form.jsp within that.This will be our class creation form.</p>
<p>Add the following to your form.jsp file</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&lt;%@ taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot; %&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;/dynamiclists/js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
	var studentPosition = 0;
	$(&quot;#addStudentButton&quot;).click(function() {
		studentPosition++;
&nbsp;
		$.get(&quot;&lt;%=request.getContextPath()%&gt;/appendStudentView.page&quot;, { fieldId: studentPosition},
			function(data){
				$(&quot;#submitRow&quot;).before(data);
		});
	});
});
&lt;/script&gt;
&nbsp;
&lt;form:form method=&quot;post&quot; name=&quot;classForm&quot; id=&quot;classForm&quot; commandName=&quot;classCommand&quot;&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Class Name:&lt;/td&gt;
			&lt;td colspan=&quot;2&quot;&gt;&lt;form:input path=&quot;className&quot; size=&quot;40&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td colspan=&quot;3&quot;&gt;&lt;strong&gt;Students&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Student name&lt;/td&gt;
			&lt;td&gt;
				&lt;spring:bind path=&quot;classCommand.students[0].studentName&quot;&gt;
					&lt;form:input path=&quot;${status.expression}&quot; size=&quot;40&quot; /&gt;
	  			&lt;/spring:bind&gt;
			&lt;/td&gt;
			&lt;td&gt;&lt;input type=&quot;button&quot; id=&quot;addStudentButton&quot; value=&quot;Add&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr id=&quot;submitRow&quot;&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Save Class&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/form:form&gt;</pre></div></div>

<p>This probably requires a bit of an explanation. The first two lines of the file include the required Spring tag libraries.</p>
<p>After that we include the link to our downloaded jQuery Javascript source. I&#8217;ll make an assumption that people are familiar with jQuery.</p>
<p>Next we use <a href="http://docs.jquery.com/Events/click#fn" target="_blank">jQuery to add a click event listener</a> to our form &#8216;Add&#8217; button. When the user clicks add button we fire off an <a href="http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype" target="_blank">Ajax Get request</a> to our server which sends an incremented number as part of its data string. This Ajax request hits our server which simply <a href="http://docs.jquery.com/Manipulation/before#content" target="_blank">appends a new row</a> to the form for another student name. We use the number for the index number in the collection of students.</p>
<p>The response is simply appended as a new row in our form before the form submit button. After that Javascript the rest is a basic Spring form.</p>
<p>Next create a new file called formStudentInsert.jsp in the same location as your form.jsp and add the following contents.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&lt;%@ taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot; %&gt;
&nbsp;
&lt;tr&gt;
	&lt;td&gt;&amp;nbsp;&lt;/td&gt;
	&lt;td&gt;
		&lt;form:input path=&quot;classCommand.students[${studentNumber}].studentName&quot; size=&quot;40&quot; /&gt;
	&lt;/td&gt;
	&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;</pre></div></div>

<p>As mentioned before this is JSP that gets appended to our form when we send the Ajax request.</p>
<p>The form should look similar to the image below.</p>
<p><a href="http://eggsylife.co.uk/wp-content/uploads/2009/11/class_form.jpg"><img src="http://eggsylife.co.uk/wp-content/uploads/2009/11/class_form-150x150.jpg" alt="Picture of class form" title="Class Form" width="150" height="150" class="size-thumbnail wp-image-186" /></a></p>
<p>Next create a JSP page that is displayed when the user submits the form. Create a new JSP called formResult.jsp and add the following contents.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&nbsp;
&lt;h2&gt;Class ${savedClass.className}&lt;/h2&gt;
&nbsp;
&lt;p&gt;Students&lt;/p&gt;
&nbsp;
&lt;ul&gt;
	&lt;c:forEach var=&quot;student&quot; items=&quot;${savedClass.students}&quot;&gt;
		&lt;li&gt;${student.studentName}&lt;/li&gt;
	&lt;/c:forEach&gt;
&lt;/ul&gt;</pre></div></div>

<p><strong>The Form Backing Object</strong></p>
<p>We next need to create the form backing objects that will be used for this example. Create a new Java class called Student and add the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy _at_ eggsylife.co.uk
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Student<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> studentName <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getStudentName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> studentName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStudentName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> studentName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">studentName</span> <span style="color: #339933;">=</span> studentName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class is a simple Student object. Student objects will belong to a class object. Now we can go ahead and create the Class object.</p>
<p>Create a new class called ClassCommand and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.util.AutoPopulatingList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy _at_ eggsylife.co.uk
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassCommand <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> className <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> AutoPopulatingList students <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getClassName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> className<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setClassName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> className<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">className</span> <span style="color: #339933;">=</span> className<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> AutoPopulatingList getStudents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> students<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStudents<span style="color: #009900;">&#40;</span>AutoPopulatingList students<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">students</span> <span style="color: #339933;">=</span> students<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class command object is the backing object we shall use for our form. It simply has a class name. However it does also have a list of students. For this we could have used a basic java.util.ArrayList but this causes issues when attempting to bind the form. As the list wouldn&#8217;t (initially) have any contents we would experience IndexOutOfBounds exception messages. This is where we can use the incredibly helpful <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/util/AutoPopulatingList.html" target="_blank">Spring AutopopulatingList</a>.</p>
<p><strong>Spring Annotated Form Controller</strong></p>
<p>Finally we need to create the Spring Form controller that handles our various requests. Note in the servlet XML file we specified that we would scan the package com.annotations.controller package for controller annotations. Go ahead and create this package. (You can of course change this). Now create a class called ClassFormController containing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Controller</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.ui.ModelMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.util.AutoPopulatingList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.ModelAttribute</span><span style="color: #339933;">;</span>
<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>
<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>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.RequestParam</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.annotations.command.ClassCommand</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.annotations.command.Student</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy _at_ eggsylife.co.uk
 */</span>
@Controller
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClassFormController
<span style="color: #009900;">&#123;</span>
&nbsp;
	@ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classCommand&quot;</span><span style="color: #009900;">&#41;</span>
    	<span style="color: #000000; font-weight: bold;">public</span> ClassCommand getClassCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    	<span style="color: #009900;">&#123;</span>
		ClassCommand classCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		classCommand.<span style="color: #006633;">setStudents</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AutoPopulatingList<span style="color: #009900;">&#40;</span>Student.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> classCommand<span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * &lt;p&gt;Called when the form is initially displayed&lt;/p&gt;
	 * @param model
	 * @return
	 */</span>
	@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/processClassForm.page&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> showNameForm<span style="color: #009900;">&#40;</span>ModelMap model<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>	
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;form.view&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * &lt;p&gt;Called when the user clicks the add button&lt;/p&gt;
	 * @param fieldId
	 * @param model
	 * @return
	 */</span>
	@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/appendStudentView.page&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> appendStudentField<span style="color: #009900;">&#40;</span>@RequestParam <span style="color: #003399;">Integer</span> fieldId, ModelMap model<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>	
		model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;studentNumber&quot;</span>, fieldId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;formStudentInsert.view&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * &lt;p&gt;Called when the user finally submits the class form&lt;/p&gt;
	 * @param classCommand
	 * @param model
	 * @return
	 */</span>
	@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span>, value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/processClassForm.page&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> onSubmit<span style="color: #009900;">&#40;</span> @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;classCommand&quot;</span><span style="color: #009900;">&#41;</span> ClassCommand classCommand,  ModelMap model <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;savedClass&quot;</span>, classCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;formResult.view&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ll assume you&#8217;re comfortable with <a href="http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/" target="_blank">Spring annotations</a> so I&#8217;ll just highlight the important parts. Firstly notice the way we declare our ClassCommand object. We new up the object then set the Students list as a new AutoPopulatingList of type Student.class. This is again so that we can ensure we experience no IndexOutOfBounds errors. The next method is called when we display our form simply returning the form.view. After that we have the method that gets called when the user clicks the Add button. This returns the small HTMl snippet that gets appended to our form. Finally we have the onSubmit method that gets called when we submit our form and returns the formResult view.</p>
<p>Ok so thats pretty much it. If you followed the examples word for word you after setting up your web application you should be able to visit <a href="http://localhost:8080/dynamiclists/processClassForm.page">http://localhost:8080/dynamiclists/processClassForm.page</a> and test your web app.</p>
<p>You can also <a href="http://eggsylife.co.uk/SpringDynamicListsExample.zip" target="_blank">download the example project here</a> within the project there is a docs folder that also includes a sample Tomcat context file for application deployment.</p>
<p>References: <a href="http://blog.richardadamdean.com/?page_id=119">http://blog.richardadamdean.com/?page_id=119</a></p>
<p>Any comments or questions are always welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Ajax Spring Forms with Annotations and jQuery</title>
		<link>http://eggsylife.co.uk/2009/11/12/ajax-spring-forms-with-annotations-and-jquery/</link>
		<comments>http://eggsylife.co.uk/2009/11/12/ajax-spring-forms-with-annotations-and-jquery/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 10:30:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax form]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring annotations]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=152</guid>
		<description><![CDATA[In one of my previous posts we documented how to implement Spring Annotations with a form. If you haven&#8217;t got a copy of the project from that post you can grab it here.
Building on that example this guide shows how, using Spring and jQuery, you can Ajaxify your forms so that when submitted the user [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my previous posts we documented <a href="http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/">how to implement Spring Annotations with a form</a>. If you haven&#8217;t got a copy of the project from that post you can <a href="http://eggsylife.co.uk/SpringAnnotationsExample.zip">grab it here.</a></p>
<p>Building on that example this guide shows how, using Spring and jQuery, you can Ajaxify your forms so that when submitted the user doesn&#8217;t have to be redirected to a new page</p>
<p><strong>jQuery and required plugins</strong></p>
<p>Create a new folder in the root of your project and call it &#8216;js&#8217;</p>
<p>Then grab a copy of <a href="http://docs.jquery.com/Downloading_jQuery">jQuery</a>. The minified version will be fine. Add the jQuery javascript file to your &#8216;js&#8217; folder.</p>
<p>Currently it is at version 1.3.2 so I have a file called jquery-1.3.2.min.js in my js folder</p>
<p>Next download a very useful plugin the <a href="http://www.malsup.com/jquery/form/">jQuery Form Plugin</a>. Also place the plugin in your js folder.</p>
<p><strong>Linking your JS</strong></p>
<p>Open the form.jsp file and add the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;/annotations/js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;/annotations/js/jquery.form.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Here we are linking to the two jQuery javascript files we have just downloaded. Note I reference /annotations/ that is my application name yours may however be different.</p>
<p><strong>Convert the form to Ajax</strong></p>
<p>This step involves using the jQuery form plugin to make your form submit via Ajax. After including the scripts add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #003366; font-weight: bold;">var</span> formOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>target<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#nameFormResponse'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#nameForm'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxForm</span><span style="color: #009900;">&#40;</span>formOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This assumes you are quite comfortable with jQuery but just to explain, in the above code we are saying that when the document is read fetch the form with the id of nameForm and convert it to  an ajaxForm (The ajaxForm function is built into the form plugin we download). We are also specifying some options for the ajaxForm method namely the target. A target is an jQuery selector that the Ajax Form plugin will automatically populate with the response from your submit</p>
<p>After your form go ahead and add in the div with the same id as the target, in the case above id=&#8221;nameFormResponse&#8221;.</p>
<p>The whole form.jsp should now look like so:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;/annotations/js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;/annotations/js/jquery.form.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot;&gt;
	$(document).ready(function() { 
		var formOptions = {target: '#nameFormResponse'};
		$('#nameForm').ajaxForm(formOptions);
	});
&lt;/script&gt;
&nbsp;
&nbsp;
&lt;form:form method=&quot;post&quot; name=&quot;nameForm&quot; id=&quot;nameForm&quot; commandName=&quot;nameCommand&quot;&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Name:&lt;/td&gt;
			&lt;td&gt;&lt;form:input path=&quot;name&quot; size=&quot;40&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Send my name&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/form:form&gt;
&nbsp;
&lt;div id=&quot;nameFormResponse&quot;&gt;
&lt;/div&gt;</pre></div></div>

<p>That should be it &#8211; so now if you visit <em>http://localhost:8080/annotations/processName.page</em> and submit the form you will see that the page doesn&#8217;t change and we render the response of the form submit into our nameFormResponse div element.</p>
<p><strong>You can <a href="/SpringAnnotationsAjax.zip">grab a copy of this project here</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/12/ajax-spring-forms-with-annotations-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring annotations User Principal</title>
		<link>http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/</link>
		<comments>http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 16:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring form]]></category>
		<category><![CDATA[user principal]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=147</guid>
		<description><![CDATA[In case anyone was wondering after reading the previous Spring Annotations guide you can also user various parameters to get hold of the current logged in user.
To get hold of the user principal simply add a new parameter to your Request annotated methods called:

org.springframework.web.context.request.WebRequest myWebRequest

Using the previous tutorial as an example your method should look [...]]]></description>
			<content:encoded><![CDATA[<p>In case anyone was wondering after reading the previous Spring Annotations guide you can also user various parameters to get hold of the current logged in user.</p>
<p>To get hold of the user principal simply add a new parameter to your Request annotated methods called:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">org.<span style="color: #006633;">springframework</span>.<span style="color: #006633;">web</span>.<span style="color: #006633;">context</span>.<span style="color: #006633;">request</span>.<span style="color: #006633;">WebRequest</span> myWebRequest</pre></div></div>

<p>Using the previous tutorial as an example your method should look like something similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> showNameForm<span style="color: #009900;">&#40;</span>WebRequest webRequest, ModelMap model<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>	
	<span style="color: #003399;">String</span> username <span style="color: #339933;">=</span> webRequest.<span style="color: #006633;">getUserPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//Do what you need with the username</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;form.view&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Form Annotations Project Download</title>
		<link>http://eggsylife.co.uk/2009/11/09/spring-form-annotations-project-download/</link>
		<comments>http://eggsylife.co.uk/2009/11/09/spring-form-annotations-project-download/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 13:22:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[example project]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring form]]></category>
		<category><![CDATA[spring mvc annotations]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=145</guid>
		<description><![CDATA[I have made the Spring Annotations project available for download.
I would recommend still using the tutorial and use the project for reference
Spring Form Annotations Project
]]></description>
			<content:encoded><![CDATA[<p>I have made the <a href="/SpringAnnotationsExample.zip">Spring Annotations project available for download.</a></p>
<p>I would recommend still using the tutorial and use the project for reference</p>
<p><a href="/SpringAnnotationsExample.zip">Spring Form Annotations Project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/09/spring-form-annotations-project-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Annotations and Forms</title>
		<link>http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/</link>
		<comments>http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 09:09:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[annotated form]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring mvc annotations]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=98</guid>
		<description><![CDATA[Since the release of Spring 2.5, the framework has allowed for MVC annotations for mapping your URL&#8217;s to your various controllers.
This tutorial gives an example of how the annotations can be used within your application.
Grab the Spring Framework
First of all grab the latest stable release of Spring (version 2.5.6)
I&#8217;m going to assume you&#8217;re comfortable in [...]]]></description>
			<content:encoded><![CDATA[<p>Since the release of Spring 2.5, the framework has allowed for MVC annotations for mapping your URL&#8217;s to your various controllers.</p>
<p>This tutorial gives an example of how the annotations can be used within your application.</p>
<p><strong>Grab the Spring Framework</strong></p>
<p>First of all grab the <a href="http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-2.5.6.SEC01-with-dependencies.zip">latest stable release of Spring (version 2.5.6)</a></p>
<p>I&#8217;m going to assume you&#8217;re comfortable in creating Web Application Java projects using Eclipse so I&#8217;ll ignore this step.</p>
<p><strong>Configuring your build path</strong></p>
<p>We simply need to add the following JAR&#8217;s to our build path. Add the Jars into your lib directory.</p>
<pre>
commons-beanutils.jar
commons-collections.jar
commons-digester.jar
jstl.jar
spring.jar
spring-core.jar
spring-webmvc.jar
standard.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
</pre>
<p>Note: This is the minimum amount of required libs for this example and all Jars can be taken from the Spring Framework download lib and dist directories.</p>
<p><strong>JSP Form page creation</strong></p>
<p>If you haven&#8217;t already create a new folder in your project called pages and add a file in it called form.jsp (Of course depending on configuration you can name the files however you like.</p>
<p>Add the following to the form.jsp file. This creates the simple JSP page that is our form</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&nbsp;
&lt;form:form method=&quot;post&quot; name=&quot;nameForm&quot; id=&quot;nameForm&quot; commandName=&quot;nameCommand&quot;&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Name:&lt;/td&gt;
			&lt;td&gt;&lt;form:input path=&quot;name&quot; size=&quot;40&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Send my name&quot; /&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/form:form&gt;</pre></div></div>

<p>In the JSP you can see I&#8217;m using the Spring frameworks form tag library. Also note we specify and commandName, if you haven&#8217;t already encountered this &#8211; that is important. We specify the nameCommand and we shall be using that later.</p>
<p>Now add another JSP called formResult.jsp. This is the JSP that gets displayed when the form has been submitted. Add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
&nbsp;
&lt;p&gt;${formResult}&lt;/p&gt;</pre></div></div>

<p><strong>Configuring the web.xml</strong></p>
<p>Next we can move onto the web.xml and what setup requirements we need to make our Web application work with Spring MVC</p>
<p>If you haven&#8217;t already create a web.xml file in your WEB-INF folder</p>
<p>Add the following into your web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">		 <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spring Annotations Example<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
		<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>annotations<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>
                <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>
                <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>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&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>
    	        <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>annotations<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>
    	         <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>*.page<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;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.ContextLoaderListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<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>
		<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>classpath:applicationContext.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>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<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></pre></div></div>

<p>To explain we&#8217;re going to create a web application under the context name annotations and then we&#8217;re mapping the Spring DispatcherServlet to it and Spring will handle any URL&#8217;s ending in .page</p>
<p>Again depending on configuration this doesn&#8217;t have to be .page it can be something of your choice.</p>
<p><strong>Tiles configuration</strong></p>
<p>For this example I am using Tiles for the &#8216;View&#8217; part of our MVC</p>
<p>Under the WEB-INF folder create a new directory called &#8217;tiles-defs&#8217; and add a file in it called &#8216;templates.xml&#8217;.</p>
<p>Add the following to your templates.xml file</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE tiles-definitions PUBLIC</span>
<span style="color: #00bbdd;">       &quot;-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">       &quot;http://tiles.apache.org/dtds/tiles-config_2_0.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;form.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/form.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formResult.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/formResult.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tiles-definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Very basically we are defining a new View called form.view that relates to our previously created form.jsp page and formResult.view that maps to the formResult.jsp</p>
<p><strong>Spring MVC Servlet.xml</strong></p>
<p>Now we can configure the Spring MVC servlet.xml file. Create a new file called annotations-servlet.xml and place it in the WEB-INF directory. (The same place as the web.xml.</p>
<p>Note: we call the file annotations-servlet.xml because of the servlet mapping named using in our web.xml &#8211; you can of course change this.</p>
<p>Add the following to the file</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/beans/spring-beans.xsd</span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context </span>
<span style="color: #009900;">                           http://www.springframework.org/schema/context/spring-context.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
&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.annotations.controller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!-- Enables plain Controllers --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tilesConfigurer&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	    <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;definitions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/tiles-defs/templates.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewResolver&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.UrlBasedViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<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;viewClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.tiles2.TilesView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This file sets up a few things &#8211; firstly you&#8217;ll notice that we use the context-component scan &#8211; this means that Spring will look through the package we specify for any annotations which means when we annotate our URL&#8217;s they will work. In this example I scan the com.annotations.controller package. We also setup the Spring annotation handlers.</p>
<p>The items after that basically configure our view resolver which in our case is Tiles and we specify the location of the templates.xml file we created in the previous steps</p>
<p><strong>A bit of business logic</strong></p>
<p>To make the tutorial a bit more relevant I&#8217;m going to include Springs applicationContext.xml file which would generally include a web applications database/business logic beans.</p>
<p>In the WEB-INF/src folder create a new file called &#8216;applicationContext.xml&#8217;</p>
<p>Add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;businessBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.annotations.business.BusinessLogicBeanImpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Just define one business logic bean com.annotations.business.BusinessLogicBeanImpl which will code next.</p>
<p><strong>The business logic bean</strong></p>
<p>Go ahead and create and create a new java package under the path com.annotations.business</p>
<p>Add a new &lt;&lt;Interface&gt;&gt; into this class called BusinessLogicBean and add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.annotations.business</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> BusinessLogicBean
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> processName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re defining a single business logic method that will handle the name the user has entered and do something with it</p>
<p>Create a new class in the same package called BusinessLogicBeanImpl and make it implement the BusinessLogicBean interface.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.annotations.business</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessLogicBeanImpl <span style="color: #000000; font-weight: bold;">implements</span> BusinessLogicBean
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/* (non-Javadoc)
	 * @see com.annotations.business.BusinessLogicBean#processName(java.lang.String)
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> processName<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>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> name.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;admin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello admin welcome&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hey you're not admin but Hi to &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; anyway!!&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve defined a pretty useless business method but feel free to change this <img src='http://eggsylife.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<p><strong>The Form command Object</strong></p>
<p>On the form JSP file in the form we mentioned an attribute called commandName and we gave it a value of nameCommand. This basically tells Spring what object to use behind the form. We can now go ahead and create that object.</p>
<p>Create a new package called com.annotations.command and place a new class in it called NameCommand. Add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.annotations.command</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameCommand
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the name
	 */</span>
	<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>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param name the name to set
	 */</span>
	<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>
		<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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice we have one class variable called name this maps to the name input field on our form</p>
<p><strong>Finally&#8230;the annotated controller</strong></p>
<p>If you remember in the servlet.xml file we stated that we would scan the com.annotations.controller package. Go ahead and create this package.</p>
<p>In the package create a new class called NameFormController</p>
<p>Just before the class defintion add the following annotations:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Controller
@RequestMapping<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/processName.page&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The annotations mean that this controller will map any URL&#8217;s matching processName.page</p>
<p>Next within the class add a variable that is our BusinessLogicBean. Add the lines:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Autowired
<span style="color: #000000; font-weight: bold;">protected</span> BusinessLogicBean businessBean <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

<p>This introduces another annotation the @Autowired, this means Spring will automatically inject this object for us. Note the bean is given the name businessBean this identically matches our definition in the applicationContext file</p>
<p>Next we setup the form backing object which if you remember was the NameCommand this is done with a few more annotations like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameCommand&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> NameCommand getProductCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    NameCommand nameCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> nameCommand<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again notice that tie we use an annotation of @ModelAttribute and give it a name of nameCommand which is used by the form on our JSP.</p>
<p>Next up is to add the annotated get method. This is the method that renders the view which should display the form.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> showNameForm<span style="color: #009900;">&#40;</span>ModelMap model<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>	
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;form.view&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Important to notice here is that we return &#8220;form.view&#8221; which matches the tiles definition we created earlier. Also note that we use the @RequestMapping method and we map this to the GET which is called to display the form</p>
<p>Finally we can go ahead and write the method that handles the form submit. Add the following to the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> onSubmit<span style="color: #009900;">&#40;</span> @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameCommand&quot;</span><span style="color: #009900;">&#41;</span> NameCommand nameCommand,  ModelMap model <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> formResult <span style="color: #339933;">=</span> businessBean.<span style="color: #006633;">processName</span><span style="color: #009900;">&#40;</span>nameCommand.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formResult&quot;</span>, formResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;formResult.view&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This method is slightly more complex. As before we use the @RequestMapping annotation but this time we map it to the POST. In the parameters however we use the @ModelAttribute annotation again but this time it specifies that from the form being submitted we will have a populated nameCommand object. Using our business logic bean we process the name entered.</p>
<p>If you remember the formResult.jsp we simply output an item called $formResult, using our model object we add the formResult to the model so that the JSP is able to access this and then we simply return the formResult tiles view</p>
<p>The whole class should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.annotations.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Controller</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.ui.ModelMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.annotation.ModelAttribute</span><span style="color: #339933;">;</span>
<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>
<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>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.annotations.business.BusinessLogicBean</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.annotations.command.NameCommand</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author Eggsy - eggsy_at_eggsylife.co.uk
 */</span>
@Controller
@RequestMapping<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/processName.page&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NameFormController
<span style="color: #009900;">&#123;</span>
    @Autowired
    <span style="color: #000000; font-weight: bold;">protected</span> BusinessLogicBean businessBean <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameCommand&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> NameCommand getProductCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        NameCommand nameCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NameCommand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> nameCommand<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
    @RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">GET</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> showNameForm<span style="color: #009900;">&#40;</span>ModelMap model<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>	
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;form.view&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @RequestMapping<span style="color: #009900;">&#40;</span>method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> onSubmit<span style="color: #009900;">&#40;</span> @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nameCommand&quot;</span><span style="color: #009900;">&#41;</span> NameCommand nameCommand,  ModelMap model <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> formResult <span style="color: #339933;">=</span> businessBean.<span style="color: #006633;">processName</span><span style="color: #009900;">&#40;</span>nameCommand.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	model.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formResult&quot;</span>, formResult<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;formResult.view&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Testing&#8230;</strong></p>
<p>Phew that should be it &#8211; assuming you have setup your application the same as mine and have configured your server environment such as Tomcat if you visit http://localhost:8080/annotations/processName.page you should see the form. Try submitting the form and you should see the response as dictated by our business logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
