<?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 &#187; Spring Framework</title>
	<atom:link href="http://eggsylife.co.uk/category/spring_framework/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.2.1</generator>
		<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>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>9</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>18</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>
		<item>
		<title>Spring annotations User Principal</title>
		<link>http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/</link>
		<comments>http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 16:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring annotations]]></category>
		<category><![CDATA[spring form]]></category>
		<category><![CDATA[user principal]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=147</guid>
		<description><![CDATA[In case anyone was wondering after reading the previous Spring Annotations guide you can also user various parameters to get hold of the current logged in user. To get hold of the user principal simply add a new parameter to your Request annotated methods called: org.springframework.web.context.request.WebRequest myWebRequest Using the previous tutorial as an example your [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2009/11/10/spring-annotations-user-principal/" size="medium" count="true"></div></div><p>In case anyone was wondering after reading the previous Spring Annotations guide you can also user various parameters to get hold of the current logged in user.</p>
<p>To get hold of the user principal simply add a new parameter to your Request annotated methods called:</p>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p><strong>Testing&#8230;</strong></p>
<p>Phew that should be it &#8211; assuming you have setup your application the same as mine and have configured your server environment such as Tomcat if you visit http://localhost:8080/annotations/processName.page you should see the form. Try submitting the form and you should see the response as dictated by our business logic.</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2009/11/06/spring-annotations-and-forms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Hibernate, Spring, Google Web Toolkit &#8211; Part Four</title>
		<link>http://eggsylife.co.uk/2008/09/05/hibernate-spring-google-web-toolkit-part-four/</link>
		<comments>http://eggsylife.co.uk/2008/09/05/hibernate-spring-google-web-toolkit-part-four/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 09:55:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Google Web Toolkit]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[gwt tomcat setup]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring hibernate]]></category>
		<category><![CDATA[tomcat configuration]]></category>
		<category><![CDATA[web.xml]]></category>

		<guid isPermaLink="false">http://eggsylife.co.uk/?p=26</guid>
		<description><![CDATA[A few people have now requested the final step in the tutorial &#8211; making the application a true web app and how to setup the web.xml etc. So thought it time to pull my finger out of my &#8230; and get it done. This tutorial is based upon the previous tutorials in this work and [...]]]></description>
			<content:encoded><![CDATA[<div class="none"><div class="g-plusone" data-href="http://eggsylife.co.uk/2008/09/05/hibernate-spring-google-web-toolkit-part-four/" size="medium" count="true"></div></div><p>A few people have now requested the final step in the tutorial &#8211; making the application a true web app and how to setup the web.xml etc.</p>
<p>So thought it time to pull my finger out of my &#8230; and get it done.</p>
<p>This tutorial is based upon the previous tutorials in this work and is performed using Tomcat version 5.5. I think other tomcat versions should matter but dont quote me on that.</p>
<p>Firstly my sample context is as follows:</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;Context</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/theproject&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">docBase</span>=<span style="color: #ff0000;">&quot;/Users/james/Documents/workspace/TheProject&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">workDir</span>=<span style="color: #ff0000;">&quot;/Users/james/Documents/workspace/TheProjectWorkDir&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">reloadable</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">crossContext</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Logger</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.apache.catalina.logger.FileLogger&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;localhost_theproject_log.&quot;</span> <span style="color: #000066;">suffix</span>=<span style="color: #ff0000;">&quot;.txt&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">timestamp</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Context<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Thats stuff most should be comfortable with. On my setup I place my context file in CATALINA_HOME/conf/Catalina/localhost/</p>
<p>The next step is to compile up our GWT it Javascript.</p>
<p>Run your GWT application in hosted mode. </p>
<p>Then when ready click &#8216;Compile&#8217; on the GWT hosted mode browser. If you have the same launch/compile/shell scripts as the ones I defined in Part Two of the tutorial then you should get a new folder within your project titled <span style="font-weight:bold;"><span style="font-style:italic;">&#8216;www&#8217;</span></span>.</p>
<p>The next step is to make your web.xml and put it in the location /WEB-INF/web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee</span>
<span style="color: #009900;">   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;
&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>The Project<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;
    <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>theproject<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>theproject<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*.page<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- GWT Servlets   --&gt;</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>
        <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>teachers<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>com.company.server.gwt.TeacherServiceImpl<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;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <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>teachers<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>/www/com.company.MyApplication/teachers<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;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>webAppRootKey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>theproject.root<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-timeout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-timeout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The Usual Welcome File List --&gt;</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;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>
&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>A few important points in the web.xml are firtly notice the spring elements and I&#8217;m mapping anything with .page to go through my Spring controller.</p>
<p>Then also notice the GWT mappings &#8211; we&#8217;re using the url of teachers as defined in our RPC&#8217;s and pointing this at that www folder we made!! Easy so far!</p>
<p>Next we need to make the Spring servlet that handles the Spring controllers for sending back to the user a view. The name of the file is very important mine is called theproject-servlet.xml because this is the name used in my web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&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;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;">xmlns:tx</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/tx&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">       http://www.springframework.org/schema/beans classpath:spring-beans-2.0.xsd</span>
<span style="color: #009900;">       http://www.springframework.org/schema/tx classpath:spring-tx-2.0.xsd</span>
<span style="color: #009900;">       http://www.springframework.org/schema/aop classpath:spring-aop-2.0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;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: #808080; font-style: italic;">&lt;!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet --&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;handlerMapping&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&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;mappings&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>
                 /**/help.page=simpleViewController
                 /home.page=homepageController                
            <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>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- List of controllers for the application --&gt;</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;simpleViewController&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.mvc.UrlFilenameViewController&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: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;homepageController&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.company.server.web.controlle.HomepageController&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;successView&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;home.tile&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;exceptionView&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;exception.tile&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;
&nbsp;
 <span style="color: #808080; font-style: italic;">&lt;!-- Convention based ViewResolver for Tiles2 --&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;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>
&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>

<p>A few more important parts here &#8211; firstly the definition of a tiles file. Those familar with tiles should understand this. This is where we shall define all our pages. Secondly you can see our mappings &#8211; in this case home.page. They are handled by controllers. Notice the controller homepageController we&#8217;ll go ahead and define that class now:</p>
<p>Firstly in good Object oriented design we can make an abstract controller that all our controllers can extends for ease.</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.company.server.web.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.mvc.BaseCommandController</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> ParameterisedViewCommandController <span style="color: #000000; font-weight: bold;">extends</span> BaseCommandController
<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> successView <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> exceptionView <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
  * @return the successView
  */</span>
 <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getSuccessView <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> successView<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
  * @param successView the successView to set
  */</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSuccessView <span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span> successView <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;">successView</span> <span style="color: #339933;">=</span> successView<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
  * @return the exceptionView
  */</span>
 <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getExceptionView <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> exceptionView<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
  * @param exceptionView the exceptionView to set
  */</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setExceptionView <span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span> exceptionView <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;">exceptionView</span> <span style="color: #339933;">=</span> exceptionView<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice the basic strings exception and success view that we defined in our servlet.xml file.</p>
<p>Next just the basic implementation of our defined HomepageController.</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.company.server.web.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<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;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HomepageController <span style="color: #000000; font-weight: bold;">extends</span> ParameterisedViewCommandController
<span style="color: #009900;">&#123;</span>
&nbsp;
 @Override
 <span style="color: #000000; font-weight: bold;">protected</span> ModelAndView handleRequestInternal<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
 <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span>getSuccessView<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;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this case we just return a new view of the success view but you can obviously return various views. Also those familiar with struts actions the classes above are basically those.</p>
<p>Now go ahead and make the tiles files defined in the servlet. Create a file as specified in location /WEB-INF/tiles-def/templates.xml and in here we can define our templates. Mine if 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;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;home.tile&quot;</span> <span style="color: #000066;">template</span>=<span style="color: #ff0000;">&quot;/pages/home.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;title&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;The Project&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;gwtjs&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/theproject/www/com.company.MyApplication/com.company.MyApplication.nocache.js&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>The tile definition probably needs some explaing. Firstly I&#8217;m using JSTL to do my various important inserts such as the title of the page and the insert of my GWT compiled up javascript. Again still referencing that www folder and our javascript.</p>
<p>Right the final step is to make that page so make a folder in the root of your project called pages and make a file called home.jsp.</p>
<p>My page is below:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;%@ taglib uri=&quot;http://tiles.apache.org/tags-tiles&quot; prefix=&quot;tiles&quot; %&gt;
&nbsp;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html&gt;
&nbsp;
 &lt;head&gt;
  &lt;title&gt;&lt;tiles:getAsString name=&quot;title&quot; /&gt;&lt;/title&gt;
&nbsp;
  &lt;!-- Include the GWT Javascript --&gt;
  &lt;script language='javascript' src='&lt;tiles:getAsString name=&quot;gwtjs&quot; /&gt;'&gt;&lt;/script&gt;
 &lt;/head&gt;
&nbsp;
&nbsp;
 &lt;body&gt;
&nbsp;
  &lt;h1&gt;MyApplication&lt;/h1&gt;
&nbsp;
  &lt;p&gt;
   Example GWT application calling RPC calls
  &lt;/p&gt;
&nbsp;
  &lt;div id=&quot;database&quot;&gt;&lt;/div&gt;
&nbsp;
 &lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>The important areas of the page are firstly the tiles inserts &#8211; where we insert of GWT javascript and also the content of the page between the body tags. The HTML is basically exactly the same as the hosted page. Our application looks for a &#8220;database&#8221; div to replace and such this element needs to be on the page!!</p>
<p>That should wrap everything up now! Also as I have migrated across to the latest versions of struts/spring/tiles etc you can <a href="http://www.fileshack.us/get_file.php?id=863543&#038;file=lib.zip">download a copy of my lib directory</a> so that the above tutorial should work.</p>
<p>Any questions please post a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://eggsylife.co.uk/2008/09/05/hibernate-spring-google-web-toolkit-part-four/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

