<?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>Webnoob &#187; Web Development</title>
	<atom:link href="http://www.webnoob.com/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webnoob.com</link>
	<description>I am a noob on the World Wide Web</description>
	<lastBuildDate>Sun, 06 Sep 2009 06:12:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sping Framework &#8211; step by step tutorial (2)</title>
		<link>http://www.webnoob.com/2009/06/spring-tutorial-2/</link>
		<comments>http://www.webnoob.com/2009/06/spring-tutorial-2/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 08:25:41 +0000</pubDate>
		<dc:creator>NDLoc</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.webnoob.com/?p=151</guid>
		<description><![CDATA[Part 2 – Developing and Configuring the Application
his is Part 2 of a step-by-step account of how to develop a web application from scratch using the Spring Framework. In Part 1 (Steps 1 – 12) we configured the environment and set up a basic application that we will build upon.
This is what we have to [...]]]></description>
			<content:encoded><![CDATA[<h3>Part 2 – Developing and Configuring the Application</h3>
<p>his is Part 2 of a step-by-step account of how to develop a web application from scratch using the Spring Framework. In Part 1 (Steps 1 – 12) we configured the environment and set up a basic application that we will build upon.</p>
<p>This is what we have to start with.</p>
<ul>
<li>An introduction page index.jsp.</li>
<li> A DispatcherServlet with a corresponding springapp-servlet.xml configuration file.</li>
<li> A controller springappController.java.</li>
<li>A view hello.jsp.</li>
</ul>
<p>We will now improve on these parts to build a more useful application.<span id="more-151"></span></p>
<p><strong>Step 13 – Improve index.jsp</strong></p>
<p>We will make use of JSP Standard Tag Library (JSTL) so I will start by copying the JSTL files we need to our WEB-INF/lib directory. Copy jstl.jar from the 'spring-framework-1.2/lib/j2ee' directory and standard.jar from the 'spring-framework-1.2/lib/jakarta-taglibs' directory to the springapp/war/WEB-INF/lib directory. I am also creating a “header” file that will be included in every JSP page that I'm going to write. This will make development easier and I will be sure that I have the same definitions in all JSPs. I am going to put all JSPs in a directory named jsp under the WEB-INF directory. This will ensure that only the controller has access to the views - it is not possible to get to these pages by entering them directly as a URL in the browser. This strategy might not work in all application servers and if this is the case with the one you are using, just move the jsp directory up a level. You would then use springapp/war/jsp as the directory instead of springapp/war/WEB-INF/jsp in all the code examples that will follow.<br />
<em><strong><br />
springapp/war/WEB-INF/jsp/include.jsp</strong></em></p>
<div class="igBar"><span id="ljava-14"><a href="#" onclick="javascript:showPlainTxt('java-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-14">
<div class="java">&amp;lt;%@ page session=<span style="color: #ff0000;">"false"</span>%&amp;gt;<br />
&amp;lt;%@ taglib prefix=<span style="color: #ff0000;">"c"</span> uri=<span style="color: #ff0000;">"http://java.sun.com/jstl/core"</span> %&amp;gt;<br />
&amp;lt;%@ taglib prefix=<span style="color: #ff0000;">"fmt"</span> uri=<span style="color: #ff0000;">"http://java.sun.com/jstl/fmt"</span> %&amp;gt;</div>
</div>
</div>
<p></p>
<p>Now we can change index.jsp to use this include and since we are using JSTL we can use the  tag for redirecting to our Controller. This ties the index.jsp into our application framework.</p>
<p><em><strong>springapp/war/index.jsp</strong></em></p>
<div class="igBar"><span id="ljava-15"><a href="#" onclick="javascript:showPlainTxt('java-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-15">
<div class="java">&amp;lt;%@ include file=<span style="color: #ff0000;">"/WEB-INF/jsp/include.jsp"</span> %&amp;gt;</p>
<p>&amp;lt;%-- Redirected because we can<span style="color: #ff0000;">'t set the welcome page to a virtual URL. --%&amp;gt; </span></div>
</div>
</div>
<p></p>
<p><strong>Step 14 – Improve the view and the controller</strong></p>
<p>I am going to move the view hello.jsp to the WEB-INF/jsp directory. The same include that was added to index.jsp gets added to hello.jsp. I also add the current date and time as output that I will retrieve from the model, passed to the view, using the JSTL  tag.</p>
<p><em><strong>springapp/war/WEB-INF/jsp/hello.jsp</strong></em></p>
<div class="igBar"><span id="ljava-16"><a href="#" onclick="javascript:showPlainTxt('java-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-16">
<div class="java">&lt;%@ include file=<span style="color: #ff0000;">"/WEB-INF/jsp/include.jsp"</span> %&gt;</p>
<p>&lt;html&gt;<br />
&lt;head&gt;&lt;title&gt;Hello :: <span style="color: #006600;">Spring</span> Application&lt;/title&gt;&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Hello - Spring Application&lt;/h1&gt;<br />
&lt;p&gt;Greetings, it is now &lt;c:out value=<span style="color: #ff0000;">"${now}"</span>/&gt;<br />
&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
</div>
</div>
<p></p>
<p>For SpringappController.java there are a few changes we need to make. Change the view to WEB-INF/jsp/hello.jsp since we moved the file to this new location. Also add a string containing the current data and time as the model.</p>
<p><em><strong>springapp/src/SpringappController.java</strong></em></p>
<div class="igBar"><span id="ljava-17"><a href="#" onclick="javascript:showPlainTxt('java-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-17">
<div class="java"><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span></p>
<p><span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></p>
<p><span style="color: #a1a100;">import java.io.IOException;</span></p>
<p><span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringappController <span style="color: #000000; font-weight: bold;">implements</span> Controller <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #808080; font-style: italic;">/** Logger for this class and subclasses */</span><br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> now = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"returning hello view with "</span> + now<span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"WEB-INF/jsp/hello.jsp"</span>, <span style="color: #ff0000;">"now"</span>, now<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Now we are ready to try this after we build and deploy this new code. We enter http://localhost:8080/springapp in a browser and that should pull up index.jsp, which should redirect to hello.htm, which in turn gets us to the controller that sends the data and time to the view.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-152" title="spring_3" src="http://www.webnoob.com/wp-content/uploads/2009/06/spring_3.jpg" alt="spring_3" width="540" height="374" /></p>
<p><strong>Step 15 – Decouple the view and the controller</strong></p>
<p>Right now the controller specifies the full path of the view, which creates an unnecessary dependency between the controller and the view. Ideally we would like to map to the view using a logical name, allowing us to switch the view without having to change the controller. You can set this mapping in a properties file if you like using a ResourceBundleViewResolver and a SimpleUrlHandlerMapping class. If your mapping needs are simple it is easier to just set a prefix and a suffix on the InternalResourceViewResolver. The latter approach is the one that I will implement now, so I modify the springapp-servlet.xml and include this viewResolver entry. I have elected to use a JstlView which will enable us to use JSTL in combination with message resource bundles and it will also support internationalization.</p>
<p><em><strong>springapp/war/WEB-INF/springapp-servlet.xml</strong></em></p>
<div class="igBar"><span id="lxml-18"><a href="#" onclick="javascript:showPlainTxt('xml-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-18">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span></p>
<p><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--<br />
&nbsp; - Application context definition for &quot;springapp&quot; DispatcherServlet.<br />
&nbsp; --&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"springappController"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"SpringappController"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"urlMapping"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"mappings"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"/hello.htm"</span><span style="font-weight: bold; color: black;">&gt;</span></span>springappController<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/prop<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"viewResolver"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"viewClass"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.servlet.view.JstlView<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"prefix"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>/WEB-INF/jsp/<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"suffix"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>.jsp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>So now I can remove the prefix and suffix from the view name in the controller.<br />
<em><strong>springapp/src/SpringappController.java</strong></em></p>
<div class="igBar"><span id="ljava-19"><a href="#" onclick="javascript:showPlainTxt('java-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-19">
<div class="java"><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span></p>
<p><span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></p>
<p><span style="color: #a1a100;">import java.io.IOException;</span></p>
<p><span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringappController <span style="color: #000000; font-weight: bold;">implements</span> Controller <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #808080; font-style: italic;">/** Logger for this class and subclasses */</span><br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> now = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"returning hello view with "</span> + now<span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"hello"</span>, <span style="color: #ff0000;">"now"</span>, now<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Compile and deploy and the application should still work.</p>
<p><strong>Step 16 – Add some classes for business logic</strong></p>
<p>So far our application is not very useful. I would like to add a little bit of business logic in form of a Product class and a class that will manage all the products. I name this management class ProductManager. In order to separate the web dependent logic from the business logic I will create two separate packages for the Java source – web and bus. If this was an application for a real company I would name the packages something like com.mycompany.web and com.mycompany.bus, but since this is just a demo application I will keep the package names real short. The Product class is implemented as a JavaBean – it has the default constructor (automatically provided if we don't specify any constructors) and getters and setters for the two instance variables description and price. I also make it Serializable, not necessary for our application, but could come in handy later on if we have to pass this class between different application layers.</p>
<p><strong><em>springapp/src/bus/Product.java</em></strong></p>
<div class="igBar"><span id="ljava-20"><a href="#" onclick="javascript:showPlainTxt('java-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-20">
<div class="java">package bus;</p>
<p><span style="color: #a1a100;">import java.io.Serializable;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?q=allinurl%3ASerializable+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> description;<br />
<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3ADouble+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Double</span></a> price;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setDescription<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> s<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
description = s;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> description;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setPrice<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ADouble+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Double</span></a> d<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
price = d;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3ADouble+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Double</span></a> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> price;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>The ProductManager holds a List of Products, and again this this class is implemented as a JavaBean.</p>
<p><em><strong>springapp/src/bus/ProductManager.java</strong></em></p>
<div class="igBar"><span id="ljava-21"><a href="#" onclick="javascript:showPlainTxt('java-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-21">
<div class="java">package bus;</p>
<p><span style="color: #a1a100;">import java.io.Serializable;</span><br />
<span style="color: #a1a100;">import java.util.List;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProductManager <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?q=allinurl%3ASerializable+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Serializable</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> products;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setProducts<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
products = p;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> getProducts<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> products;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Next, I modify the SpringappController to hold a reference to this ProductManager class. As you can see, it is now in a separate package called web – remember to move the source to this new location. I also add code to have the controller pass some product information to the view. The getModelAndView now returns a Map with both the date and time and the product manager reference.</p>
<p><em><strong>springapp/src/web/SpringappController.java</strong></em></p>
<div class="igBar"><span id="ljava-22"><a href="#" onclick="javascript:showPlainTxt('java-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-22">
<div class="java">package web;</p>
<p><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span></p>
<p><span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></p>
<p><span style="color: #a1a100;">import java.io.IOException;</span><br />
<span style="color: #a1a100;">import java.util.Map;</span><br />
<span style="color: #a1a100;">import java.util.HashMap;</span></p>
<p><span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span></p>
<p><span style="color: #a1a100;">import bus.Product;</span><br />
<span style="color: #a1a100;">import bus.ProductManager;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringappController <span style="color: #000000; font-weight: bold;">implements</span> Controller <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #808080; font-style: italic;">/** Logger for this class and subclasses */</span><br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">private</span> ProductManager prodMan;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> now = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"returning hello view with "</span> + now<span style="color: #66cc66;">&#41;</span>;</p>
<p><a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> myModel = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"now"</span>, now<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"products"</span>, getProductManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getProducts</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"hello"</span>, <span style="color: #ff0000;">"model"</span>, myModel<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setProductManager<span style="color: #66cc66;">&#40;</span>ProductManager pm<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
prodMan = pm;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> ProductManager getProductManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> prodMan;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>Step 17 – Modify the view to display business data and add support for message bundle</strong></p>
<p>Using the JSTL  tag, I add a section that displays product information. I have also replaced the title, heading and greeting text with a JSTL  tag that pulls the text to display from a provided 'message' source – I will show this source in a later step.</p>
<p><strong><em>springapp/war/WEB-INF/jsp/hello.jsp</em></strong></p>
<div class="igBar"><span id="ljava-23"><a href="#" onclick="javascript:showPlainTxt('java-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-23">
<div class="java">&lt;%@ include file=<span style="color: #ff0000;">"/WEB-INF/jsp/include.jsp"</span> %&gt;</p>
<p>&lt;html&gt;<br />
&lt;head&gt;&lt;title&gt;&lt;fmt:message key=<span style="color: #ff0000;">"title"</span>/&gt;&lt;/title&gt;&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;&lt;fmt:message key=<span style="color: #ff0000;">"heading"</span>/&gt;&lt;/h1&gt;<br />
&lt;p&gt;&lt;fmt:message key=<span style="color: #ff0000;">"greeting"</span>/&gt; &lt;c:out value=<span style="color: #ff0000;">"${model.now}"</span>/&gt;<br />
&lt;/p&gt;<br />
&lt;h3&gt;Products&lt;/h3&gt;<br />
&lt;c:<span style="color: #b1b100;">forEach</span> items=<span style="color: #ff0000;">"${model.products}"</span> var=<span style="color: #ff0000;">"prod"</span>&gt;<br />
&nbsp; &lt;c:out value=<span style="color: #ff0000;">"${prod.description}"</span>/&gt; &lt;i&gt;$&lt;c:out value=<span style="color: #ff0000;">"${prod.price}"</span>/&gt;&lt;/i&gt;&lt;br&gt;&lt;br&gt;<br />
&lt;/c:forEach&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
</div>
</div>
<p></p>
<p><strong>Step 18 – Add some test data to automatically populate some business objects</strong></p>
<p>I am not going to add any code to load the business objects from a database just yet. Instead, we can “wire up” a couple of instances using Spring's bean and application context support. I will simply put the data I need as a couple of bean entries in springapp-servlet.xml. I will also add the messageSource entry that will pull in the messages resource bundle ('messages.properties') that I will create in the next step.</p>
<p><em><strong>springapp/war/WEB-INF/springapp-servlet.xml</strong></em></p>
<div class="igBar"><span id="lxml-24"><a href="#" onclick="javascript:showPlainTxt('xml-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-24">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span></p>
<p><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--<br />
&nbsp; - Application context definition for &quot;springapp&quot; DispatcherServlet.<br />
&nbsp; --&gt;</span></span></p>
<p>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"springappController"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"web.SpringappController"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"productManager"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"prodMan"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"prodMan"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bus.ProductManager"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"products"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;list<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"product1"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"product2"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"product3"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/list<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"product1"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bus.Product"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"description"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Lamp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"price"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>5.75<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"product2"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bus.Product"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"description"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Table<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"price"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>75.25<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"product3"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bus.Product"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"description"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Chair<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"price"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>22.79<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"messageSource"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.context.support.ResourceBundleMessageSource"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"basename"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>messages<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"urlMapping"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"mappings"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"/hello.htm"</span><span style="font-weight: bold; color: black;">&gt;</span></span>springappController<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/prop<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"viewResolver"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"viewClass"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.servlet.view.JstlView<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"prefix"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>/WEB-INF/jsp/<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"suffix"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>.jsp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p><strong>Step 19 – Add the message bundle and a 'clean' target to build.xml</strong></p>
<p>I create a 'messages.properties' file in the war/WEB-INF/classes directory. This properties bundle so far has three entries matching the keys specified in the  tags that we added to the hello.jsp.</p>
<p><strong><em>springapp/war/WEB-INF/classes/messages.properties</em></strong></p>
<div class="igBar"><span id="ljava-25"><a href="#" onclick="javascript:showPlainTxt('java-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-25">
<div class="java">title=SpringApp<br />
heading=Hello :: <span style="color: #006600;">SpringApp</span><br />
greeting=Greetings, it is now</div>
</div>
</div>
<p></p>
<p>Since we moved some source files around, it makes sense to add a 'clean' and an 'undeploy' target to the build scripts. I add the following entries to the build.xml file.</p>
<div class="igBar"><span id="lxml-26"><a href="#" onclick="javascript:showPlainTxt('xml-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-26">
<div class="xml"></div>
</div>
</div>
<p></p>
<p>Now stop the Tomcat server, run the clean, undeploy and deploy targets. This should remove all old class files, re-build the application and deploy it. Start up Tomcat again and you should see the following:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-153" title="spring_4" src="http://www.webnoob.com/wp-content/uploads/2009/06/spring_4.jpg" alt="spring_4" width="540" height="390" /></p>
<p style="text-align: right;">NDLoc, 30/06/2009</p>
<p><a href="http://www.webnoob.com/2009/06/spring-tutorial-1/" target="_self">Spring Framework tutorial 1</a></p>
<p><strong>References:</strong><br />
- Thomas Risberg, Rick Evans, Portia Tung: <a href="http://static.springframework.org/docs/Spring-MVC-step-by-step/">Developing a Spring Framework MVC application step-by-step</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webnoob.com/2009/06/spring-tutorial-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Framework &#8211; step by step tutorial (1)</title>
		<link>http://www.webnoob.com/2009/06/spring-tutorial-1/</link>
		<comments>http://www.webnoob.com/2009/06/spring-tutorial-1/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 06:32:17 +0000</pubDate>
		<dc:creator>NDLoc</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.webnoob.com/?p=137</guid>
		<description><![CDATA[Part 1 – Basic Application and Environment Setup
This is a step-by-step account of how to develop a web application from scratch using the Spring Framework.
Prerequisites:


Java SDK (I am currently using version 1.4.2)


Ant (using version 1.6.2)


Apache Tomcat (using version 5.0.2)


You should also be reasonably comfortable using the above software.
I am not going to cover a lot [...]]]></description>
			<content:encoded><![CDATA[<h3>Part 1 – Basic Application and Environment Setup</h3>
<p style="margin-bottom: 0in;">This is a step-by-step account of how to develop a web application from scratch using the Spring Framework.</p>
<p style="margin-bottom: 0in;">Prerequisites:</p>
<ul>
<li>
<p style="margin-bottom: 0in;">Java SDK (<em>I am currently using version 1.4.2</em>)</p>
</li>
<li>
<p style="margin-bottom: 0in;">Ant (<em>using version 1.6.2</em>)</p>
</li>
<li>
<p style="margin-bottom: 0in;">Apache Tomcat (<em>using version 5.0.2)</em></p>
</li>
</ul>
<p>You should also be reasonably comfortable using the above software.</p>
<p>I am not going to cover a lot of background information or theory in this document -- there are plenty of books available that covers this in depth. Instead we will dive right into developing the application.<br />
<span id="more-137"></span><br />
<strong>Step 1 – development directory</strong></p>
<p>We are going to need a place to keep all the source and other files we will be creating, so I create a directory that I name 'springapp'. You can place this directory in your home folder or in some other location. I created mine in a 'projects' directory that I already had in my home directory so the full path to my directory is '/Users/trisberg/projects/springapp'. Inside this directory I create a 'src' directory to hold all Java source files. Then I create another directory that I name 'war'. This directory will hold everything that should go into the WAR file, that we would use to deploy our application. All source files other than Java source, like JSPs and configuration files, belongs in this directory.</p>
<p><strong>Step 2 – index.jsp</strong></p>
<p>I will start by creating a JSP page named 'index.jsp' in the war directory. This is the entry point for our application.</p>
<p><strong><em>springapp/war/index.jsp</em></strong></p>
<div class="igBar"><span id="ljava-44"><a href="#" onclick="javascript:showPlainTxt('java-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-44">
<div class="java">&lt;html&gt;<br />
&lt;head&gt;&lt;title&gt;Example :: <span style="color: #006600;">Spring</span> Application&lt;/title&gt;&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Example - Spring Application&lt;/h1&gt;<br />
&lt;p&gt;This is my test.&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
</div>
</div>
<p>
Just to have a complete web application, I create a web.xml in a WEB-INF directory that I create under the war directory.</p>
<p><strong><em>springapp/war/WEB-INF/web.xml</em></strong></p>
<div class="igBar"><span id="lxml-45"><a href="#" onclick="javascript:showPlainTxt('xml-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-45">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'&gt;</span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web</span>-app<span style="font-weight: bold; color: black;">&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web</span>-app<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</div>
</div>
<p>
<strong>Step 3 – deploying the application to Tomcat</strong></p>
<p>Next, I write an Ant build script that we are going to use throughout this document. There are tasks for building and deploying the application. A separate build script contains the app server specific tasks There are also tasks for controlling the application under Tomcat.<br />
<strong><em>springapp/build.xml</em></strong></p>
<div class="igBar"><span id="lxml-46"><a href="#" onclick="javascript:showPlainTxt('xml-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-46">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;project</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"springapp"</span> <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">"."</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">"usage"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">"build.properties"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"src.dir"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"src"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"web.dir"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"war"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"build.dir"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"${web.dir}/WEB-INF/classes"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"name"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"springapp"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"master-classpath"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"${web.dir}/WEB-INF/lib"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"*.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/fileset<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- We need the servlet API classes:&nbsp; &nbsp; &nbsp; &nbsp; --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--&nbsp; &nbsp;for Tomcat 4.1 use servlet.jar&nbsp; &nbsp; &nbsp; &nbsp; --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--&nbsp; &nbsp;for Tomcat 5.0 use servlet-api.jar&nbsp; &nbsp; --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--&nbsp; &nbsp;for Other app server - check the docs --&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"${appserver.home}/common/lib"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"servlet*.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/fileset<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;pathelement</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"${build.dir}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/path<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"usage"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">""</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"${name} build file"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"-----------------------------------"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">""</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"Available targets are:"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">""</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"build&nbsp; &nbsp; &nbsp;--&gt;</span> Build the application&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;echo message="</span>deploy&nbsp; &nbsp; --<span style="font-weight: bold; color: black;">&gt;</span></span> Deploy application as directory&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"deploywar --&gt;</span> Deploy application as a WAR file&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;echo message="</span>install&nbsp; &nbsp;--<span style="font-weight: bold; color: black;">&gt;</span></span> Install application in Tomcat&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"reload&nbsp; &nbsp; --&gt;</span> Reload application in Tomcat&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;echo message="</span>start&nbsp; &nbsp; &nbsp;--<span style="font-weight: bold; color: black;">&gt;</span></span> Start Tomcat application&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">"stop&nbsp; &nbsp; &nbsp; --&gt;</span> Stop Tomcat application&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;echo message="</span>list&nbsp; &nbsp; &nbsp; --<span style="font-weight: bold; color: black;">&gt;</span></span> List Tomcat applications&quot;/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;echo</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">""</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"build"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Compile main source tree java files"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;mkdir</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"${build.dir}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;javac</span> <span style="color: #000066;">destdir</span>=<span style="color: #ff0000;">"${build.dir}"</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">"1.3"</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">"true"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">deprecation</span>=<span style="color: #ff0000;">"false"</span> <span style="color: #000066;">optimize</span>=<span style="color: #ff0000;">"false"</span> <span style="color: #000066;">failonerror</span>=<span style="color: #ff0000;">"true"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;src</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">"${src.dir}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">"master-classpath"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/javac<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"deploy"</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">"build"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Deploy application"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">"${deploy.path}/${name}"</span> <span style="color: #000066;">preservelastmodified</span>=<span style="color: #ff0000;">"true"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"${web.dir}"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"**/*.*"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/fileset<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/copy<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"deploywar"</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">"build"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Deploy application as a WAR file"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;war</span> <span style="color: #000066;">destfile</span>=<span style="color: #ff0000;">"${name}.war"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">webxml</span>=<span style="color: #ff0000;">"${web.dir}/WEB-INF/web.xml"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"${web.dir}"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"**/*.*"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/fileset<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/war<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">"${deploy.path}"</span> <span style="color: #000066;">preservelastmodified</span>=<span style="color: #ff0000;">"true"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">"."</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"*.war"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/fileset<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/copy<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- ============================================================== --&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Tomcat tasks - remove these if you don't have Tomcat installed --&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- ============================================================== --&gt;</span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"install"</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">"org.apache.catalina.ant.InstallTask"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">"${appserver.home}/server/lib/catalina-ant.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/taskdef<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"reload"</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">"org.apache.catalina.ant.ReloadTask"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">"${appserver.home}/server/lib/catalina-ant.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/taskdef<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"list"</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">"org.apache.catalina.ant.ListTask"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">"${appserver.home}/server/lib/catalina-ant.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/taskdef<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"start"</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">"org.apache.catalina.ant.StartTask"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">"${appserver.home}/server/lib/catalina-ant.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/taskdef<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"stop"</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">"org.apache.catalina.ant.StopTask"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;path</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">"${appserver.home}/server/lib/catalina-ant.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/classpath<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/taskdef<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"install"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Install application in Tomcat"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;install</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">"${tomcat.manager.url}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">username</span>=<span style="color: #ff0000;">"${tomcat.manager.username}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">password</span>=<span style="color: #ff0000;">"${tomcat.manager.password}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/${name}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">war</span>=<span style="color: #ff0000;">"${name}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"reload"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Reload application in Tomcat"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;reload</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">"${tomcat.manager.url}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">username</span>=<span style="color: #ff0000;">"${tomcat.manager.username}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">password</span>=<span style="color: #ff0000;">"${tomcat.manager.password}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/${name}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"start"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Start Tomcat application"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;start</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">"${tomcat.manager.url}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">username</span>=<span style="color: #ff0000;">"${tomcat.manager.username}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">password</span>=<span style="color: #ff0000;">"${tomcat.manager.password}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/${name}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"stop"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"Stop Tomcat application"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;stop</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">"${tomcat.manager.url}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">username</span>=<span style="color: #ff0000;">"${tomcat.manager.username}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">password</span>=<span style="color: #ff0000;">"${tomcat.manager.password}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">path</span>=<span style="color: #ff0000;">"/${name}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"list"</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">"List Tomcat applications"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;list</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">"${tomcat.manager.url}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">username</span>=<span style="color: #ff0000;">"${tomcat.manager.username}"</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">password</span>=<span style="color: #ff0000;">"${tomcat.manager.password}"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/target<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- End Tomcat tasks --&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/project<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>This script now contains all the targets that we are going to need to make our development efforts easier. I am not going to cover this script in detail since most if not all of it is pretty much standard Ant and Tomcat stuff. You can just copy the above build file and put it at the root of your development directory tree. We also need a build.properties file that you should customize to match your server installation. This file belongs in the same directory as the build.xml file.</p>
<p><strong><em>springapp/build.properties</em></strong></p>
<div class="igBar"><span id="ljava-47"><a href="#" onclick="javascript:showPlainTxt('java-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-47">
<div class="java"># Ant properties <span style="color: #b1b100;">for</span> building the springapp</p>
<p>appserver.<span style="color: #006600;">home</span>=$<span style="color: #66cc66;">&#123;</span>user.<span style="color: #006600;">home</span><span style="color: #66cc66;">&#125;</span>/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span><br />
deploy.<span style="color: #006600;">path</span>=$<span style="color: #66cc66;">&#123;</span>appserver.<span style="color: #006600;">home</span><span style="color: #66cc66;">&#125;</span>/webapps</p>
<p>tomcat.<span style="color: #006600;">manager</span>.<span style="color: #006600;">url</span>=http:<span style="color: #808080; font-style: italic;">//localhost:8080/manager</span><br />
tomcat.<span style="color: #006600;">manager</span>.<span style="color: #006600;">username</span>=admin<br />
tomcat.<span style="color: #006600;">manager</span>.<span style="color: #006600;">password</span>=tomcat</div>
</div>
</div>
<p></p>
<blockquote><p>If you are on a system where you are not the owner of the Tomcat install, then the Tomcat owner must either grant you full access to the webapps directory or the owner must create a new directory named 'springapp' in the 'webapps' directory of the Tomcat installation, and also give you full rights to deploy to this newly created directory. On Linux I run the command chmod a+rwx springapp to give everybody full rights to this directory.</p>
<p>If you are using a different web application server, then you can remove the Tomcat specific tasks at the end of the build script. You will have to rely on your server's hot deploy feature, or you will have to stop and start your application manually.</p></blockquote>
<p>Now I run Ant to make sure that everything is working OK. You should have your current directory set to the 'springapp' directory.</p>
<div class="igBar"><span id="ljava-48"><a href="#" onclick="javascript:showPlainTxt('java-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-48">
<div class="java"><span style="color: #66cc66;">&#91;</span>trisberg@localhost springapp<span style="color: #66cc66;">&#93;</span>$ ant<br />
Buildfile: build.<span style="color: #006600;">xml</span></p>
<p>usage:</p>
<p><span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> springapp build file<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> -----------------------------------</p>
<p><span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> Available targets are:</p>
<p><span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> build&nbsp; &nbsp; &nbsp;--&amp;gt; Build the application<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> deploy&nbsp; &nbsp; --&amp;gt; Deploy application as directory<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> deploywar --&amp;gt; Deploy application as a WAR file<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> install&nbsp; &nbsp;--&amp;gt; Install application in Tomcat<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> reload&nbsp; &nbsp; --&amp;gt; Reload application in Tomcat<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> start&nbsp; &nbsp; &nbsp;--&amp;gt; Start Tomcat application<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> stop&nbsp; &nbsp; &nbsp; --&amp;gt; Stop Tomcat application<br />
<span style="color: #66cc66;">&#91;</span>echo<span style="color: #66cc66;">&#93;</span> list&nbsp; &nbsp; &nbsp; --&amp;gt; <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> Tomcat applications</p>
<p>BUILD SUCCESSFUL<br />
Total time: <span style="color: #cc66cc;">2</span> seconds</div>
</div>
</div>
<p></p>
<p>Last action here is to do the actual deployment. Just run Ant and specify 'deploy' or 'deploywar' as the target.</p>
<div class="igBar"><span id="ljava-49"><a href="#" onclick="javascript:showPlainTxt('java-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-49">
<div class="java"><span style="color: #66cc66;">&#91;</span>trisberg@localhost springapp<span style="color: #66cc66;">&#93;</span>$ ant deploy<br />
Buildfile: build.<span style="color: #006600;">xml</span></p>
<p>build:<br />
<span style="color: #66cc66;">&#91;</span>mkdir<span style="color: #66cc66;">&#93;</span> Created dir: /Users/trisberg/projects/springapp/war/WEB-INF/classes</p>
<p>deploy:<br />
<span style="color: #66cc66;">&#91;</span>copy<span style="color: #66cc66;">&#93;</span> Copying <span style="color: #cc66cc;">2</span> files to /Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/springapp</p>
<p>BUILD SUCCESSFUL<br />
Total time: <span style="color: #cc66cc;">2</span> seconds</div>
</div>
</div>
<p>
<strong> </strong></p>
<p><strong>Step 4 – Test the application</strong></p>
<p>Let's just quickly start Tomcat and make sure that we can access the application. Use the 'list' task from our build file to see if Tomcat has picked up the new application.</p>
<div class="igBar"><span id="ljava-50"><a href="#" onclick="javascript:showPlainTxt('java-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-50">
<div class="java"><span style="color: #66cc66;">&#91;</span>trisberg@localhost springapp<span style="color: #66cc66;">&#93;</span>$ ant list<br />
Buildfile: build.<span style="color: #006600;">xml</span></p>
<p>list:<br />
<span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> OK - Listed applications <span style="color: #b1b100;">for</span> virtual host localhost</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /admin:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/server/webapps/admin</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /webdav:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/webdav</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /servlets-examples:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/servlets-examples</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /springapp:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/springapp</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /jsp-examples:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/jsp-examples</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /balancer:running:<span style="color: #cc66cc;">0</span>:balancer</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /tomcat-docs:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/tomcat-docs</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/webapps/ROOT</p>
<p><span style="color: #66cc66;">&#91;</span>list<span style="color: #66cc66;">&#93;</span> /manager:running:<span style="color: #cc66cc;">0</span>:/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/server/webapps/manager</p>
<p>BUILD SUCCESSFUL<br />
Total time: <span style="color: #cc66cc;">1</span> second</div>
</div>
</div>
<p>
If it is not listed, use the 'install' task to get the application installed in Tomcat.</p>
<div class="igBar"><span id="ljava-51"><a href="#" onclick="javascript:showPlainTxt('java-51'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-51">
<div class="java"><span style="color: #66cc66;">&#91;</span>trisberg@localhost springapp<span style="color: #66cc66;">&#93;</span>$ ant install<br />
Buildfile: build.<span style="color: #006600;">xml</span></p>
<p>install:<br />
<span style="color: #66cc66;">&#91;</span>install<span style="color: #66cc66;">&#93;</span> OK - Installed application at context path /springapp<br />
&lt;p style=<span style="color: #ff0000;">"text-align: left;"</span>&gt;BUILD SUCCESSFUL<br />
Total time: <span style="color: #cc66cc;">2</span> seconds</div>
</div>
</div>
<p></p>
<p style="text-align: left;">Now open a browser and browse to <a href="http://localhost:8080/springapp/index.jsp">http://localhost:8080/springapp/index.jsp.</a></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-138" title="spring_1" src="http://www.webnoob.com/wp-content/uploads/2009/06/spring_1.png" alt="spring_1" width="529" height="333" /><br />
<strong><br />
Step 5 – Download Spring distribution</strong></p>
<p>If you have not already downloaded the Spring Framework Release file, now is the time to do so. I am currently using 'spring-framework-1.2-with-dependencies.zip' that can be downloaded from www.springframework.org/download.html. I unzipped this file in my home directory. We are going to use several files from this download later on.</p>
<p>This completes the setup of the environment that is necessary, and now we can start actually developing our Spring Framework MVC application.<br />
<strong><br />
Step 6 – Modify web.xml in WEB-INF directory</strong></p>
<p>Go to the 'springapp/war/ WEB-INF' directory. Modify the minimal 'web.xml' file that we created earlier. Now we will modify it to suit our needs. We define a DispatcherServlet that is going to control where all our request are routed based on information we will enter at a later point. It also has a standard servlet-mapping entry that maps to the url patterns that we will be using. I have decided to let any url with an '.htm' extension be routed to the 'springapp' dispatcher.</p>
<p><strong><em>springapp/war/WEB-INF/web.xml</em></strong></p>
<div class="igBar"><span id="lxml-52"><a href="#" onclick="javascript:showPlainTxt('xml-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-52">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'&gt;</span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web</span>-app<span style="font-weight: bold; color: black;">&gt;</span></span></p>
<p>&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>springapp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-class<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;load</span>-on-startup<span style="font-weight: bold; color: black;">&gt;</span></span>1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/load</span>-on-startup<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span></p>
<p>&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span>springapp<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-name<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span>*.htm<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url</span>-pattern<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet</span>-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></p>
<p>&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;welcome</span>-file-list<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;welcome</span>-file<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; index.jsp<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/welcome</span>-file<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/welcome</span>-file-list<span style="font-weight: bold; color: black;">&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web</span>-app<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</div>
</div>
<p></p>
<p>Next, create a file called 'springapp-servlet.xml' in the springapp/war/WEB-INF directory (you can copy an example of this file from the Spring distributions sample/skeletons/webapp-minimal directory). This is the file where definitions used by the DispatcherServlet should be entered. It is named based on the servlet-name from web.xml with '-servlet' appended. This is a standard naming convention used in the Spring Framework. Now, add a bean entry named springappController and make the class SpringappController. This defines the controller that our application will be using. We also need to add a url mapping so the DispatcherServlet knows which controller should be invoked for different urls:</p>
<p><strong><em>springapp/war/WEB-INF/springapp-servlet.xml</em></strong></p>
<div class="igBar"><span id="lxml-53"><a href="#" onclick="javascript:showPlainTxt('xml-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-53">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span></p>
<p><span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--<br />
&nbsp; - Application context definition for &quot;springapp&quot; DispatcherServlet.<br />
&nbsp; --&gt;</span></span></p>
<p><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"springappController"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"SpringappController"</span><span style="font-weight: bold; color: black;">/&gt;</span></span></p>
<p>&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"urlMapping"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"mappings"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"/hello.htm"</span><span style="font-weight: bold; color: black;">&gt;</span></span>springappController<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/prop<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/props<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p><strong>Step 7 – Copy jars to WEB-INF/lib</strong></p>
<p>First create a 'lib' directory in the 'war/WEB-INF' directory.  Then, from the Spring distribution, copy spring.jar (spring-framework-1.2/dist/spring.jar) to the new war/WEB-INF/lib directory. Also copy commons-logging jars to the war/WEB-INF/lib directory (spring-framework-1.2/lib/jakarta-commons/commons-logging.jar). We are also going to need a log4j jar. Copy log4j-1.2.9.jar to the war/WEB-INF/lib directory (spring-framework-1.2/lib/log4j/log4j-1.2.9.jar). These jars will be deployed to the server and they are also used during the build process.</p>
<p><strong>Step 8 – Create your Controller</strong></p>
<p>Create your Controller – I named mine SpringappController.java and placed it in the springapp/src directory.</p>
<p><strong><em>springapp/src/SpringappController.java</em></strong></p>
<div class="igBar"><span id="ljava-54"><a href="#" onclick="javascript:showPlainTxt('java-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-54">
<div class="java"><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span></p>
<p><span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></p>
<p><span style="color: #a1a100;">import java.io.IOException;</span></p>
<p><span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringappController <span style="color: #000000; font-weight: bold;">implements</span> Controller <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** Logger for this class and subclasses */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"SpringappController - returning hello view"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"hello.jsp"</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p>
This is as basic a Controller as you can use. We will be expanding this later on, and we will also later on extend some provided abstract base implementations. The Controller “handles” the request and returns a ModelAndView. We have not yet defined any Views, so right now there is nothing to do.</p>
<p><strong>Step 9 – Build the Application</strong></p>
<p>Run the 'build' task of the build.xml. Hopefully the code compiles OK.</p>
<div class="igBar"><span id="ljava-55"><a href="#" onclick="javascript:showPlainTxt('java-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-55">
<div class="java"><span style="color: #66cc66;">&#91;</span>trisberg@localhost springapp<span style="color: #66cc66;">&#93;</span>$ ant build<br />
Buildfile: build.<span style="color: #006600;">xml</span></p>
<p>build:<br />
<span style="color: #66cc66;">&#91;</span>javac<span style="color: #66cc66;">&#93;</span> Compiling <span style="color: #cc66cc;">1</span> source file to /Users/trisberg/projects/springapp/war/WEB-INF/classes</p>
<p>BUILD SUCCESSFUL<br />
Total time: <span style="color: #cc66cc;">2</span> seconds</div>
</div>
</div>
<p></p>
<p><strong>Step 10 – Copy and modify log4j.properties</strong></p>
<p>The Spring Framework uses log4j for logging so we have to create a configuration file for log4j. Copy the log4j.properties from the sample Petclinic application (spring-framework-1.2/samples/petclinic/war/WEB-INF/log4j.properties) to the war/WEB-INF/classes directory (this directory should have been created in the previous step). Now uncomment or modify the log4j.rootCategory property and change the name and location of the logfile that will be written. I decided to have it written to the same directory as all other Tomcat logs.</p>
<p><strong><em>springapp/war/WEB-INF/classes/log4j.properties</em></strong></p>
<div class="igBar"><span id="ljava-56"><a href="#" onclick="javascript:showPlainTxt('java-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-56">
<div class="java"># <span style="color: #b1b100;">For</span> JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.<span style="color: #006600;">xml</span>!<br />
# <span style="color: #b1b100;">For</span> all other servers: Comment out the Log4J listener in web.<span style="color: #006600;">xml</span> to activate Log4J.<br />
<span style="color: #006600;">log4j</span>.<span style="color: #006600;">rootLogger</span>=INFO, stdout, logfile</p>
<p>log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">stdout</span>=org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">ConsoleAppender</span><br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">stdout</span>.<span style="color: #006600;">layout</span>=org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">PatternLayout</span><br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">stdout</span>.<span style="color: #006600;">layout</span>.<span style="color: #006600;">ConversionPattern</span>=%d %p <span style="color: #66cc66;">&#91;</span>%c<span style="color: #66cc66;">&#93;</span> - &amp;lt;%m&amp;gt;%n</p>
<p>log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>=org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">RollingFileAppender</span><br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>.<a href="http://www.google.com/search?q=allinurl%3AFile+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">File</span></a>=/Users/trisberg/jakarta-tomcat-<span style="color: #cc66cc;">5</span>.<span style="color: #cc66cc;">0</span>.<span style="color: #cc66cc;">28</span>/logs/springapp.<span style="color: #006600;">log</span><br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>.<span style="color: #006600;">MaxFileSize</span>=512KB<br />
# Keep three backup files.<br />
<span style="color: #006600;">log4j</span>.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>.<span style="color: #006600;">MaxBackupIndex</span>=<span style="color: #cc66cc;">3</span><br />
# Pattern to output: date priority <span style="color: #66cc66;">&#91;</span>category<span style="color: #66cc66;">&#93;</span> - message<br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>.<span style="color: #006600;">layout</span>=org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">PatternLayout</span><br />
log4j.<span style="color: #006600;">appender</span>.<span style="color: #006600;">logfile</span>.<span style="color: #006600;">layout</span>.<span style="color: #006600;">ConversionPattern</span>=%d %p <span style="color: #66cc66;">&#91;</span>%c<span style="color: #66cc66;">&#93;</span> - %m%n</div>
</div>
</div>
<p></p>
<p><strong>Step 11 – Deploy Application</strong></p>
<p>Run the 'deploy' task and then the 'stop' and 'start' tasks of the build.xml. This will force a reload of the application. We have to check the Tomcat logs for any deployment errors – there could be typos in the above xml files or there could be missing classes or jar files. This is an example of what it should look like. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)</p>
<div class="igBar"><span id="ljava-57"><a href="#" onclick="javascript:showPlainTxt('java-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-57">
<div class="java"><span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">112</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Initializing servlet <span style="color: #ff0000;">'springapp'</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">261</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - FrameworkServlet <span style="color: #ff0000;">'springapp'</span>: initialization started<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">373</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">xml</span>.<span style="color: #006600;">XmlBeanDefinitionReader</span><span style="color: #66cc66;">&#93;</span> - Loading XML bean definitions from ServletContext resource <span style="color: #66cc66;">&#91;</span>/WEB-INF/springapp-servlet.<span style="color: #006600;">xml</span><span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">498</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">XmlWebApplicationContext</span><span style="color: #66cc66;">&#93;</span> - Bean factory <span style="color: #b1b100;">for</span> application context <span style="color: #66cc66;">&#91;</span>WebApplicationContext <span style="color: #b1b100;">for</span> namespace <span style="color: #ff0000;">'springapp-servlet'</span><span style="color: #66cc66;">&#93;</span>: org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DefaultListableBeanFactory</span> defining beans <span style="color: #66cc66;">&#91;</span>springappController,urlMapping<span style="color: #66cc66;">&#93;</span>; root of BeanFactory hierarchy<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">505</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">XmlWebApplicationContext</span><span style="color: #66cc66;">&#93;</span> - <span style="color: #cc66cc;">2</span> beans defined in application context <span style="color: #66cc66;">&#91;</span>WebApplicationContext <span style="color: #b1b100;">for</span> namespace <span style="color: #ff0000;">'springapp-servlet'</span><span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">523</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">core</span>.<span style="color: #006600;">CollectionFactory</span><span style="color: #66cc66;">&#93;</span> - JDK <span style="color: #cc66cc;">1</span>.<span style="color: #cc66cc;">4</span>+ collections available<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">524</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">core</span>.<span style="color: #006600;">CollectionFactory</span><span style="color: #66cc66;">&#93;</span> - Commons <a href="http://www.google.com/search?q=allinurl%3ACollections+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a> <span style="color: #cc66cc;">3</span>.<span style="color: #006600;">x</span> available<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">537</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">XmlWebApplicationContext</span><span style="color: #66cc66;">&#93;</span> - Unable to locate MessageSource with name <span style="color: #ff0000;">'messageSource'</span>: using default <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DelegatingMessageSource</span>@8dacb<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">539</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">XmlWebApplicationContext</span><span style="color: #66cc66;">&#93;</span> - Unable to locate ApplicationEventMulticaster with name <span style="color: #ff0000;">'applicationEventMulticaster'</span>: using default <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">event</span>.<span style="color: #006600;">SimpleApplicationEventMulticaster</span>@5674a4<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">549</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">ui</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">UiApplicationContextUtils</span><span style="color: #66cc66;">&#93;</span> - No ThemeSource found <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#91;</span>WebApplicationContext <span style="color: #b1b100;">for</span> namespace <span style="color: #ff0000;">'springapp-servlet'</span><span style="color: #66cc66;">&#93;</span>: using ResourceBundleThemeSource<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">556</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DefaultListableBeanFactory</span><span style="color: #66cc66;">&#93;</span> - Pre-instantiating singletons in factory <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DefaultListableBeanFactory</span> defining beans <span style="color: #66cc66;">&#91;</span>springappController,urlMapping<span style="color: #66cc66;">&#93;</span>; root of BeanFactory hierarchy<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">557</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DefaultListableBeanFactory</span><span style="color: #66cc66;">&#93;</span> - Creating shared instance of singleton bean <span style="color: #ff0000;">'springappController'</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">603</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">beans</span>.<span style="color: #006600;">factory</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">DefaultListableBeanFactory</span><span style="color: #66cc66;">&#93;</span> - Creating shared instance of singleton bean <span style="color: #ff0000;">'urlMapping'</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">667</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Using context <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">context</span>.<span style="color: #006600;">support</span>.<span style="color: #006600;">XmlWebApplicationContext</span><span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100;">for</span> servlet <span style="color: #ff0000;">'springapp'</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">668</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Unable to locate MultipartResolver with name <span style="color: #ff0000;">'multipartResolver'</span>: no multipart request handling provided<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">670</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Unable to locate LocaleResolver with name <span style="color: #ff0000;">'localeResolver'</span>: using default <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">i18n</span>.<span style="color: #006600;">AcceptHeaderLocaleResolver</span>@<span style="color: #cc66cc;">318309</span><span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">675</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Unable to locate ThemeResolver with name <span style="color: #ff0000;">'themeResolver'</span>: using default <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">theme</span>.<span style="color: #006600;">FixedThemeResolver</span>@c11e94<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">681</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - No HandlerAdapters found in servlet <span style="color: #ff0000;">'springapp'</span>: using default<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">700</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - No ViewResolvers found in servlet <span style="color: #ff0000;">'springapp'</span>: using default<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">700</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - FrameworkServlet <span style="color: #ff0000;">'springapp'</span>: initialization completed in <span style="color: #cc66cc;">439</span> ms<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">14</span>:<span style="color: #cc66cc;">58</span>:<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">704</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Servlet <span style="color: #ff0000;">'springapp'</span> configured successfully</div>
</div>
</div>
<p></p>
<p><strong>Step 12 – Create a View</strong></p>
<p>Now it is time to create our first view. I will use a JSP page that I decided to name hello.jsp. I'll put it in the war directory to begin with.</p>
<p><strong><em>springapp/war/hello.jsp</em></strong></p>
<div class="igBar"><span id="ljava-58"><a href="#" onclick="javascript:showPlainTxt('java-58'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-58">
<div class="java">Example :: <span style="color: #006600;">Spring</span> Application<br />
&lt;h1&gt;Hello - Spring Application&lt;/h1&gt;<br />
Greetings.</div>
</div>
</div>
<p></p>
<p>Nothing fancy here, but it will do for now. Next we have to modify the SpringappController to forward to this view.<br />
<strong><em> </em></strong></p>
<p><strong><em>springapp/src/SpringappController.java</em></strong></p>
<div class="igBar"><span id="ljava-59"><a href="#" onclick="javascript:showPlainTxt('java-59'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-59">
<div class="java"><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span></p>
<p><span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span></p>
<p><span style="color: #a1a100;">import java.io.IOException;</span></p>
<p><span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringappController <span style="color: #000000; font-weight: bold;">implements</span> Controller <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #808080; font-style: italic;">/** Logger for this class and subclasses */</span><br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p>logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"SpringappController - returning hello view"</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"hello.jsp"</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>While I was modifying this class, I also added a logger so we can verify that we actually got here. Changes are highlighted in red. The model that this class returns is actually resolved via a ViewResolver. Since we have not specified a specific one, we are going to get a default one that just forwards to a url matching the name of the view specified. We will modify this later on.</p>
<p>Now compile and deploy the application. After instructing Tomcat to stop and then start the application, everything should get reloaded.</p>
<p style="text-align: left;">Let's try it in a browser – enter the url http://localhost:8080/springapp/hello.htm and we should see the following:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-139" title="spring_2" src="http://www.webnoob.com/wp-content/uploads/2009/06/spring_2.png" alt="spring_2" width="535" height="412" /></p>
<p>We can also check the log – I'm only showing the last entries, but we can see that the controller did get invoked and that it forwarded to the hello view. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)</p>
<div class="igBar"><span id="ljava-60"><a href="#" onclick="javascript:showPlainTxt('java-60'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-60">
<div class="java"><span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">15</span>:<span style="color: #cc66cc;">01</span>:<span style="color: #cc66cc;">56</span>,<span style="color: #cc66cc;">217</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - FrameworkServlet <span style="color: #ff0000;">'springapp'</span>: initialization completed in <span style="color: #cc66cc;">372</span> ms<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">15</span>:<span style="color: #cc66cc;">01</span>:<span style="color: #cc66cc;">56</span>,<span style="color: #cc66cc;">217</span> INFO <span style="color: #66cc66;">&#91;</span>org.<span style="color: #006600;">springframework</span>.<span style="color: #006600;">web</span>.<span style="color: #006600;">servlet</span>.<span style="color: #006600;">DispatcherServlet</span><span style="color: #66cc66;">&#93;</span> - Servlet <span style="color: #ff0000;">'springapp'</span> configured successfully<br />
<span style="color: #cc66cc;">2005</span>-<span style="color: #cc66cc;">04</span>-<span style="color: #cc66cc;">24</span> <span style="color: #cc66cc;">15</span>:<span style="color: #cc66cc;">03</span>:<span style="color: #cc66cc;">57</span>,<span style="color: #cc66cc;">908</span> INFO <span style="color: #66cc66;">&#91;</span>SpringappController<span style="color: #66cc66;">&#93;</span> - SpringappController - returning hello view</div>
</div>
</div>
<p></p>
<p><strong>Summary</strong></p>
<p>Let's take quick look at the parts of our application that we have created so far.</p>
<p>1. An introduction page index.jsp that does not do anything useful. It was just used to test our setup. We will later change this to actually provide a link into our application.<br />
2.A DispatcherServlet with a corresponding springapp-servlet.xml configuration file.<br />
3.A controller springappController.java with limited functionality – it just forwards a ModelAndView to the ViewResolver. Actually, we only have an empty model so far, but we will fix this later.<br />
4.A view hello.jsp that again is extremely basic. But the whole setup works and we are now ready to add more functionality.</p>
<p style="text-align: right;">NDLoc, 29/06/2009</p>
<p style="text-align: left;"><a href="../2009/06/spring-tutorial-2/" target="_self">Spring Framework tutorial 2</a></p>
<p><strong>References:</strong><br />
- Thomas Risberg, Rick Evans, Portia Tung: <a href="http://static.springframework.org/docs/Spring-MVC-step-by-step/">Developing a Spring Framework MVC application step-by-step</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.webnoob.com/2009/06/spring-tutorial-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UniNet &#8211; a Sakai plugin built on Spring and Hibernate</title>
		<link>http://www.webnoob.com/2009/06/uninet/</link>
		<comments>http://www.webnoob.com/2009/06/uninet/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 04:38:26 +0000</pubDate>
		<dc:creator>NDLoc</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.webnoob.com/?p=126</guid>
		<description><![CDATA[UniNet is a university-scoped social network built on Spring and Hibernate and integrated into Sakai portal as a independent module.
The purpose of UniNet is  to provide a communication channel where students can contact with each other seeking help ,  looking for other students (from the same country, or city;  speaking the same language; doing the [...]]]></description>
			<content:encoded><![CDATA[<p>UniNet is a university-scoped social network built on Spring and Hibernate and integrated into Sakai portal as a independent module.</p>
<p>The purpose of UniNet is  to provide a communication channel where students can contact with each other seeking help ,  looking for other students (from the same country, or city;  speaking the same language; doing the course or subject;  having some skills or interest) or to team up a group (of their interest or to do a project).</p>
<blockquote><p><a href="http://sakaiproject.org/portal" target="_blank"><strong>Sakai</strong></a> is a community of academic institutions, commercial organizations and individuals who work together to develop a common Collaboration and Learning Environment (CLE). The Sakai CLE is a free, community source, educational software platform distributed under the Educational Community License (a type of open source license). The Sakai CLE is used for teaching, research and collaboration. Systems of this type are also known as Course Management Systems (CMS), Learning Management Systems (LMS), or Virtual Learning Environments (VLE).</p>
<p>Sakai is a Java-based, service-oriented application suite that is designed to be scalable, reliable, interoperable and extensible. Version 1.0 was released in March 2005.</p>
<p>As of July 2007, Sakai is in production at over 150 institutions and being piloted by over 100 more.</p></blockquote>
<p>UniNet is developed on the MVC model of Spring framework and Object Mapping technique of Hibernate framework. The module follows Sakai standards and integrated into Sakai project as a plug-in tool.</p>
<p>During the development and testing phase, the module used Tomcat as virtual web server, MySQL as database server and Maven as deployment tool. Project timeline and management activities has been done through Trac; code version was controlled by Subversion through Eclipse plug-in (Subclipse).<span id="more-126"></span></p>
<h3>System Architecture:</h3>
<p><strong>Description:</strong></p>
<ul>
<li>when a student enrolls in a course, he will automatically has a UniNet page but it is up to him to activate his page or not.</li>
<li> UniNet page contain the contents about Personal information, Group list, Course information, Subject information , Interests, Skills</li>
</ul>
<p><strong>Functionalities: </strong></p>
<ul>
<li>User<strong> </strong>can customize the information he wants to post on his page (except Course information) : he can choose what information to be displayed; upload his represent pictures; write and edit entries, upload images in the entry</li>
<li>User can search for students from any countries or cities; for students who are doing the same course or units with him; for students with specific interests; for students with specific skills</li>
<li>User can create a group and give a description about the group such as, then he could invite other users to join his group. Each group will have a group page where all members can write entries and upload images about the group activities.</li>
<li>When join a group, user can send internal emails to all members of group as well as receive emails from others</li>
</ul>
<p style="text-align: center;"><strong>MVC structure</strong><br />
<img class="aligncenter size-full wp-image-129" title="uninet_5" src="http://www.webnoob.com/wp-content/uploads/2009/06/uninet_5.jpg" alt="uninet_5" width="540" height="247" /></p>
<h3>Sample code</h3>
<p><strong>Model : UserManager.java</strong></p>
<div class="igBar"><span id="ljava-64"><a href="#" onclick="javascript:showPlainTxt('java-64'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-64">
<div class="java">package model;</p>
<p><span style="color: #a1a100;">import org.hibernate.HibernateException;</span><br />
<span style="color: #a1a100;">import org.hibernate.Session;</span><br />
<span style="color: #a1a100;">import java.util.*;</span><br />
<span style="color: #808080; font-style: italic;">/**<br />
* Model : class UserManager<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserManager <span style="color: #66cc66;">&#123;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> UserManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> getUserList<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> list = <span style="color: #000000; font-weight: bold;">null</span>;<br />
Session session = HibernateUtil.<span style="color: #006600;">getSessionFactory</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getCurrentSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
session.<span style="color: #006600;">beginTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">try</span><br />
<span style="color: #66cc66;">&#123;</span><br />
list = session.<span style="color: #006600;">createQuery</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"from User"</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
session.<span style="color: #006600;">getTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>HibernateException e<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
session.<span style="color: #006600;">getTransaction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">rollback</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">throw</span> e;<br />
<span style="color: #66cc66;">&#125;</span><br />
<a href="http://www.google.com/search?q=allinurl%3ACollections+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a>.<span style="color: #006600;">sort</span><span style="color: #66cc66;">&#40;</span>list<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">return</span> list;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>Controller: UserController.java</strong></p>
<div class="igBar"><span id="ljava-65"><a href="#" onclick="javascript:showPlainTxt('java-65'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-65">
<div class="java">package controller;</p>
<p><span style="color: #a1a100;">import org.springframework.web.servlet.mvc.Controller;</span><br />
<span style="color: #a1a100;">import org.springframework.web.servlet.ModelAndView;</span><br />
<span style="color: #a1a100;">import javax.servlet.ServletException;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span><br />
<span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.Log;</span><br />
<span style="color: #a1a100;">import org.apache.commons.logging.LogFactory;</span><br />
<span style="color: #a1a100;">import java.io.IOException;</span><br />
<span style="color: #a1a100;">import java.util.*;</span><br />
<span style="color: #a1a100;">import model.*;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserController <span style="color: #000000; font-weight: bold;">implements</span> Controller<span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> UserManager userManager;<br />
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> Log logger = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">public</span> ModelAndView handleRequest<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">throws</span> ServletException, <a href="http://www.google.com/search?q=allinurl%3AIOException+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span></p>
<p><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> dateSt = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006600;">util</span>.<a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
logger.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"User access homepage on: "</span> + dateSt<span style="color: #66cc66;">&#41;</span>;</p>
<p><a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Map</span></a> myModel = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">HashMap</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333;">int</span>&nbsp;userID= <a href="http://www.google.com/search?q=allinurl%3AInteger+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a>.<span style="color: #006600;">parseInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#41;</span>request.<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"id"</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333;">int</span> home = request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span><span style="color: #66cc66;">&#41;</span>!=<span style="color: #000000; font-weight: bold;">null</span>?<br />
<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AInteger+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a><span style="color: #66cc66;">&#41;</span>request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #cc66cc;">0</span>;</p>
<p><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>home==<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
home = userID;<br />
<span style="color: #66cc66;">&#125;</span><br />
User user= userManager.<span style="color: #006600;">getUser</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span>, home<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"user"</span>, user<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333;">int</span> state = user.<span style="color: #006600;">getState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>state==<span style="color: #cc66cc;">0</span> &amp;amp;&amp;amp; home!=userID<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span>,home<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> rs = <span style="color: #ff0000;">"This user has not been activated"</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"notActive"</span>, rs<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"activate"</span>, <span style="color: #ff0000;">"ToActivate"</span>, myModel<span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #66cc66;">&#125;</span><br />
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>state==<span style="color: #cc66cc;">0</span> &amp;amp;&amp;amp; home==userID<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> rs =<span style="color: #ff0000;">"Click the following link to activate your UniNet"</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"doActive"</span>, rs<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"activate"</span>, <span style="color: #ff0000;">"ToActivate"</span>, myModel<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span><br />
request.<span style="color: #006600;">getSession</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setAttribute</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span>,home<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> subList = userManager.<span style="color: #006600;">getSubList</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> skillList = userManager.<span style="color: #006600;">getSkillList</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> intList= userManager.<span style="color: #006600;">getIntList</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> courseList = userManager.<span style="color: #006600;">getCourseList</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> groupList = userManager.<span style="color: #006600;">getGroupList</span><span style="color: #66cc66;">&#40;</span>userID<span style="color: #66cc66;">&#41;</span>;<br />
<a href="http://www.google.com/search?q=allinurl%3AList+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">List</span></a> newMsg = userManager.<span style="color: #006600;">getMessageOfUser</span><span style="color: #66cc66;">&#40;</span>userID,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"subList"</span>, subList<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"courseList"</span>, courseList<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"skillList"</span>, skillList<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"intList"</span>, intList<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"groupList"</span>, groupList<span style="color: #66cc66;">&#41;</span>;<br />
myModel.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"newMsg"</span>,newMsg.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</p>
<p><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"home"</span>, <span style="color: #ff0000;">"Home"</span>, myModel<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setUserManager<span style="color: #66cc66;">&#40;</span>UserManager uMage<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
userManager = uMage;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> UserManager getUserManager<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> userManager;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>View: search.jsp</strong></p>
<div class="igBar"><span id="lhtml-66"><a href="#" onclick="javascript:showPlainTxt('html-66'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-66">
<div class="html"><span style="color: #ddbb00;">&amp;lt;</span>%@ include file=&quot;include/include.jsp&quot; %<span style="color: #ddbb00;">&amp;gt;</span></p>
<p><span style="color: #ddbb00;">&amp;lt;</span>fmt:message key=&quot;search&quot;/<span style="color: #ddbb00;">&amp;gt;</span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"body_container"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"header"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span>SEARCH RESULT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"home"</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"/uninet/home.htm?id=&amp;lt;c:out value="</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span>&quot;<span style="color: #ddbb00;">&amp;gt;</span>Home<br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"main"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"info"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul&gt;</span></a></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></a></span> <span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a</span></a> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"/uninet/home.htm?id=&amp;lt;c:out value="</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
&quot;<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</div>
</div>
<p></p>
<h3>Screen shot</h3>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-130" title="uninet_1" src="http://www.webnoob.com/wp-content/uploads/2009/06/uninet_1.jpg" alt="uninet_1" width="540" height="364" />Home page</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-131" title="uninet_3" src="http://www.webnoob.com/wp-content/uploads/2009/06/uninet_3.jpg" alt="uninet_3" width="540" height="168" /></p>
<p style="text-align: center;">Message Box</p>
<p style="text-align: left;">Download:  <a href="http://www.webnoob.com/audio/sakai-uninet.rar" target="_self">sakai-uninet.rar</a></p>
<p style="text-align: right;">NDLoc, 29/06/2009</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webnoob.com/2009/06/uninet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flickr Panda – a mashup written in Ruby on Rail and Ajax</title>
		<link>http://www.webnoob.com/2009/06/flickr-panda/</link>
		<comments>http://www.webnoob.com/2009/06/flickr-panda/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:53:14 +0000</pubDate>
		<dc:creator>NDLoc</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.webnoob.com/?p=90</guid>
		<description><![CDATA[Flickr is one of the largest online photo management and sharing application which claims to host more than 3.6 billion of images as June 2009. Flickr provides APIs for outside developers to develop mashup application which can interact with Flickr services. Flickr Panda is a set of AIPs allows users to take a list of [...]]]></description>
			<content:encoded><![CDATA[<p>Flickr is one of the largest online photo management and sharing application which claims to host more than 3.6 billion of images as June 2009. Flickr provides APIs for outside developers to develop mashup application which can interact with Flickr services. Flickr Panda is a set of AIPs allows users to take a list of recent public and safe photos.</p>
<blockquote><p>In web development, a <strong>mashup</strong> is "a web page or application that combines data or functionality from two or more external sources to create a new service. The term <strong>mashup</strong> implies easy, fast integration, frequently using open APIs and data sources to produce results that were not the original reason for producing the raw source data.</p>
<p>An example of a mashup is the use of cartographic data from Google Maps to add location information to real estate data, thereby creating a new and distinct Web service that was not originally provided by either source.</p></blockquote>
<p>Below is a step by step instruction to develop a simple Flickr Panda mashup application using in Ruby on Rail and Ajax.<br />
<span id="more-90"></span></p>
<h3>Installation and configuration</h3>
<p><strong>Install Ruby on Rail:</strong></p>
<p>The easy way to install Ruby on your machine is to visit download section of rubyonrails.org, and download the bundle  install package, then download and install RubyGem.<br />
When you have Gem installed, then you can install Rails by command:</p>
<p style="text-align: left;"><em>gem install rails</em></p>
<p><strong>Configure MySQL</strong></p>
<p>By default, ROR bundle comes with a light weight database carrier call SQLite but I think that many web developers are fans of MySQL. To install MySQL :</p>
<p style="text-align: left;"><em>gem install mysql</em></p>
<p>After installing MySQL, when you start your application, you may suffer the following error:</p>
<p><em>"!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql."</em></p>
<p>The problem is because MySQL 5.1 client library doesn't play well with Rails - the solution is simple however:</p>
<ul>
<li> download older MySQL client library, for example one from InstantRails: <a href="http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll" target="_blank">http://instantrails.rubyforge.org/svn/t ... bmySQL.dll</a></li>
<li>copy the downloaded file to C:\Ruby\bin (or wherever you installed Ruby)</li>
</ul>
<p><strong>Create ROR application</strong></p>
<p style="text-align: left;"><em>rails flickr</em></p>
<p style="text-align: center;">The above command will generate a ROR standard directory structure<br />
<img class="aligncenter size-full wp-image-113" title="ruby_create" src="http://www.webnoob.com/wp-content/uploads/2009/06/ruby_create.jpg" alt="ruby_create" width="540" height="174" /><br />
<img class="aligncenter size-full wp-image-114" title="ruby_dir" src="http://www.webnoob.com/wp-content/uploads/2009/06/ruby_dir.jpg" alt="ruby_dir" width="193" height="274" /></p>
<p>Change the configuration of the database in Rails application:<br />
Browse to flickr/config, open the file database.yml  by wordpad and make some change:</p>
<div class="igBar"><span id="ljava-73"><a href="#" onclick="javascript:showPlainTxt('java-73'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-73">
<div class="java"># MySQL version</p>
<p>#   gem install<br />
development:<br />
adapter: mysql<br />
database: ruby<br />
encoding: utf8<br />
username: root<br />
password: admin<br />
host: localhost</div>
</div>
</div>
<p>
* You may need to change the values database, username and password to the setting on your machine</p>
<p><strong>Start webrick server:</strong></p>
<p style="text-align: left;">In flickr directory:</p>
<p style="text-align: left;"><em>ruby script/server</em></p>
<p>By default, webrick server will start on port 3000, open a web browser and type in:</p>
<p><a href="http://localhost:3000/flickr/">http://localhost:3000/flickr/</a></p>
<p>to view your ROR application.</p>
<blockquote><p>Note: even that your application does not need to use any database, you still have to install and config MySQL ( or another database carrier ) accurately in order for ROR to start a service.</p></blockquote>
<p>Now, you have everything ready, let start to code.</p>
<h3>Coding</h3>
<p>ROR follows MVC model with a very strict naming convention, you need to pay a good attention to the names of model, view and controller</p>
<p><strong>Model</strong></p>
<p>To create a ROR model, you can go flickr/app/models , then create a new file named flickr.rb, but there is a better way</p>
<p style="text-align: left;"><em>rails script/create model flickr</em></p>
<p>This command will automatically generate all necessary files and folders, such as controller, view, layout, test and set up the configuration as well.<br />
Browse to flickr/app/models/ and open file flickr.rb to add some codes.<br />
We are going to use 3 APIs from Flickr:</p>
<p><a href="http://api.flickr.com/services/rest/?method=flickr.panda.getList">flickr.panda.getList</a> : get a list of available Pandas<br />
<a href="http://api.flickr.com/services/rest/?method=flickr.panda.getPhotos">flickr.panda.getPhotos</a> : get a list of photos under a Panda name<br />
<a href="http://api.flickr.com/services/rest/?method=flickr.photos.getInfo">lickr.photos.getInfo</a>: get information of a specific photo.</p>
<blockquote><p>Noting that , in order to use Flickr APIs, you need to have a API key, to obtain a new key go to<br />
<a href="http://www.flickr.com/services/api/keys/apply/">http://www.flickr.com/services/api/keys/apply/</a></p></blockquote>
<p>Because Flickr API will return a XML document, so we need to have to import rexml package to handle XML and of course net package to send REST request, you model will start with:<br />
<em>require 'net/http'<br />
require 'rexml/document'</em><br />
<strong>Sample code :</strong></p>
<div class="igBar"><span id="ljava-74"><a href="#" onclick="javascript:showPlainTxt('java-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-74">
<div class="java">require <span style="color: #ff0000;">'net/http'</span><br />
require <span style="color: #ff0000;">'rexml/document'</span><br />
# key : ce8cc447f982b2f864ba423b8dff3e1a<br />
# secret : 3abb6ef1ec9e1d75<br />
# http:<span style="color: #808080; font-style: italic;">//www.flickr.com/services/api/explore/?method=flickr.panda.getList</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Flickr<br />
#@@proxy_addr = <span style="color: #ff0000;">'localhost'</span><br />
#@@proxy_port = <span style="color: #cc66cc;">8000</span><br />
@@api_key=<span style="color: #ff0000;">''</span><br />
@@uri_list = <span style="color: #ff0000;">'http://api.flickr.com/services/rest/?method=flickr.panda.getList'</span><br />
@@uri = <span style="color: #ff0000;">'http://api.flickr.com/services/rest/?method=flickr.panda.getPhotos'</span><br />
@@uri_info = <span style="color: #ff0000;">'http://api.flickr.com/services/rest/?method=flickr.photos.getInfo'</span></p>
<p>def initialize<span style="color: #66cc66;">&#40;</span>key<span style="color: #66cc66;">&#41;</span><br />
@@api_key = key<br />
@@uri =<span style="color: #ff0000;">"#{@@uri}&amp;amp;api_key=#{@@api_key}"</span><br />
@@uri_list =<span style="color: #ff0000;">"#{@@uri_list}&amp;amp;api_key=#{@@api_key}"</span><br />
@@uri_info =<span style="color: #ff0000;">"#{@@uri_info}&amp;amp;api_key=#{@@api_key}"</span><br />
end</p>
<p>def get_list<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
#res = Net::<span style="color: #006600;">HTTP</span>::<a href="http://www.google.com/search?q=allinurl%3AProxy+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Proxy</span></a><span style="color: #66cc66;">&#40;</span>@@proxy_addr, @@proxy_port<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>@@uri_list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
res = Net::<span style="color: #006600;">HTTP</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>@@uri_list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
xml = REXML::<a href="http://www.google.com/search?q=allinurl%3ADocument+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Document</span></a>.<span style="color: #000000; font-weight: bold;">new</span><span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span><br />
re=<span style="color: #ff0000;">"<br />
&lt;ul id="</span>list<span style="color: #ff0000;">"&gt;"</span><br />
xml.<span style="color: #006600;">elements</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'rsp/pandas/panda'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |e|<br />
re =&nbsp; re + <span style="color: #ff0000;">"<br />
&nbsp; &nbsp; &lt;li&gt;"</span> +<span style="color: #ff0000;">"&lt;a href="</span>\&amp;quot;javascript:getPanda<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#{e.text}'</span><span style="color: #66cc66;">&#41;</span>\&amp;quot;<span style="color: #ff0000;">"&gt;"</span>+ e.<span style="color: #006600;">text</span>+<span style="color: #ff0000;">"&lt;/a&gt;"</span>+ <span style="color: #ff0000;">"&lt;/li&gt;<br />
"</span><br />
end<br />
re = re + <span style="color: #ff0000;">"&lt;/ul&gt;<br />
"</span><br />
puts re<br />
<span style="color: #000000; font-weight: bold;">return</span> re<br />
end</p>
<p>def get_panda<span style="color: #66cc66;">&#40;</span>panda_name,limit=<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><br />
panda_name = panda_name.<span style="color: #006600;">gsub</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">' '</span>,<span style="color: #ff0000;">'+'</span><span style="color: #66cc66;">&#41;</span><br />
uri_temp = <span style="color: #ff0000;">"#{@@uri}&amp;amp;panda_name=#{panda_name}&amp;amp;per_page=#{limit}&amp;amp;page=1"</span><br />
#res= Net::<span style="color: #006600;">HTTP</span>::<a href="http://www.google.com/search?q=allinurl%3AProxy+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Proxy</span></a><span style="color: #66cc66;">&#40;</span>@@proxy_addr, @@proxy_port<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>uri_temp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
res= Net::<span style="color: #006600;">HTTP</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>uri_temp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
xml = REXML::<a href="http://www.google.com/search?q=allinurl%3ADocument+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Document</span></a>.<span style="color: #000000; font-weight: bold;">new</span><span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span><br />
photos =<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span></p>
<p>xml.<span style="color: #006600;">elements</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'rsp/photos/photo'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |e|<br />
photos &amp;lt;&amp;lt;e<br />
end<br />
i=<span style="color: #cc66cc;">0</span><br />
re= <span style="color: #ff0000;">"<br />
&lt;div id="</span>panda_inside<span style="color: #ff0000;">"&gt;<br />
&lt;h2&gt;List #{limit} photos of Panda #{panda_name}&lt;/h2&gt;<br />
"</span><br />
re= re + <span style="color: #ff0000;">"<br />
&lt;ul id="</span>panda_list<span style="color: #ff0000;">"&gt;"</span><br />
<span style="color: #b1b100;">while</span> i</p>
<p><span style="color: #b1b100;">if</span> photos<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"title"</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">""</span><br />
photos<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"title"</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #ff0000;">"No Title"</span><br />
end<br />
re= re + <span style="color: #ff0000;">"<br />
&nbsp; &nbsp; &lt;li&gt;"</span> + <span style="color: #ff0000;">"&lt;a href="</span>\<span style="color: #ff0000;">"&gt;"</span> + photos<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"title"</span><span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">"&lt;/a&gt;&lt;/li&gt;<br />
&lt;a href="</span>\<span style="color: #ff0000;">"&gt;"</span><br />
i+=<span style="color: #cc66cc;">1</span><br />
end<br />
re=re + <span style="color: #ff0000;">"&lt;/a&gt;&lt;/ul&gt;<br />
&lt;/div&gt;<br />
&lt;a href="</span>\<span style="color: #ff0000;">"&gt;"</span><br />
puts re<br />
<span style="color: #000000; font-weight: bold;">return</span> re<br />
end&lt;/a&gt;</p>
<p>def get_info<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><br />
uri_temp = <span style="color: #ff0000;">"#{@@uri_info}&amp;amp;photo_id=#{id}"</span><br />
#res= Net::<span style="color: #006600;">HTTP</span>::<a href="http://www.google.com/search?q=allinurl%3AProxy+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Proxy</span></a><span style="color: #66cc66;">&#40;</span>@@proxy_addr, @@proxy_port<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>uri_temp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
res= Net::<span style="color: #006600;">HTTP</span>.<span style="color: #006600;">get_response</span><span style="color: #66cc66;">&#40;</span>URI.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>uri_temp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">body</span><br />
xml = REXML::<a href="http://www.google.com/search?q=allinurl%3ADocument+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Document</span></a>.<span style="color: #000000; font-weight: bold;">new</span><span style="color: #66cc66;">&#40;</span>res<span style="color: #66cc66;">&#41;</span><br />
owner=<span style="color: #ff0000;">""</span><br />
title=<span style="color: #ff0000;">""</span><br />
date=<span style="color: #ff0000;">""</span><br />
xml.<span style="color: #006600;">elements</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'rsp/photo/owner'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |e|<br />
owner = <span style="color: #ff0000;">"Author: "</span> + e.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"realname"</span><span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">". Location: "</span> + e.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"location"</span><span style="color: #66cc66;">&#93;</span><br />
end</p>
<p>xml.<span style="color: #006600;">elements</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'rsp/photo/title'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |e|<br />
title = e.<span style="color: #006600;">text</span><br />
end</p>
<p>xml.<span style="color: #006600;">elements</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'rsp/photo'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">do</span> |e|<br />
date= e.<span style="color: #006600;">attributes</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">"dateuploaded"</span><span style="color: #66cc66;">&#93;</span><br />
date = <a href="http://www.google.com/search?q=allinurl%3ADate+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Date</span></a>.<span style="color: #006600;">jd</span><span style="color: #66cc66;">&#40;</span>date.<span style="color: #006600;">to_i</span><span style="color: #66cc66;">&#41;</span><br />
date=date.<span style="color: #006600;">strftime</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">"%B %d, %Y"</span><span style="color: #66cc66;">&#41;</span></p>
<p>end<br />
re= <span style="color: #ff0000;">"&lt;span id="</span>info<span style="color: #ff0000;">"&gt;Title: "</span> + title + <span style="color: #ff0000;">"<br />
Uploaded date in Julian: "</span> + date.<span style="color: #006600;">to_s</span> + <span style="color: #ff0000;">"<br />
"</span> + owner + <span style="color: #ff0000;">"&lt;/span&gt;"</span><br />
<span style="color: #000000; font-weight: bold;">return</span> re<br />
end</p>
<p>def get_image<span style="color: #66cc66;">&#40;</span>farm,server,id,secret<span style="color: #66cc66;">&#41;</span><br />
src = <span style="color: #ff0000;">"'http://farm#{farm}.static.flickr.com/#{server}/#{id}_#{secret}.jpg'"</span><br />
img = <span style="color: #ff0000;">"&lt;img src="</span>#<span style="color: #66cc66;">&#123;</span>src<span style="color: #66cc66;">&#125;</span><span style="color: #ff0000;">" alt="</span><span style="color: #ff0000;">" /&gt;"</span><br />
info = get_info<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><br />
re = img + <span style="color: #ff0000;">"<br />
"</span> + info<br />
<span style="color: #000000; font-weight: bold;">return</span> re<br />
end<br />
end</div>
</div>
</div>
<p></p>
<p>if your program runs through a proxy, then uncomment</p>
<p><em>#@@proxy_addr = 'localhost'<br />
#@@proxy_port = 8000</em></p>
<p>and use the net command with proxy:</p>
<p><em>#res = Net::HTTP::Proxy(@@proxy_addr, @@proxy_port).get_response(URI.parse(@@uri_list)).body<br />
#res= Net::HTTP::Proxy(@@proxy_addr, @@proxy_port).get_response(URI.parse(uri_temp)).body<br />
#res= Net::HTTP::Proxy(@@proxy_addr, @@proxy_port).get_response(URI.parse(uri_temp)).body</em></p>
<p><strong>Controllers </strong><br />
<em>rails script/create model flickr </em></p>
<p>ROR will generate a controller name flickr_controller.rb under the directory  flickr/app/controllers/<br />
We will need three actions to handle to different request from the views:</p>
<ul>
<li>index</li>
<li>get_panda</li>
<li>get_image</li>
</ul>
<p>Sample code:</p>
<div class="igBar"><span id="ljava-75"><a href="#" onclick="javascript:showPlainTxt('java-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVA:</span>
<div id="java-75">
<div class="java"><span style="color: #000000; font-weight: bold;">class</span> FlickrController &amp;lt;ApplicationController&nbsp; &nbsp; &nbsp;@@flickr = Flickr.<span style="color: #000000; font-weight: bold;">new</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ce8cc447f982b2f864ba423b8dff3e1a'</span><span style="color: #66cc66;">&#41;</span>&nbsp; &nbsp;def index&nbsp; &nbsp; &nbsp;@fl = @@flickr&nbsp; &nbsp;end&nbsp; &nbsp;def get_panda&nbsp; &nbsp; &nbsp;@fl = @@flickr&nbsp; &nbsp; &nbsp;panda_name = params<span style="color: #66cc66;">&#91;</span>:panda_name<span style="color: #66cc66;">&#93;</span>&nbsp; &nbsp; &nbsp;re = @fl.<span style="color: #006600;">get_panda</span><span style="color: #66cc66;">&#40;</span>panda_name<span style="color: #66cc66;">&#41;</span>&nbsp; &nbsp; &nbsp;render :text=&amp;gt;re<br />
end</p>
<p>def get_image<br />
@fl = @@flickr<br />
farm = params<span style="color: #66cc66;">&#91;</span>:farm<span style="color: #66cc66;">&#93;</span><br />
server = params<span style="color: #66cc66;">&#91;</span>:server<span style="color: #66cc66;">&#93;</span><br />
id=params<span style="color: #66cc66;">&#91;</span>:id<span style="color: #66cc66;">&#93;</span><br />
secret = params<span style="color: #66cc66;">&#91;</span>:secret<span style="color: #66cc66;">&#93;</span><br />
re = @fl.<span style="color: #006600;">get_image</span><span style="color: #66cc66;">&#40;</span>farm,server,id,secret<span style="color: #66cc66;">&#41;</span><br />
render :text=&amp;gt;re<br />
end<br />
end</div>
</div>
</div>
<p></p>
<p><strong>Views</strong><br />
go to flickr/app/views/flickr/, create a file named  index.html.erb</p>
<div class="igBar"><span id="lhtml-76"><a href="#" onclick="javascript:showPlainTxt('html-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-76">
<div class="html">Ruby on Rails - Panda Search<br />
<span style="color: #ddbb00;">&amp;lt;</span>%= javascript_include_tag &quot;flickr.js&quot; %<span style="color: #ddbb00;">&amp;gt;</span>;<br />
<span style="color: #ddbb00;">&amp;lt;</span>%= stylesheet_link_tag &quot;flickr.css&quot; %<span style="color: #ddbb00;">&amp;gt;</span>;<br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"wraper"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"top"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #ddbb00;">&amp;lt;</span>%= image_tag &quot;rails.png&quot; %<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"header"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span> Ruby on Rails<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/h1.html"><span style="color: #000000; font-weight: bold;">&lt;h1&gt;</span></a></span>Flickr Panda Search<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1&gt;</span></span><br />
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span><span style="color: #ddbb00;">&amp;lt;</span>%= render :text=<span style="color: #ddbb00;">&amp;gt;</span>@fl.get_list() %<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></div>
</div>
</div>
<p></p>
<p><strong>JavaScript</strong>&lt;</p>
<p>The javascript will be stored under directory flickr/public/javascripts/<br />
Create a file named flickr.js and use the syntax bellow to include a javacript in the View<br />
<em>&lt;%= javascript_include_tag "flickr.js" %&gt;</em></p>
<div class="igBar"><span id="ljavascript-77"><a href="#" onclick="javascript:showPlainTxt('javascript-77'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-77">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> asyncRequest;</p>
<p><span style="color: #003366; font-weight: bold;">function</span> getPanda<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">//alert(query.value);</span></p>
<p><span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span><br />
asyncRequest = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
asyncRequest.<span style="color: #006600;">onreadystatechange</span> = displayPanda;</p>
<p><span style="color: #003366; font-weight: bold;">var</span> url =<span style="color: #3366CC;">"http://localhost:3000/flickr/get_panda?panda_name="</span> + <span style="color: #000066;">name</span>;<br />
url=url+<span style="color: #3366CC;">"&amp;amp;sid="</span>+Math.<span style="color: #006600;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #009900; font-style: italic;">//alert(url);</span><br />
asyncRequest.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'GET'</span>,url,<span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
asyncRequest.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;<br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'panda'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;img id=&quot;loader&quot; src=&quot;/images/loader.gif&quot; alt=&quot;&quot; /&gt;'</span>;<br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">''</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>exception<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>exception<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
setCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"panda"</span>,<span style="color: #000066;">name</span>,<span style="color: #CC0000;">180</span>,<span style="color: #3366CC;">'/'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #003366; font-weight: bold;">function</span> displayPanda<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">//alert(query.value);</span><br />
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>asyncRequest.<span style="color: #006600;">readyState</span>==<span style="color: #CC0000;">4</span> &amp;amp;&amp;amp; asyncRequest.<span style="color: #000066;">status</span>==<span style="color: #CC0000;">200</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> display =asyncRequest.<span style="color: #006600;">responseText</span>;<br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'panda'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span>=display;<br />
setCookie<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"panda_list"</span>,display,<span style="color: #CC0000;">180</span>,<span style="color: #3366CC;">'/'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> getImage<span style="color: #66cc66;">&#40;</span>farm,server,id,secret<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span><br />
asyncRequest = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
asyncRequest.<span style="color: #006600;">onreadystatechange</span> = displayImage;</p>
<p><span style="color: #003366; font-weight: bold;">var</span> url =<span style="color: #3366CC;">"http://localhost:3000/flickr/get_image?farm="</span><br />
+ farm + <span style="color: #3366CC;">"&amp;amp;server="</span> + server + <span style="color: #3366CC;">"&amp;amp;id="</span> + id + <span style="color: #3366CC;">"&amp;amp;secret="</span> + secret;<br />
url=url+<span style="color: #3366CC;">"&amp;amp;sid="</span>+Math.<span style="color: #006600;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
asyncRequest.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'GET'</span>,url,<span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
asyncRequest.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;<br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = <span style="color: #3366CC;">'&lt;img id=&quot;loading&quot; src=&quot;/images/loading.gif&quot; alt=&quot;&quot; /&gt;'</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>exception<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>exception<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></p>
<p><span style="color: #003366; font-weight: bold;">function</span> displayImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>asyncRequest.<span style="color: #006600;">readyState</span>==<span style="color: #CC0000;">4</span> &amp;amp;&amp;amp; asyncRequest.<span style="color: #000066;">status</span>==<span style="color: #CC0000;">200</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> display =asyncRequest.<span style="color: #006600;">responseText</span>;<br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'img'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span>=display;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p><strong>CSS</strong></p>
<p>The stylesheet will be stored under directory flickr/public/stylesheets/<br />
Create a file name flickr.css and use the syntax bellow to include a CSS in the View</p>
<p><em>&lt;%= stylesheet_link_tag "flickr.css" %&gt;</em></p>
<div class="igBar"><span id="lcss-78"><a href="#" onclick="javascript:showPlainTxt('css-78'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CSS:</span>
<div id="css-78">
<div class="css">body <span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">margin</span>:0px;padding:0px;font-family: Verdana, Arial, <span style="color: #993333;">sans-serif</span>;font-<span style="color: #000000; font-weight: bold;">size</span>:9pt;<span style="color: #66cc66;">&#125;</span><br />
#wraper<span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">width</span>:1003px;min-<span style="color: #000000; font-weight: bold;">height</span>:600px;margin<span style="color: #3333ff;">:<span style="color: #cc66cc;">0</span> </span>auto;border<span style="color: #3333ff;">:7px </span>inset #666666; padding<span style="color: #3333ff;">:10px </span>20px 20px 20px;<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #cc00cc;">#header <span style="color: #66cc66;">&#123;</span></span>font-<span style="color: #000000; font-weight: bold;">size</span>:20pt;font-weight:<span style="color: #993333;">bold</span>;<span style="color: #66cc66;">&#125;</span><br />
#footer<span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">clear</span>:<span style="color: #993333;">both</span>;<span style="color: #66cc66;">&#125;</span><br />
.<span style="color: #993333;">middle</span><span style="color: #66cc66;">&#123;</span><span style="color: #000000; font-weight: bold;">float</span>:<span style="color: #000000; font-weight: bold;">left</span>;vertical-align:text-<span style="color: #000000; font-weight: bold;">top</span>;<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<h3>Screen shots:</h3>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-95" title="flickr_2" src="http://www.webnoob.com/wp-content/uploads/2009/06/flickr_21.jpg" alt="flickr_2" width="540" height="360" /></p>
<p>Download: <a href="http://www.webnoob.com/audio/flickr_panda.rar" target="_self">flick_panda.rar</a></p>
<p style="text-align: right;">NDLoc, 28/06/2009</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webnoob.com/2009/06/flickr-panda/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Allimports &#8211; MVC and Ajax CMS</title>
		<link>http://www.webnoob.com/2009/06/allimports/</link>
		<comments>http://www.webnoob.com/2009/06/allimports/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 04:03:24 +0000</pubDate>
		<dc:creator>NDLoc</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.webnoob.com/?p=64</guid>
		<description><![CDATA[Allimports is content management system (CMS) that I developed for a car importer locating in Haberfield, Sydney. The system is a fully functional Ajax CMS in which I created a new MVC model on my own.
The website provides all features of a standard CMS and addresses the specific functions required for a car trading website, [...]]]></description>
			<content:encoded><![CDATA[<p>Allimports is content management system (CMS) that I developed for a car importer locating in Haberfield, Sydney. The system is a fully functional Ajax CMS in which I created a new MVC model on my own.</p>
<p>The website provides all features of a standard CMS and addresses the specific functions required for a car trading website, such as Special Offer, New Arrival list, multiple input search, email spam, to name just a few.</p>
<p>The system is developed by using the cutting edge web techniques, Ajax and MVC. Coining in 2005, for the last couple of years the term Ajax has captured a great concern of all web developers around the world and be considered as the hottest technique nowadays.</p>
<blockquote><p><strong>Ajax</strong>, Asynchronous JavaScript and XML, is a group of interrelated web development techniques used on the client-side to create interactive web applications or rich Internet applications. With Ajax, web applications can retrieve data from the server asynchronously in the background using XMLHttpRequest object  without interfering with the display and behavior of the existing page(wikipedia).</p></blockquote>
<p><span id="more-64"></span></p>
<h3>Web site: <a href="http://www.allimports.com.au" target="_blank">www.allimports.com.au</a></h3>
<h3>Functionalities</h3>
<p><strong>Visitor:</strong></p>
<ul>
<li>View car list, special offer, details, photo show (using Mootool framwork)</li>
<li>Search, send enquiry, subscribe</li>
<li>....</li>
</ul>
<p><strong>Users:</strong></p>
<ul>
<li> View cars, add new, update, remove.</li>
<li> Add, remove, update photos</li>
<li> View enquiries, reply, remove</li>
<li> View user, update user state, unsubscribe</li>
<li> Manage profile</li>
<li>....</li>
</ul>
<p><strong>Admin</strong></p>
<ul>
<li> Add, remove users, disable users</li>
<li> Change system setting.</li>
<li>....</li>
</ul>
<h3>The new MCV model</h3>
<blockquote><p><strong>Model–view–controller</strong> (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other.</p>
<p>- The <em>model</em> represents the information (the data) of the application.<br />
- The <em>view</em> corresponds to elements of the user interface such as text, checkbox items, etc., and is responsible for the communication of data from the model to the user interface.<br />
- The <em>controller</em> manages the communication of data from the user interface to the model, and the application of business rules to this data.</p>
<p>Typically, a <em>view</em> and a <em>controller</em> exist as a pair, and many such pairs exist in an application, each corresponding to very specific user interface elements.</p></blockquote>
<p>In the analysis phase of the project, I intended to use an open source framework like CakePHP or Zend, therefore  I have CakePHP installed and configured properly on my machine already. However, after examining CakePHP, I realized that the key factor underlying the MVC model of the framework is XMLHttpRequest, so I decided to develop a MVC model on my own using XMLHttpRequest as the request carrier.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-70" title="allimports" src="http://www.webnoob.com/wp-content/uploads/2009/06/allimports.jpg" alt="MVC model" width="547" height="262" /></p>
<p>Below is how models, views and controllers communicate with each other:</p>
<ul>
<li> When a request is sent from a View, such as user clicks a link or submit a search query, a JavaScript function of a Carrier is raised and send a XMLHttpRequest to the controller match with this view.</li>
<li> The action of  the Controller will user a Model to perform some tasks, the return will be put back to the XMLHttpRequest</li>
<li> The View then receives response of the XMLHttpRequest and partially update the page to display the result.</li>
</ul>
<h3>Sample code</h3>
<p><strong>class.user.php</strong></p>
<div class="igBar"><span id="lphp-82"><a href="#" onclick="javascript:showPlainTxt('php-82'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-82">
<div class="php"><span style="color:#616100;">require_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'setting.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#000000; font-weight:bold;">class</span> User<span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#000000; font-weight:bold;">function</span> addSpecialDeal<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$car_id</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$start_date</span> = <a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'Y-m-d'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$query</span>=<span style="color:#FF0000;">"INSERT INTO specials(car_id,start_date) VALUES("</span>.<span style="color:#0000FF;">$car_id</span>.<span style="color:#FF0000;">",'"</span>.<span style="color:#0000FF;">$start_date</span>.<span style="color:#FF0000;">"')"</span>;<br />
<span style="color:#0000FF;">$result</span>= <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$query</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
or <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Error addSpecialDeal - the query could not be executed<span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">return</span> <span style="color:#0000FF;">$result</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#000000; font-weight:bold;">function</span> updateCarState<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$car_id</span>,<span style="color:#0000FF;">$value</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$query</span>=<span style="color:#FF0000;">"UPDATE cars SET value="</span>.<span style="color:#0000FF;">$value</span>.<span style="color:#FF0000;">" WHERE car_id="</span>.<span style="color:#0000FF;">$car_id</span>;<br />
<span style="color:#0000FF;">$result</span>= <a href="http://www.php.net/mysql_query"><span style="color:#000066;">mysql_query</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$query</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
or <a href="http://www.php.net/die"><span style="color:#000066;">die</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">"Error updateCarState - the query could not be executed<span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">return</span> <span style="color:#0000FF;">$result</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p>
<strong><br />
controller-subscribe.php</strong></p>
<div class="igBar"><span id="lphp-83"><a href="#" onclick="javascript:showPlainTxt('php-83'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-83">
<div class="php"><span style="color:#616100;">require_once</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'../model/class.view.php'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$name</span> = <span style="color:#0000FF;">$_REQUEST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">"name"</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
<span style="color:#0000FF;">$emial</span> = <span style="color:#0000FF;">$_REQUEST</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#FF0000;">"email"</span><span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
<span style="color:#0000FF;">$inform</span> =<span style="color:#FF0000;">""</span>;<br />
<span style="color:#0000FF;">$check</span>=<span style="color:#000000; font-weight:bold;">true</span>;</p>
<p><span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$name</span>==<span style="color:#FF0000;">""</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$inform</span> =<span style="color:#0000FF;">$inform</span>.<span style="color:#FF0000;">"<br />
Please fill in your name"</span>;<br />
<span style="color:#0000FF;">$check</span>=<span style="color:#000000; font-weight:bold;">false</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$emial</span>==<span style="color:#FF0000;">""</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$inform</span> =<span style="color:#0000FF;">$inform</span>.<span style="color:#FF0000;">"<br />
Please fill in your email"</span>;<br />
<span style="color:#0000FF;">$check</span>=<span style="color:#000000; font-weight:bold;">false</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$check</span>==<span style="color:#000000; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span>View::<span style="color:#006600;">checkSubscribe</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$emial</span><span style="color:#006600; font-weight:bold;">&#41;</span>==<span style="color:#000000; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$result</span> = View::<span style="color:#006600;">subscribe</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$name</span>,<span style="color:#0000FF;">$emial</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$result</span>==<span style="color:#000000; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><br />
<span style="color:#0000FF;">$inform</span> = <span style="color:#FF0000;">'&lt;span class=&quot;text_alert&quot;&gt;You have joined Allimports.&lt;/span&gt;'</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">else</span> <span style="color:#0000FF;">$inform</span> = <span style="color:#FF0000;">'&lt;span class=&quot;text_alert&quot;&gt;Please try again.&lt;/span&gt;'</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">else</span> <span style="color:#0000FF;">$inform</span> = <span style="color:#FF0000;">'&lt;span class=&quot;text_alert&quot;&gt;You already subscibed to Allimports.&lt;/span&gt;'</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<a href="http://www.php.net/print"><span style="color:#000066;">print</span></a> <span style="color:#0000FF;">$inform</span>;</div>
</div>
</div>
<p>
<strong><br />
class.user.js</strong></p>
<div class="igBar"><span id="ljavascript-84"><a href="#" onclick="javascript:showPlainTxt('javascript-84'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-84">
<div class="javascript"><span style="color: #003366; font-weight: bold;">var</span> xmlHttp;<br />
<span style="color: #003366; font-weight: bold;">function</span> GetXmlHttpObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> xmlHttp = <span style="color: #003366; font-weight: bold;">null</span>;<br />
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// Firefox, Opera 8.0+, Safari</span><br />
xmlHttp = <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// Internet Explorer</span><br />
<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span><br />
xmlHttp = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"Msxml2.XMLHTTP"</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
xmlHttp = <span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"Microsoft.XMLHTTP"</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000066; font-weight: bold;">return</span> xmlHttp;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">function</span> removeEnquiry<span style="color: #66cc66;">&#40;</span>id,page<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
pos=<span style="color: #3366CC;">"car_list"</span>;<br />
xmlHttp = GetXmlHttpObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>xmlHttp == <span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"Your browser does not support AJAX!"</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066; font-weight: bold;">return</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> url = <span style="color: #3366CC;">"../controller/controller-remove-enquiry.php?id="</span>+id+<span style="color: #3366CC;">"&amp;amp;page="</span>+page;<br />
url=url+<span style="color: #3366CC;">"&amp;amp;sid="</span>+Math.<span style="color: #006600;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
xmlHttp.<span style="color: #006600;">onreadystatechange</span> = <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>xmlHttp.<span style="color: #006600;">readyState</span> == <span style="color: #CC0000;">4</span> || xmlHttp.<span style="color: #006600;">readyState</span> == <span style="color: #3366CC;">"complete"</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span>pos<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">innerHTML</span> = xmlHttp.<span style="color: #006600;">responseText</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span>;<br />
xmlHttp.<span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">"GET"</span>, url, <span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
xmlHttp.<span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<h3>Screen shots:</h3>
<p style="text-align: center;"><strong>Homepage </strong><br />
<img class="aligncenter size-full wp-image-74" title="allimports_screenshot" src="http://www.webnoob.com/wp-content/uploads/2009/06/allimports_screenshot.jpg" alt="allimports_screenshot" width="540" height="375" /></p>
<p><strong>User dashboard</strong></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-75" title="allimports_user" src="http://www.webnoob.com/wp-content/uploads/2009/06/allimports_user.jpg" alt="allimports_user" width="540" height="338" /></p>
<p style="text-align: right;">NDLoc, 27/06/2009</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webnoob.com/2009/06/allimports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
