<?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>Mon, 18 Apr 2011 19:40:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Obfuscating Javascript with Maven and Google Closure</title>
		<link>http://eggsylife.co.uk/2011/04/18/obfuscating-javascript-with-maven-and-google-closure/</link>
		<comments>http://eggsylife.co.uk/2011/04/18/obfuscating-javascript-with-maven-and-google-closure/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 11:45:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[automate obfuscation]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[google closure]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven build]]></category>
		<category><![CDATA[obfuscator]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=375</guid>
		<description><![CDATA[GitHub Repository &#8211; with Maven project example At the time of writing I couldn&#8217;t find an easy way to automate Javascript obfuscation when building a Maven project. Using the Googles Closure compiler I decided to work on my own concept. This guide shows how you can incorporate the same process into your maven projects. EDIT: [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2011/04/18/obfuscating-javascript-with-maven-and-google-closure/" size="medium" count="true"></div></div><p><a href="https://github.com/eggsy84/MavenClosureCompiler" target="_blank">GitHub Repository &#8211; with Maven project example</a></p>
<p>At the time of writing I couldn&#8217;t find an easy way to automate Javascript obfuscation when building a <a href="http://maven.apache.org/" target="_blank">Maven</a> project. Using the <a href="http://code.google.com/closure/compiler/docs/gettingstarted_ui.html" target="_blank">Googles Closure compiler</a> I decided to work on my own concept. This guide shows how you can incorporate the same process into your maven projects.</p>
<p><em>EDIT: <a href="http://code.google.com/p/wro4j/wiki/MavenPluginWithYuiOrGoogleClosure" target="_blank">wro4j is a plugin</a> that makes use of the Closure compiler however it only allows for SIMPLE_CUSTOMIZATIONS level obfuscation.</em></p>
<p><em>EDIT: As per Alex&#8217;s comment below it seems you can <a href="http://code.google.com/p/wro4j/wiki/MavenPluginCustomization" target="_blank">customise the wro4j plugin</a></em></p>
<p>Firstly, you need to include the <a href="http://mojo.codehaus.org/exec-maven-plugin/" target="_blank">Exec-Maven</a>. This plugin allows you to execute Java applications during your build process. Include this plugin in your maven pom.xml 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;plugin<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.codehaus.mojo<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>exec-maven-plugin<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<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;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mainClass<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>uk.co.eggsylife.JavascriptObfuscator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mainClass<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}/src/main/webapp/scripts/raw.js<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Input file --&gt;</span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}/src/main/webapp/scripts/raw.min.js<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/argument<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> <span style="color: #808080; font-style: italic;">&lt;!-- Output file --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notice the configuration of the plugin. Firstly we are targeting the goal &#8216;java&#8217;. I shall discuss this later. Secondly, we <a href="http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html" target="_blank">define a main class</a>. This is the Java application the plugin will run. Finally we have the arguments, these are the arguments that will be passed to the &#8216;public static void main(String[] args)&#8217; method. In the example above we pass the input file and the location/name of the output file.</p>
<p>Now for the &#8216;JavascriptObfuscator&#8217; class. This class communicates with the <a href="http://code.google.com/closure/compiler/docs/gettingstarted_api.html" target="_blank">Closure Compiler API</a> passing in the raw Javascript from the input file &#8211; asking for the obfuscated version from the API and then saving the output to the specified output file.</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;">uk.co.eggsylife</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</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> JavascriptObfuscator <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param args
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Obfuscating Javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> args.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">int</span> argumentCount <span style="color: #339933;">=</span> args.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> jsFile <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #003399;">String</span> outputFile <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #003399;">String</span> compilationLevel <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ADVANCED_OPTIMIZATIONS&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>argumentCount <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				compilationLevel  <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
			<span style="color: #003399;">String</span> outputFormat <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;text&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>argumentCount <span style="color: #339933;">==</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				outputFormat  <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Processing ( &quot;</span> <span style="color: #339933;">+</span> jsFile <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; )&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Compilation Level ( &quot;</span> <span style="color: #339933;">+</span> compilationLevel <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; )&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Output Format ( &quot;</span> <span style="color: #339933;">+</span> outputFormat <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; )&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving to ( &quot;</span> <span style="color: #339933;">+</span> outputFile <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; )&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #003399;">StringBuffer</span> contents <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">BufferedReader</span> reader <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;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>jsFile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	            		reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileReader</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			    	<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
			    	<span style="color: #666666; font-style: italic;">// Read the Javascript</span>
			    	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>text <span style="color: #339933;">=</span> reader.<span style="color: #006633;">readLine</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			        	contents.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;line.separator&quot;</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;
				<span style="color: #666666; font-style: italic;">// Javascript Pre-Obfuscation</span>
			    	<span style="color: #003399;">String</span> jsCode <span style="color: #339933;">=</span> contents.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			    	ClosureCompiler compiler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClosureCompiler<span style="color: #009900;">&#40;</span>outputFile, jsCode, compilationLevel, outputFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			    	compiler.<span style="color: #006633;">processCompilation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Obfuscation Finished&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span> 
			<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	            		e.<span style="color: #006633;">printStackTrace</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: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	                		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>reader <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>
	                			reader.<span style="color: #006633;">close</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> 
				<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	                		e.<span style="color: #006633;">printStackTrace</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>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No (not enough) arguments passed to Obfuscation - requires JS file and Output directory&quot;</span><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;">&#125;</span></pre></div></div>

<p>The class *must* have at least two arguments &#8211; the input and output file &#8211; however you can pass further arguments which allow you to customise the Closure compiler obfuscation.</p>
<ul>
<li>args[0] = Input file (required)</li>
<li>args[1] = Output file (required)</li>
<li>args[2] = Compilation level (default ADVANCED_OPTIMIZATIONS)</li>
<li>args[3] = Output format (default test)</li>
</ul>
<p>You can of course customise this class to make it perform as you like</p>
<p>Next notice the use of the &#8216;ClosureCompiler&#8217; class, this class communicates with the Closure API</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;">uk.co.eggsylife</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.BufferedWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStreamReader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStreamWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLEncoder</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> ClosureCompiler <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> outputFile <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> <span style="color: #003399;">String</span> jsCode <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> <span style="color: #003399;">String</span> compilationLevel <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> <span style="color: #003399;">String</span> outputFormat <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> ClosureCompiler<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> outputFile, <span style="color: #003399;">String</span> jsCode, <span style="color: #003399;">String</span> compilationlevel, <span style="color: #003399;">String</span> outputFormat<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;">outputFile</span> <span style="color: #339933;">=</span> outputFile<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jsCode</span> <span style="color: #339933;">=</span> jsCode<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">compilationLevel</span> <span style="color: #339933;">=</span> compilationlevel<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">outputFormat</span> <span style="color: #339933;">=</span> outputFormat<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> processCompilation<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;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> obfuscatedCode <span style="color: #339933;">=</span> sendForCompilation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			saveObfuscatedCodeToFile<span style="color: #009900;">&#40;</span>outputFile, obfuscatedCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> 
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Exception when obfuscating code: &quot;</span> <span style="color: #339933;">+</span> e.<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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> sendForCompilation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://closure-compiler.appspot.com/compile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">String</span> data <span style="color: #339933;">=</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;js_code&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>jsCode, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		data <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;compilation_level&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>compilationLevel, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		data <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output_format&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>outputFormat, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		data <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output_info&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">URLEncoder</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;compiled_code&quot;</span>, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">URLConnection</span> conn <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setDoOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">OutputStreamWriter</span> wr <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">OutputStreamWriter</span><span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		wr.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		wr.<span style="color: #006633;">flush</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;">// Get the response</span>
		<span style="color: #003399;">BufferedReader</span> rd <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">getInputStream</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>
		<span style="color: #003399;">String</span> codeLine<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">StringBuffer</span> obfuscatedCodeLine <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>codeLine <span style="color: #339933;">=</span> rd.<span style="color: #006633;">readLine</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			obfuscatedCodeLine.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>codeLine<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;line.separator&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		wr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		rd.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> obfuscatedCodeLine.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><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;">private</span> <span style="color: #000066; font-weight: bold;">void</span> saveObfuscatedCodeToFile<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> outputdirectory, <span style="color: #003399;">String</span> javascriptCode<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;">FileWriter</span> fstream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileWriter</span><span style="color: #009900;">&#40;</span>outputdirectory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">BufferedWriter</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedWriter</span><span style="color: #009900;">&#40;</span>fstream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>javascriptCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//Close the output stream</span>
		out.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It fetches the obfuscated result and saves it to the specified output file</p>
<p>That just about completes the blog, the last thing is to show how your would invoke this. Thats a simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">mvn exec:java</pre></div></div>

<p>You can of course customise the goal of the plugin so that it obfuscates as and when you need it to.</p>
<p>I would also welcome any comments or further customisations on this</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2011/04/18/obfuscating-javascript-with-maven-and-google-closure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring JS &#8211; Ajax Popup with list refresh</title>
		<link>http://eggsylife.co.uk/2010/11/08/spring-js-ajax-popup-with-list-refresh/</link>
		<comments>http://eggsylife.co.uk/2010/11/08/spring-js-ajax-popup-with-list-refresh/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 09:49:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax form]]></category>
		<category><![CDATA[list refresh]]></category>
		<category><![CDATA[partial rendering]]></category>
		<category><![CDATA[popup]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring js]]></category>
		<category><![CDATA[spring web flow]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=272</guid>
		<description><![CDATA[The concept I have been working with Spring-JS for a while now and have noticed that there doesn&#8217;t seem to be too many examples out there. This tutorial should hopefully address one common usage of Spring JS and its Ajax simplifications. One common use of any Ajax framework is to have a page list a [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/11/08/spring-js-ajax-popup-with-list-refresh/" size="medium" count="true"></div></div><h3>The concept</h3>
<p>I have been working with Spring-JS for a while now and have noticed that there doesn&#8217;t seem to be too many examples out there. This tutorial should hopefully address one common usage of Spring JS and its Ajax simplifications.</p>
<p>One common use of any Ajax framework is to have a page list a number of items. Then allow the user to add to those items. When adding an item a popup is displayed asking for the item details. Upon saving the item the popup is closed and the list underneath is refreshed. All actions working through Ajax and without changing page.</p>
<p>As a flow this would be:</p>
<ul>
<li>User clicks to list items</li>
<li>User clicks to add items</li>
<li>Popup displayed asking for new item details</li>
<li>User clicks save within popup</li>
<li>If new details passes validation</li>
<li>List of items refreshed and popup closed</li>
</ul>
<h3>Getting hold of the sample code</h3>
<p>I have hosted the completed project on GitHub. So please feel free to clone the repository @ git://github.com/eggsy84/SpringPopupRefreshList.git</p>
<p>Also if anyone has any contributions to add to the project please drop me an email or submit a request via GitHub for write access. The GitHub page is <a href="https://github.com/eggsy84/SpringPopupRefreshList" target="_blank">https://github.com/eggsy84/SpringPopupRefreshList</a>. You can also download the project from this site. The rest of the tutorial will continue assuming you have grabbed the source code.</p>
<h3>The servlet XML</h3>
<p>We firstly need to configure our <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html" target="_blank">Spring MVC</a> through the servlet-context.xml file. For Spring-JS and partial rendering to work we define the use of <a href="http://tiles.apache.org/" target="_blank">Apache Tiles as our view resolving</a> mechanism. We can do this by simply configuring a few beans:</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;beans: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;beans: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;beans:list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans:value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/tile-defs/tile-defs.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans:bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans: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;beans: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;/beans:bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The main item to note here is the location of our tile defintions file. This shall be discussed shortly. The final thing to add to the servlet-xml file is to tell <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-controller" target="_blank">Spring to context scan our project for Spring controllers</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Enables the Spring MVC @Controller programming model --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;annotation-driven</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&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;uk.co.eggsylife.controller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>We&#8217;re informing Spring to scan the &#8216;uk.co.eggsylife.controller&#8217; package. The <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml" target="_blank">complete servlet-context.xml</a> can be seen online.</p>
<h3>The configuration XML</h3>
<p>For the purpose of this tutorial, the application lists students and allows the user to add students. The &#8216;students&#8217; bean acts as an in-memory database that is added to at runtime. It is a simple <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html" target="_blank">Java ArrayList</a> of Student model objects. Typically you will have your own model objects and different <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD operations</a> on those objects.</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;students&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;java.util.ArrayList&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 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;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;uk.co.eggsylife.persistent.Student&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;studentNumber&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;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;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;firstName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;John&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;lastName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Smith&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;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;uk.co.eggsylife.persistent.Student&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;studentNumber&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&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;firstName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Jane&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;lastName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Smith&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;/constructor-arg<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>Within the application when a Student is added we also validate the details entered. A simple <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/java/uk/co/eggsylife/validation/StudentValidator.java" target="_blank">Student Validator</a> is created and referenced from the configuration 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;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;studentValidator&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;uk.co.eggsylife.validation.StudentValidator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Finally we tell Spring the location of our ValidationMessages message properties file. If the user enter any details incorrectly we can raise an error for the field entered and specify a message code for the error. Spring will then look up that message code from the <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/resources/ValidationMessages.properties" target="_blank">ValidationMessages.properties</a> 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;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;messageSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.context.support.ReloadableResourceBundleMessageSource&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;basenames&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>classpath:ValidationMessages<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;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cacheSeconds&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</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>60<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;/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 <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/spring/app-config.xml" target="_blank">app-config is now completed</a>.</p>
<h3>Tiles setup and JSP file</h3>
<p>Earlier in the tutorial we referenced the location of the Tile definitions, &#8216;/WEB-INF/tile-defs/tile-defs.xml&#8217;. This file declares all the views the web application uses and is crucial to making Spring JS and partial rendering function correctly.</p>
<p>Firstly we define an overall template that can be be used for any views our application requires. Declaring a template allows you to re-use the styling across multiple views.</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;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;template.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/templates/template.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;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/blank.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></pre></div></div>

<p>The &#8216;template.jsp&#8217; file is pretty basic. It simple declares the HTML headers. Each time we change the &#8216;content&#8217; of this Tile definition it will continue to be surrounded by the markup on the template.jsp.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
        &lt;head&gt;
                &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;c:url value=&quot;/resources/dojo/dojo.js&quot; /&gt;&quot;&gt; &lt;/script&gt;
                &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;c:url value=&quot;/resources/spring/Spring.js&quot; /&gt;&quot;&gt; &lt;/script&gt;
                &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;c:url value=&quot;/resources/spring/Spring-Dojo.js&quot; /&gt;&quot;&gt; &lt;/script&gt;
                &lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;&lt;c:url value='/resources/dijit/themes/tundra/tundra.css' /&gt;&quot; /&gt;
&nbsp;
                &lt;title&gt;Spring-JS Edit Dialog and Partial JS Example&lt;/title&gt;
&nbsp;
        &lt;/head&gt;
&nbsp;
        &lt;body class=&quot;tundra&quot;&gt;
&nbsp;
                &lt;p&gt;Spring Edit Dialog and Partial Content Update Tutorial&lt;/p&gt;
&nbsp;
                &lt;tiles:insertAttribute name=&quot;content&quot; /&gt;
        &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>This means that every page will have the title set as above and every page will have the &#8216;Spring Edit&#8230;..&#8217; paragraph present. Also note we are including the various Spring required Javascript files. By default they are located in the /resources/ folder of the web application. They are required in order to make Spring-JS Ajax functions work.</p>
<p>In this example I have also included the Dojo Tundra theme to improve the look and feel of the sample application but this is by no means required.</p>
<p>The tutorial only has one screen which lists students and should look similar to figure 1.</p>
<div id="attachment_313" class="wp-caption aligncenter" style="width: 160px"><a href="http://eggsylife.co.uk/wp-content/uploads/2010/11/studentsList.jpg"><img src="http://eggsylife.co.uk/wp-content/uploads/2010/11/studentsList-150x150.jpg" alt="" title="studentsList" width="150" height="150" class="size-thumbnail wp-image-313" /></a><p class="wp-caption-text">Figure 1</p></div>
<p>This screen simply lists the students and provides a link for the user to add a new student. When a new student is added we don&#8217;t navigate away from the page we simply append the student to the bottom of the list.</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;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;students.view&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;template.view&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;content&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;studentsList.view&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;studentsList.view&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/WEB-INF/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;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentsList&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/studentList.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;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentEditDialog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/util/dialogClose.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></pre></div></div>

<p>We firstly declare the overall &#8216;students.view&#8217; which extends our default template as mentioned earlier. This view is actually a template itself and requires two entries &#8211; a list of students and a student edit dialog. The &#8216;students.jsp&#8217; file is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ include file=&quot;/WEB-INF/pages/include.jsp&quot; %&gt;
&nbsp;
&lt;tiles:insertAttribute name=&quot;studentsList&quot; /&gt;
&nbsp;
&lt;tiles:insertAttribute name=&quot;studentEditDialog&quot; /&gt;</pre></div></div>

<p>The &#8216;students.jsp&#8217; doesn&#8217;t contain much other than the inclusion of the two areas of content. &#8216;studentList&#8217; and &#8216;studentEditDialog&#8217;. First of all lets look at the &#8216;studentList.jsp&#8217; file which we inject into the &#8216;studentsList&#8217; attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="html4" style="font-family:monospace;">&lt;%@ include file=&quot;/WEB-INF/pages/include.jsp&quot; %&gt;
&nbsp;
&nbsp;
&lt;div id=&quot;studentsList&quot;&gt;
	&lt;table border=&quot;1&quot; width=&quot;600px&quot;&quot;&gt;
		&lt;thead&gt;
			&lt;tr&gt;
				&lt;th width=&quot;33%&quot; align=&quot;left&quot;&gt;
					Student ID
				&lt;/th&gt;
				&lt;th width=&quot;33%&quot; align=&quot;left&quot;&gt;
					First name
				&lt;/th&gt;
				&lt;th width=&quot;33%&quot; align=&quot;left&quot;&gt; 
					Last name
				&lt;/th&gt;
			&lt;/tr&gt;
		&lt;/thead&gt;
		&lt;tbody&gt;
			&lt;c:forEach var=&quot;student&quot; items=&quot;${students}&quot;&gt;
				&lt;tr&gt;
					&lt;td&gt;
						${student.studentNumber}
					&lt;/td&gt;
					&lt;td&gt;
						${student.firstName}
					&lt;/td&gt;
					&lt;td&gt;
						${student.lastName}
					&lt;/td&gt;
				&lt;/tr&gt;
			&lt;/c:forEach&gt;
		&lt;/tbody&gt;
	&lt;/table&gt;
&nbsp;
	&lt;a href=&quot;&lt;c:url value='/students/edit' /&gt;&quot; id=&quot;createStudentLink&quot;&gt;
		Create new student
	&lt;/a&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		dojo.addOnLoad(function() {
			Spring.addDecoration(new Spring.AjaxEventDecoration({
				elementId: &quot;createStudentLink&quot;,
				event: &quot;onclick&quot;,
				popup:true,
				params: {fragments: &quot;studentsList, studentEditDialog&quot;}
			}));
		});
	&lt;/script&gt;
&nbsp;
&lt;/div&gt;</pre></div></div>

<p>The view simply lists each student in a table and provides a link to be able to add students. There are two main things to note on this JSP. Firstly notice that it is wrapped in a &#8216;DIV&#8217; element with the same id as the tiles attribute defintion, &#8216;studentsList&#8217;. This is crucial to making Spring partial refresh work. Also note the way in which we &#8216;decorate&#8217; the &#8216;Create new student&#8217; link with Spring Javascript.</p>
<p>Firstly we declare the &#8216;dojo.addOnLoad&#8217; function. This means that this piece of Javascript will execute when the page loads. Next we use the &#8216;Spring.addDecoration&#8217; method to add the Ajax decorations. This function takes a number of parameters. Firstly the &#8216;elementId&#8217; This is the id of the element that we attach the decoration to. In this case we are attaching the decoration to the &#8216;Create new student&#8217; link. The event is then specified. We want the Ajax actions to run when the link is clicked so we specify the &#8216;onclick&#8217; event. Next we set the &#8216;popup&#8217; variable to &#8216;true&#8217;. This means that whatever the response is we want it rendered as a popup dialog.</p>
<p>The final parameter is &#8216;fragments&#8217;. This identifies which areas of content we want refresh. We state that we want both the &#8216;studentsList&#8217; and the &#8216;studentEditDialog&#8217; updated. Notice the names match our tiles attributes and that we wrap the &#8216;studentsList.jsp&#8217; in a DIV with the id of &#8216;studentsList&#8217;. This way Spring knows how to identify content and replace it.</p>
<p>Now lets look at the &#8216;studentEditDialog&#8217; content. In this instance we are injecting a utility JSP file that closes any open dialogs. The JSP is basically a bit of Javascript that closes any open Dojo popup dialogs. When first listing the students it is not particularly useful as we haven&#8217;t got any open dialogs. However when we save a new student a redirect to the students list view the JSP&#8217;s use will become clear.</p>

<div class="wp_syntax"><div class="code"><pre class="html4" style="font-family:monospace;">&lt;!-- This is a utility JSP that closes any Dojo Modal dialogs --&gt;
&lt;script type=&quot;text/javscript&quot;&gt;
dojo.query(&quot;.dijitDialog&quot;).forEach(function(element) {
	dijit.byId(element.id).hide();
});
&lt;/script&gt;</pre></div></div>

<p>The last tile definition is the view that contains our full student edit dialog. The dialog allows users to create a new student, asking for the student name and id number.</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;definition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentsListAndEditDialog.view&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;studentsList.view&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentsList&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/blank.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;put-attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;studentEditDialog&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/studentEditDialog.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></pre></div></div>

<p>The only difference with this definition is the that we switch out the &#8216;dialogClose.jsp&#8217; with the actual student edit dialog. The student edit dialog is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="html5" style="font-family:monospace;">&lt;%@ include file=&quot;/WEB-INF/pages/include.jsp&quot; %&gt;
&nbsp;
&nbsp;
&lt;div id=&quot;studentEditDialog&quot;&gt;
	&lt;p&gt;Student Edit Dialog&lt;/p&gt;
&nbsp;
	&lt;form:form id=&quot;studentForm&quot; modelAttribute=&quot;student&quot;&gt;
		&lt;table&gt;
			&lt;tr&gt;
				&lt;td&gt;Student number&lt;/td&gt;
				&lt;td&gt;
					&lt;form:input path=&quot;studentNumber&quot; /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;First name&lt;/td&gt;
				&lt;td&gt;
					&lt;form:input path=&quot;firstName&quot; /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td align=&quot;center&quot; colspan=&quot;2&quot; style=&quot;color: red; font-size: 0.8em;&quot;&gt;
					&lt;form:errors path=&quot;firstName&quot; /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;Last name&lt;/td&gt;
				&lt;td&gt;
					&lt;form:input path=&quot;lastName&quot; /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td align=&quot;center&quot; colspan=&quot;2&quot; style=&quot;color: red; font-size: 0.8em;&quot;&gt;
					&lt;form:errors path=&quot;lastName&quot; /&gt;
				&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/table&gt;
		&lt;input type=&quot;submit&quot; id=&quot;saveStudentButton&quot; value=&quot;Save student&quot; /&gt;
	&lt;/form:form&gt;
&nbsp;
	&lt;script type=&quot;text/javascript&quot;&gt;
		dojo.addOnLoad(function() {
				Spring.addDecoration(new Spring.AjaxEventDecoration({
    				elementId:'saveStudentButton', 
    				event:'onclick',
    				formId:'studentForm',
    				params: {fragments: &quot;studentsList,studentEditDialog&quot;}
    			}));
		});
	&lt;/script&gt;
&lt;/div&gt;</pre></div></div>

<div id="attachment_315" class="wp-caption aligncenter" style="width: 160px"><a href="http://eggsylife.co.uk/wp-content/uploads/2010/11/popup.jpg"><img src="http://eggsylife.co.uk/wp-content/uploads/2010/11/popup-150x150.jpg" alt="" title="studentPopupDialog" width="150" height="150" class="size-thumbnail wp-image-315" /></a><p class="wp-caption-text">Figure 2</p></div>
<p>Again, notice we wrap the content in a DIV element with the same id as our tiles attribute, &#8216;studentEditDialog&#8217;. We then go on to declare a <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring-form.tld.html" target="_blank">Spring form</a>. For now I won&#8217;t go on to discuss the Spring MVC and form bindings. Finally note we attach the Spring decorations again. The only difference is this time we specify a form id and remove the popup attribute. We attach the code to the &#8216;Save student&#8217; button which is the submit button for the &#8216;studentForm&#8217;. This means that we want our Ajax events to be actioned when the form is submitted.</p>
<p>That concludes our view definitions. The <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/tile-defs/tile-defs.xml" target="_blank">tiles-defs.xml</a>, <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/pages/students.jsp" target="_blank">students.jsp</a>, <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/pages/studentList.jsp" target="_blank">studentsList.jsp</a>, <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/pages/util/dialogClose.jsp" target="_blank">dialogClose.jsp</a> and <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/webapp/WEB-INF/pages/studentEditDialog.jsp" target="_blank">studentEditDialog.jsp</a>.</p>
<h3>Java Controllers</h3>
<p>The tutorial only has one controller. The &#8216;StudentController&#8217; handles the three primary functions of the application. It handles the showing of the students list (GET), the showing of the edit dialog (GET) and the adding of a student (POST). </p>
<p>The list of students is handled by the &#8216;showStudentsList&#8217; method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> STUDENTS_VIEW_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;students.view&quot;</span><span style="color: #339933;">;</span>
&nbsp;
@Autowired
<span style="color: #000000; font-weight: bold;">protected</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>
&nbsp;
&nbsp;
@RequestMapping<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/students&quot;</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;">public</span> <span style="color: #003399;">String</span> showStudentsList<span style="color: #009900;">&#40;</span>ModelMap modelMap<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	modelMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;students&quot;</span>, students<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> STUDENTS_VIEW_KEY<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It simply adds our list of students to the view and returns the &#8216;students.view&#8217; tile definition. The list of students is Autowired from our Spring app-config file as defined earlier. It is wired to handle the URL &#8216;/students&#8217;.</p>
<p>Secondly there is the &#8216;showStudentEditForm&#8217; method. This handles the URL &#8216;/students/edit&#8217; and is called when the user clicks the &#8216;Create new student&#8217; link.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> STUDENT_EDIT_DIALOG_VIEW_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;studentsListAndEditDialog.view&quot;</span><span style="color: #339933;">;</span>
&nbsp;
@RequestMapping<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/students/edit&quot;</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;">public</span> <span style="color: #003399;">String</span> showStudentEditForm<span style="color: #009900;">&#40;</span>ModelMap modelMap<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Object class will be used as our</span>
	<span style="color: #666666; font-style: italic;">// form backing object</span>
	Student student <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Student<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	modelMap.<span style="color: #006633;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;student&quot;</span>, student<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">return</span> STUDENT_EDIT_DIALOG_VIEW_KEY<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It returns the &#8216;studentsListAndEditDialog.view&#8217; and adds a new &#8216;Student&#8217; object to the view for our form. This object is used as the form backing object. The Student object is a POJO and will be seen later. Finally we have the &#8216;saveStudentEdit&#8217; method that is actioned when the form is submitted. If the form is submitted without errors then it simply returns the same view as the listing of the students. If the user submits the form and it contains errors then it returns the edit form view with details of the error.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
@Autowired
<span style="color: #000000; font-weight: bold;">protected</span> StudentValidator studentValidator <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
@RequestMapping<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/students/edit&quot;</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;">public</span> <span style="color: #003399;">String</span> saveStudentEdit<span style="color: #009900;">&#40;</span>@ModelAttribute Student student, BindingResult errors, ModelMap modelMap<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	studentValidator.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span>student, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> errors.<span style="color: #006633;">hasErrors</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: #000000; font-weight: bold;">return</span> STUDENT_EDIT_DIALOG_VIEW_KEY<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>
		students.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>student<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		modelMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;students&quot;</span>, students<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> STUDENTS_VIEW_KEY<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice if the form doesn&#8217;t have errors we add the student object back into the collection of students and update the list of students in the view. This is important so that the list of students is updated with the student that has just been added. The utility JSP that closes any open Dojo modal dialogs now comes into full use as it makes sure that the edit student dialog is closed after a student has been successfully added. The method responds to the POST request method so it is actioned when our form is &#8216;POSTED&#8217;. The complete <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/java/uk/co/eggsylife/controller/StudentController.java" target="_blank">StudentController</a> includes all the above methods.</p>
<h3>The Validator and Student POJO</h3>
<p>To wrap things up the final two Java classes the application uses is the <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/java/uk/co/eggsylife/validation/StudentValidator.java" target="_blank">StudentValidator</a> and the POJO <a href="https://github.com/eggsy84/SpringPopupRefreshList/blob/master/src/main/java/uk/co/eggsylife/persistent/Student.java" target="_blank">Student</a> object.</p>
<p>The validator simply checks that the user has entered both a first name and last name. The Student object encapsulates items a student has.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StudentValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> supports<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> clazz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Student.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>clazz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> validate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj, Errors errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Student student <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Student<span style="color: #009900;">&#41;</span>obj<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> student.<span style="color: #006633;">getFirstName</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: #339933;">||</span> student.<span style="color: #006633;">getFirstName</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> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			errors.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;firstName&quot;</span>, <span style="color: #0000ff;">&quot;student.first.name.empty&quot;</span><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;">if</span><span style="color: #009900;">&#40;</span> student.<span style="color: #006633;">getLastName</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: #339933;">||</span> student.<span style="color: #006633;">getLastName</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> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			errors.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;lastName&quot;</span>, <span style="color: #0000ff;">&quot;student.last.name.empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> studentNumber <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> <span style="color: #003399;">String</span> firstName <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> <span style="color: #003399;">String</span> lastName <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;">Long</span> getStudentNumber<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> studentNumber<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStudentNumber<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> studentNumber<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;">studentNumber</span> <span style="color: #339933;">=</span> studentNumber<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFirstName<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> firstName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFirstName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> firstName<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;">firstName</span> <span style="color: #339933;">=</span> firstName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLastName<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> lastName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> lastName<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;">lastName</span> <span style="color: #339933;">=</span> lastName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Conclusion</h3>
<p>That should have gave you a complete example on how to use Spring-JS alongside Spring MVC and perform partial refresh of the content. The key to understanding the tutorial is understanding the tie between between the tiles defintions, the identified DIV elements and the usage of Javascript to link the two together.</p>
<p>The project can be <a href="https://github.com/eggsy84/SpringPopupRefreshList/zipball/master" target="_blank">downloaded as a Zip</a> from <a href="http://www.github.com" target="_blank">GitHub</a>. For those familiar with Git you can also get the project from the Git Repository under the URL &#8216;git://github.com/eggsy84/SpringPopupRefreshList.git&#8217;. If anyone would like to comment or have anything to contribute to the codebase please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/11/08/spring-js-ajax-popup-with-list-refresh/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Code re-use</title>
		<link>http://eggsylife.co.uk/2010/11/07/code-re-use/</link>
		<comments>http://eggsylife.co.uk/2010/11/07/code-re-use/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 19:48:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=324</guid>
		<description><![CDATA[Just read an interesting article on code re-use and over engineering. Some interesting points to bear in mind when developing software for a commercial profit making organisation. http://java.dzone.com/news/reuse-myth-can-you-afford]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/11/07/code-re-use/" size="medium" count="true"></div></div><p>Just read an interesting article on code re-use and over engineering.</p>
<p>Some interesting points to bear in mind when developing software for a commercial profit making organisation.</p>
<p><a href="http://java.dzone.com/news/reuse-myth-can-you-afford" target="_blank">http://java.dzone.com/news/reuse-myth-can-you-afford</a></p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2010/11/07/code-re-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/04/22/gwt-2-jsonp-and-javascript-overlays-with-jsonprequestbuilder/" size="medium" count="true"></div></div><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>15</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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/02/03/spring-annotation-based-aop-and-intercepting-the-ball/" size="medium" count="true"></div></div><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>5</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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/01/22/spring-js-lack-of-an-api-or-documentation/" size="medium" count="true"></div></div><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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2010/01/03/spring-3-restful-web-services/" size="medium" count="true"></div></div><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 &#8216;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>11</slash:comments>
		</item>
		<item>
		<title>Eclipse Galileo – 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 16:52:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software development]]></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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2009/12/20/eclipse-galileo-cannot-nest-src-folder/" size="medium" count="true"></div></div><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>8</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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/" size="medium" count="true"></div></div><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>19</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 [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2009/11/12/ajax-spring-forms-with-annotations-and-jquery/" size="medium" count="true"></div></div><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>
	</channel>
</rss>

