<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6262460674252256073</id><updated>2012-02-16T05:07:30.200-08:00</updated><category term='Hibernate'/><category term='Hudson'/><category term='Grails Troubleshoot'/><category term='Grails'/><category term='JMS'/><category term='Spring'/><category term='Java'/><category term='REST'/><category term='Groovy'/><title type='text'>about java, groovy, grails, software development and more...</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6364895284339048737</id><published>2011-03-15T11:52:00.000-07:00</published><updated>2011-03-15T11:53:19.387-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><title type='text'>Basics of Java's Garbage Collection</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div class="MsoNormal" style="color: #134f5c;"&gt;&lt;b&gt;Garbage collection&lt;/b&gt;:&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Every java program runs as a process and is allocated some memory. The memory allocated to the java process is called &lt;i&gt;process heap.&lt;/i&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;The &lt;i&gt;java heap &lt;/i&gt;i.e. the java &lt;i&gt;object &lt;/i&gt;heap, is part of the process heap. The java object heap (also called just java heap) store the java objects i.e. object instances. The java heap can be configured using the several command line parameters.&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Garbage collection is the mechanism used by j&lt;span lang="EN-GB"&gt;ava to reclaim or recollect the space occupied by objects that are not reachable from the program. This is necessary to ensure that the application does not run out of memory. &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;An object that cannot be reached from any reference within the running program is eligible for garbage collection.&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;The simplest form of GC iterates through all the references within the running program and marks all the objects that are reachable via those references directly or indirectly. At the end of this process, any object that is not marked, is considered garbage and becomes eligible for collection. &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;It’s obvious, that in case of this particular algorithm, the duration of the GC is relative of the number of objects referenced by the program. &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="color: #134f5c; margin-left: 0.25in; text-indent: -0.25in;"&gt;&lt;span style="font-family: Symbol;"&gt;&lt;/span&gt;&lt;b&gt;Generational Collection&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;In most applications, a larger number of objects are short-lived. i.e. there are relatively few objects that are created when the application is initialized and live until the end; which means, it makes sense for the GC to focus on the objects that are short-lived. &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;To leverage this common property noticed in most applications, JVM manages the memory in generations i.e. it divides the object memory into groups depending on the age of the objects i.e. short-lived objects would stay in the &lt;i&gt;young-generation &lt;/i&gt;and longer-living objects would be moved to the &lt;i&gt;tenured-generation. &lt;/i&gt;&amp;nbsp;New objects are allocated in the &lt;i&gt;young-generation &lt;/i&gt;and longer-living objects that have survived a good number of collections are then moved to the &lt;i&gt;tenured (older) generation.&lt;/i&gt; &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;There is also a &lt;i&gt;permanent-generation &lt;/i&gt;where metadata for classes and methods are stored. The default permanent generation is generally enough, but applications that need to load a large number of classes may want to increase the perm-gen size. This can be done using the&lt;b&gt; &lt;/b&gt;&lt;b&gt;&lt;span lang="EN-GB"&gt;XX:MaxPermSize&lt;/span&gt;&lt;/b&gt;&lt;span lang="EN-GB"&gt; option.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;b&gt;But how does this grouping help?&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;As we have already seen, the amount of time spent in GC depends on the number of &lt;i&gt;live objects. &lt;/i&gt;Since most objects in the &lt;i&gt;young-generation&lt;/i&gt; would be short-lived (&lt;i&gt;die quickly after their creation)&lt;/i&gt;, running a GC on this area should not take very long. There would be only a few live objects that would need to be copied, and hence the GC pause would be significantly small. Also, the &lt;i&gt;young-generation&lt;/i&gt; should be optimally sized which would deter the need to garbage collect the objects too soon. If the young-generation is too small, the GC would have to be called too soon, which means a lot of objects in the area would still be alive. The purpose would be defeated. Hence, the young-generation should be big enough to leverage the infant mortality (i.e. most objects will die soon – but not too soon). The young-generation should be around 1/3&lt;sup&gt;rd&lt;/sup&gt; of the total heap size.&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;b style="color: purple;"&gt;Minor collections:&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;The young-generation is divided into an area called ‘eden’ where new allocations are made initially and 2 ‘survivor spaces’ which play the part of a copying collector. When the &lt;i&gt;‘eden’&lt;/i&gt; becomes full, a &lt;i&gt;minor collection &lt;/i&gt;takes place. &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;The minor collection collects the dead objects from eden and any live objects are copied from the eden into one of the &lt;i&gt;‘survivor space’&lt;/i&gt;. If a minor collection runs again, and the eden is again found full, the live objects are copied from eden and the 1&lt;sup&gt;st&lt;/sup&gt; survivor space to the 2&lt;sup&gt;nd&lt;/sup&gt; survivor space, so on and so forth. &lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Thus every minor collection will copy live object from Eden and one of the survivor spaces into the second survivor space. Also, any live objects that have survived a certain number of collections are promoted to the tenured generation.&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Note: In case of such generational collection, the collector does not visit the tenured generation objects referred by the young generation objects. There are ways to track inter-generational references (out-of-scope for the purpose of this article)&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="color: purple;"&gt;&lt;b&gt;Major collections:&lt;/b&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;If the minor collection is not able to reclaim significant amount of memory, a major collection takes place on higher generations. Such collections collect both the generations i.e. the young as well as the tenured. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;b&gt;Note: &lt;/b&gt;&amp;nbsp;When we call System.gc(), a major collection is called through. Since major collections take a longer time to run, we should be very careful about using System.gc() since it could have a performance impact.&lt;/div&gt;&lt;div class="NormalLeft127cm"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="color: #134f5c; margin-left: 0in;"&gt;&lt;b&gt;Collection Strategies:&lt;/b&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;There are a couple of GC strategies that you can choose from to improve the performance/throughput. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;b&gt;&lt;span style="color: purple;"&gt;1. Serial collector:&lt;/span&gt;&lt;/b&gt; What we discussed until now is the serial way of collection also known as ‘Stop-the-world’ collection. As the name suggests, program execution is halted when this kind of collector runs.&amp;nbsp; Advantage is that it guarantees that no new object is allocated and object reachability does not change until the collection finishes. Disadvantage is that program execution is paused. This collection strategy is simplest and least complex. This strategy is useful for applications that are not interactive.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="margin-left: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;b&gt;&lt;span style="color: purple;"&gt;2. Parallel collector:&lt;/span&gt;&lt;/b&gt; Also called ‘&lt;i&gt;&lt;span style="font-family: Arial;"&gt;throughput&lt;/span&gt;&lt;/i&gt; collector’. In this case, parallel collection is done for the young-generation i.e. multiple threads are used for minor collections. The tenured generation collection still remains serial. New object allocations are allowed while collection is being carried out. Various locking mechanisms are used to achieve this kind of parallel behavior. This strategy is preferred in case of multiple processors. This strategy can be enabled using the &lt;b&gt;-XX:+UseParallelGC&lt;/b&gt; option. This strategy is suitable for applications with a large set of short-lived data (a large &lt;i&gt;&lt;span style="font-family: Arial;"&gt;young&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/i&gt; generation) and multiple processors.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; color: purple; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;b style="color: purple;"&gt;3. Incremental collector: &lt;/b&gt;In this strategy, GC is performed in phases. And program execution is resumed between these phases. This strategy can be enabled using the &lt;b&gt;-XX:+UseTrainGC&lt;/b&gt; option. This is not being used after Java 1.4 versions. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;b&gt;&lt;span style="color: purple;"&gt;4. Concurrent collector:&lt;/span&gt;&lt;/b&gt; In this strategy, most of the collection takes places in parallel with program execution. Pause periods are very short. Concurrent collections are done for both minor and major collections. This strategy can be enabled using the &lt;b&gt;-XX:+UseConcMarkSweepGC&lt;/b&gt; option. This strategy is suitable for applications with a large set of long-lived data (a large &lt;i&gt;&lt;span style="font-family: Arial;"&gt;tenured&lt;/span&gt;&lt;/i&gt; generation) and multiple processors.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; color: #134f5c; margin-left: 0in; padding: 0in;"&gt;&lt;b&gt;How can GC be monitored?&lt;/b&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;The following command line options will output the details of GC:&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;-verbose:gc&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;-XX:+PrintGCDetails&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;-Xloggc:gc.log &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; color: #134f5c; margin-left: 0in; padding: 0in;"&gt;&lt;b&gt;Heap and generation sizes&lt;/b&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;i&gt;Throughput&lt;/i&gt; is the percentage of the total run time not spent performing GC. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;i&gt;Pauses &lt;/i&gt;are the times when the application execution stops to allow GC to run.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;Different applications have different requirements as far as the above two metrics are concerned. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;The throughput of the application depends on the memory available. If the generations are small, lesser time will be spent in GC, hence throughput would be higher. Thus throughput is inversely proportional to the amount of memory available.&amp;nbsp; Since collections occur when generations fill up, throughput is inversely proportional to the amount of memory available&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;Total available memory is the most important factor affecting garbage collection performance.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;When the JVM initializes, some space is reserved for the java object heap. This heap size can be specified using the -Xmx option.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;b&gt;-Xms&lt;/b&gt; specified the minimum heap size and &lt;b&gt;–Xmx&lt;/b&gt; specifies the maximum heap size.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;If –Xms is less than –Xmx, then some remainder is allocated to a so-called ‘virtual space’. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;This virtual space is adjusted after each collection to maintain the ratio of the ‘available space’ to the ‘space used by live objects’ within a range. &lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;This range can be specified using the - XX:MinHeapFreeRatio and –XX:MaxHeapFreeRatio parameters.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;Some other interesting options are :&lt;/div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:NewRatio: ratio of the sizes of the young and old generations.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:NewSize: size of the young generation in bytes&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:SurvivorRatio: ratio of the sizes of the eden and the survivor spaces.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:GCTimeRatio: specifies a throughput goal. This option specifies the percentage of time to be spent in program execution instead of garbage collection.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:MaxGCPauseMillis: specifies a max pause goal. Specifies the maximum time the program execution can be halted to allow GC to finish in milliseconds. &lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;-XX:+DisableExplicitGC: If this option is enabled, any call to System.gc() would not result into a garbage collection cycle.&lt;/li&gt;&lt;/ul&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;For a detailed list of GC tuning options, visit &lt;a href="http://www.blogger.com/:%20http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html%20%20%20"&gt;this link&lt;/a&gt;.&lt;/div&gt;&lt;div class="NormalLeft127cm" style="border: medium none; margin-left: 0in; padding: 0in;"&gt;&amp;nbsp; &lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6364895284339048737?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6364895284339048737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/03/basics-of-javas-garbage-collection.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6364895284339048737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6364895284339048737'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/03/basics-of-javas-garbage-collection.html' title='Basics of Java&apos;s Garbage Collection'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-5880643385182859812</id><published>2011-03-04T01:27:00.000-08:00</published><updated>2011-03-04T01:27:25.683-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Custom error page for your grails application</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;I recently added a custom error page to my application, so that the users don't see the stacktrace and are shown a friendly message in case of a runtime error.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I simply added a controller that checks for the environment in which the application is running, and accordingly renders different views:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;import grails.util.*;&lt;br /&gt;&lt;br /&gt;class ErrorsController {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def serverError = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; def env = GrailsUtil.environment;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(env == "production")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; render(view:'/serverError')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; render(view:'/error')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def notFound = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; render(view:'/notFound')&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Added a new view file 'serverError.gsp' at \grails-app\views:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;head&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;Error on the server&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta name="layout" content="main" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/head&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;body&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;h1 style="margin-left:20px;color:#006dba;" &amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; An unexpected error occurred while processing your request. The admin team has been notified. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;br/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Please try again, or&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;a href="mailto:abc@xyz.com?&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subject= [${GrailsUtil.environment}] Application Error occured : '${exception?.message?.encodeAsHTML()}'"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; contact&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/a&amp;gt; the IT team&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/h1&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt; &lt;/blockquote&gt;To delegate the request to the ErrorController, add the following line to the &lt;span class="fn"&gt;UrlMappings.groovy&lt;/span&gt; file:&lt;br /&gt;&lt;blockquote&gt;"500"(controller: "errors", action:"serverError") &lt;span class="fn"&gt;.&lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;b&gt;Handling 404 Not Found&lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;If you want to handle the&lt;span class="fn"&gt; &lt;b&gt;404 Not Found &lt;/b&gt;situation specifically, add the following line to your UrlMappings.groovy file (which you can find at \grails-app\conf)&lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;class UrlMappings {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;static mappings = {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;"/$controller/$action?/$id?"{&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;constraints {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// apply constraints here&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;"/"(view:"/index")&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; "500"(controller: "errors", action:"serverError")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;"404"(controller:"errors", action:"notFound")&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;}&lt;/blockquote&gt;The view file notFound.gsp looks like this:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&amp;lt;html&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;head&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;title&amp;gt;Page not found&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;meta name="layout" content="main" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/head&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;body&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;h1 style="margin-left:20px;"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;p style="margin-left:20px;width:80%"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Page Requested not found, please check the URL&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/p&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/h1&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-5880643385182859812?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/5880643385182859812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/03/custom-error-page-for-your-grails.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/5880643385182859812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/5880643385182859812'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/03/custom-error-page-for-your-grails.html' title='Custom error page for your grails application'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6453739388040562398</id><published>2011-03-03T02:32:00.000-08:00</published><updated>2011-03-03T05:41:48.608-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JMS'/><category scheme='http://www.blogger.com/atom/ns#' term='Spring'/><title type='text'>Get started with Spring JMS using ActiveMQ</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Messaging is a mechanism to create and exchange data/information/objects within/between applications. An application that generates some useful data can send it's messages (objects) to an &lt;i&gt;agent &lt;/i&gt;and these messages (objects) can then be read from another application. JMS is a Java API that enables applications to be able to communicate with each other using the mechanism described above. &lt;br /&gt;&lt;br /&gt;You can read more about JMS &lt;a href="http://download.oracle.com/javaee/1.3/jms/tutorial/index.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;In this article, we will see how you can quickly start using JMS using the Spring JMS framework. Spring provides &lt;i&gt;template &lt;/i&gt;based APIs which greatly simplify the use of JMS. A lot of work is done behind the scenes which saves the developer time to concentrate on the business requirement.&lt;br /&gt;&lt;br /&gt;We will take a look at the important Spring JMS classes and apis as we go through an example. So let's get started! &lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 1: Download and install the JMS provided (ActiveMQ)&lt;/b&gt;&lt;br /&gt;The first thing you would need is a JMS provider (which is an implementation of the JMS interface for a MOM - Message Oriented Middleware). There are a lot of providers available, such as Apache's ActiveMQ, IBM's WebSphere M, JBoss messaging, RabbitMQ, Oracle AQ and many others.&lt;br /&gt;&lt;br /&gt;For the purpose of this tutorial we will be using ActiveMQ. You can &lt;a href="http://activemq.apache.org/download.html"&gt;download the zip file&lt;/a&gt; and install it using the instructions documented &lt;a href="http://activemq.apache.org/getting-started.html"&gt;here&lt;/a&gt;. Start the provider (open a command prompt to the bin folder of the installation and type 'activemq').&lt;br /&gt;&lt;br /&gt;To test the setup, browse to : &lt;a href="http://localhost:8161/admin/"&gt;http://localhost:8161/admin/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To be able to send and receive messages asynchronously, we would first need a queue, which is a destination for the messages. Let's create a fresh queue for our sample application.&lt;br /&gt;Go to the 'Queues' menu on the top of the admin page, and create a new queue called '&lt;b&gt;orderQueue&lt;/b&gt;'.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh4.googleusercontent.com/-mTESZakSC-c/TW8gjI18bDI/AAAAAAAAAnA/JKPfLfmdetA/s1600/activemq.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="194" src="https://lh4.googleusercontent.com/-mTESZakSC-c/TW8gjI18bDI/AAAAAAAAAnA/JKPfLfmdetA/s400/activemq.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;b&gt;Step 2: Create a Spring project&lt;/b&gt; &lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh3.googleusercontent.com/-9zttXMoGqGo/TW8nn1zs1iI/AAAAAAAAAnE/_T_6_GFuqA0/s1600/spring_utility_project.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" src="https://lh3.googleusercontent.com/-9zttXMoGqGo/TW8nn1zs1iI/AAAAAAAAAnE/_T_6_GFuqA0/s1600/spring_utility_project.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;You can create a spring project manually or use the STS (SpringSource Tool Suite) like i am using. This IDE helps you get started with Spring projects within no time.&lt;br /&gt;Create a 'Spring Utility Project' using 'File -&amp;gt; New -&amp;gt; Spring Template Project -&amp;gt; Simple Spring Utility Project'. &lt;br /&gt;&lt;br /&gt;Type the project name , say 'OrderApp' and choose a base package say - com.test.orderapp&lt;br /&gt;&lt;br /&gt;This project type uses maven for dependency management. We would need to add a few dependencies for using spring-jms and activemq.&lt;br /&gt;&lt;br /&gt;Open the 'pom.xml' at the root of the project and add the following dependencies:&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-jms&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${spring.framework.version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.apache.activemq&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;activemq-all&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;5.4.2&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;/blockquote&gt;Note: Update the version for activemq as per the installation you have used in Step 1.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 3: Configure beans in application context&lt;/b&gt;&lt;br /&gt;Edit the Spring context file app-context.xml at \src\main\resources\META-INF\spring and add the following to it:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:context="http://www.springframework.org/schema/context"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:jms="http://www.springframework.org/schema/jms"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="http://www.springframework.org/schema/beans &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/beans/spring-beans.xsd&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/context &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/context/spring-context.xsd&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/jms &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/jms/spring-jms.xsd&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://activemq.apache.org/schema/core &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://activemq.apache.org/schema/core/activemq-core.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;context:component-scan base-package="com.test.orderapp" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;context:annotation-config/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id="&lt;b&gt;connectionFactory&lt;/b&gt;" class="org.apache.activemq.ActiveMQConnectionFactory"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="brokerURL"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;value&amp;gt;&lt;b&gt;tcp://localhost:61616&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt; &lt;/b&gt;&amp;lt;bean id="&lt;b&gt;destination&lt;/b&gt;" class="org.apache.activemq.command.ActiveMQQueue"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;constructor-arg value="orderQueue"/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id=&lt;b&gt;"jmsTemplate"&lt;/b&gt; class="org.springframework.jms.core.JmsTemplate"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="connectionFactory" ref="connectionFactory" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="defaultDestination" ref="destination" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/blockquote&gt;Let's take a closer look at the beans we've defined in the spring xml.&lt;br /&gt;&lt;div style="color: #134f5c;"&gt;&lt;b&gt;1. connectionFactory:&lt;/b&gt;&lt;/div&gt;A connectionFactory is required to create a connection to the jms provider i.e. activemq. The javax.jms.ConnectionFactory creates a javax.jms.Connection object, which then creates a javax.jmx.Session object which is used for the message exchange between the application and the jms provider. You need to specify the brocker URL , i.e. the URL at which the jms provider is listening.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #134f5c;"&gt;&lt;b&gt;2. destination:&lt;/b&gt;&lt;/div&gt;The destination specifies the queue name to which the messages will be sent. Messages will be stored in and consumed from the destination specified. Destinations can, in general, be of two types: Queue and Topic. Queues represent a point-to-point messaging mechanism whereas Topics represent a publisher-subscriber sort of messaging where a single message is sent to multiple consumers. (we will be working with only queues in this tutorial)&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #134f5c;"&gt;&lt;b&gt;3. jmsTemplate:&lt;/b&gt;&lt;/div&gt;This is the class that does most of the jms related work, it creates connection factories, creates Sessions, takes care of the transactions, provides apis for creating and sending messages of different types. The jmsTemplate bean here takes a reference to the connectionFactory and a defaultDestination to which it would direct the messages. If you don't specify any defaultDestination, you can pass the destination in the 'send' methods that jmsTemplate provides. The important methods provided by jmsTemplate are : send, receive, convertAndSend and receiveAndConvert&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 4: Create the domain class&lt;/b&gt;&lt;br /&gt;We will be creating a simple domain class called 'Order' in a package com.test.orderapp.domain as follows:&lt;br /&gt;&amp;nbsp;package com.test.orderapp.domain;&lt;br /&gt;&lt;blockquote&gt;public class Order {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;private int orderId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;private int customerId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;private double price;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;private String orderCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public Order(int orderId, int customerId, double price, String orderCode){&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.orderId = orderId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.customerId = customerId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.price = price;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.orderCode = orderCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public int getOrderId() {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return orderId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void setOrderId(int orderId) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.orderId = orderId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public int getCustomerId() {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return customerId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void setCustomerId(int customerId) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.customerId = customerId;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public double getPrice() {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return price;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void setPrice(double price) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.price = price;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public String getOrderCode() {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return orderCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void setOrderCode(String orderCode) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;this.orderCode = orderCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;}&lt;/blockquote&gt;&lt;b&gt;Step 5: &lt;/b&gt;&lt;b&gt;Write the code to &lt;i&gt;send &lt;/i&gt;the objects&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;We will create a simple service class that creates new orders given some information and then sends them.&lt;br /&gt;&lt;blockquote&gt;package com.test.orderapp.producer;&lt;br /&gt;&lt;br /&gt;import org.springframework.beans.factory.annotation.Autowired;&lt;br /&gt;import org.springframework.stereotype.Service;&lt;br /&gt;import com.test.orderapp.domain.Order;&lt;br /&gt;&lt;br /&gt;@Service("orderService")&lt;br /&gt;public class OrderService {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static int orderSequence = 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Autowired&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private OrderSender orderSender;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setOrderSender(OrderSender orderSender){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.orderSender = orderSender;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void sendOrder(int customerId, double price)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Order order = new Order(orderSequence, 2, price, "ordercd"+ orderSequence++);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;orderSender.sendOrder(order);&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Nothing special about the service class. We are wiring the&amp;nbsp; OrderSender class into the service class.&lt;br /&gt;The orderSender class is jms aware and has all the logic to convert and send the domain object as a jms message.&lt;br /&gt;&lt;br /&gt;OrderSender.java :-&lt;br /&gt;&lt;blockquote&gt;package com.test.orderapp.producer;&lt;br /&gt;&lt;br /&gt;import javax.jms.JMSException;&lt;br /&gt;import javax.jms.MapMessage;&lt;br /&gt;import javax.jms.Message;&lt;br /&gt;import javax.jms.Session;&lt;br /&gt;import org.springframework.beans.factory.annotation.Autowired;&lt;br /&gt;import org.springframework.jms.core.JmsTemplate;&lt;br /&gt;import org.springframework.jms.core.MessageCreator;&lt;br /&gt;import com.test.orderapp.domain.Order;&lt;br /&gt;&lt;br /&gt;public class OrderSender {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Autowired&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private JmsTemplate jmsTemplate;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void sendOrder(final Order order){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; jmsTemplate.send(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; new MessageCreator() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Message createMessage(Session session) throws JMSException {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MapMessage mapMessage = session.createMapMessage();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMessage.setInt("orderId", order.getOrderId());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMessage.setInt("customerId", order.getCustomerId());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMessage.setDouble("price", order.getPrice());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mapMessage.setString("orderCode", order.getOrderCode());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return mapMessage;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Order sent - id: "+ order.getOrderId());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;The OrderSender uses the JmsTemplate's &lt;i&gt;send&lt;/i&gt; method to send the object. Notice the use of the &lt;b&gt;MessageCreator&lt;/b&gt; class. You can send different types of messages via JMS eg: TextMessage, ObjectMessage, MapMessage (all part of the java.jms package) The most preferred way to send complex domain objects is to use MapMessage to which you can add all the fields of your domain object as key-value pairs.&amp;nbsp; The &lt;i&gt;MessageCreator &lt;/i&gt;class is a callback interface for creating a message using the Session object that is passed as a parameter to the &lt;i&gt;&lt;u&gt;createMessage&lt;/u&gt;&lt;/i&gt; method.&lt;br /&gt;Also note, that we are not specifying the &lt;i&gt;destination &lt;/i&gt;in the send method since we have defined a defaultDestination for the jmsTemplate in the spring context. In case you are not going the default way, you can pass a string representing the destination as the first parameter to the &lt;i&gt;jmsTemplate.send()&lt;/i&gt; method&lt;br /&gt;for eg:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jmsTemplate.send( "orderQueue",&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new &lt;b&gt;MessageCreator&lt;/b&gt;() ..... )&lt;br /&gt;&lt;br /&gt;There are other ways to send messages such as &lt;i&gt;MessageConverter&lt;/i&gt;&lt;b&gt; &lt;/b&gt;which can also be used to convert between Java objects and JMS messages. The convertAndSend and receiveAndConvert methods of the jmsTemplate class delegate the conversion to the MessageConverter interface. We are not using MessageConverter in this tutorial. Some other time maybe...&lt;br /&gt;&lt;br /&gt;Let's update the app-context.xml file to declare two additional bean definitions:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;bean id="orderService" class="com.test.orderapp.source.OrderService" /&amp;gt;&lt;br /&gt;&amp;lt;bean id="orderSender" class="com.test.orderapp.source.OrderSender" /&lt;/blockquote&gt;&lt;b&gt;Step 6: Test the message producer&lt;/b&gt;&lt;br /&gt;Let's check if we are actually able to send any messages to the ActiveMQ 'orderQueue'. We will create a simple TestJms class as follows:&lt;br /&gt;&lt;blockquote&gt;package com.test.orderapp.runner;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.support.ClassPathXmlApplicationContext;&lt;br /&gt;import com.test.orderapp.producer.OrderService;&lt;br /&gt;&lt;br /&gt;public class TestJms {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OrderService orderService = (OrderService) ctx.getBean("orderService");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i =1; i&amp;lt;=5; i++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;orderService.sendOrder&lt;/b&gt;(1+i, 10.0D+i);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="https://lh6.googleusercontent.com/-Ts2V9qj4VCI/TW9ors9sqsI/AAAAAAAAAnM/4JzBSD4ugbI/s1600/activemqqueue.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://lh6.googleusercontent.com/-Ts2V9qj4VCI/TW9ors9sqsI/AAAAAAAAAnM/4JzBSD4ugbI/s1600/activemqqueue.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;At this point, if you run the TestJms class, 5 messages would be sent to the 'orderQueue' queue. If you browse to the &lt;a href="http://localhost:8161/admin/queues.jsp"&gt;http://localhost:8161/admin/queues.jsp&lt;/a&gt; page you should see 5 messages enqueued on orderQueue.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 7: Consuming JMS Messages&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Listening to the jms messages is in fact easier than sending them. All we need to do is define a jms Listener Container in the spring context to which we pass the &lt;i&gt;connectionFactory &lt;/i&gt;bean which specifies the jms provider, a reference to the &lt;i&gt;destination &lt;/i&gt;which specifies the queue name and we also specify here, which method from which bean needs to be called when a message is recieved.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Add the following to the app-context.xml file:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;jms:listener-container&amp;nbsp; &lt;b&gt;connection-factory&lt;/b&gt;="connectionFactory"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;jms:listener &lt;b&gt;destination&lt;/b&gt;="orderQueue" ref="&lt;b&gt;orderListener&lt;/b&gt;" method="&lt;b&gt;orderReceived&lt;/b&gt;" /&amp;gt;&lt;br /&gt;&amp;lt;/jms:listener-container&amp;gt;&lt;/blockquote&gt;Create a class OrderListener as follows:&lt;br /&gt;&lt;blockquote&gt;package com.test.orderapp.listener;&lt;br /&gt;import java.util.Map;&lt;br /&gt;import org.springframework.stereotype.Component;&lt;br /&gt;import com.test.orderapp.domain.Order;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;@Component&lt;/b&gt;&lt;br /&gt;public class OrderListener {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void &lt;b&gt;orderReceived&lt;/b&gt;(Map&amp;lt;String, Object&amp;gt; message) throws Exception {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int orderId = (Integer) message.get("orderId");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int customerId = (Integer) message.get("customerId");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; double price = (Double) message.get("price");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; String orderCode = (String) message.get("orderCode");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Order customer = new Order(orderId, customerId, price, orderCode);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Order received: "+ orderId + ", customerId: "+ customerId + ", price: "+ price);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Simple.&lt;br /&gt;&lt;br /&gt;Let's run the TestJms class once again, this time we should see some prints from the OrderListener class too.&lt;br /&gt;Pretty cool, isn't it!&lt;br /&gt;&lt;br /&gt;Well, that brings us to the end of the introductory article to Spring JMS, i hope you can now appreciate how much easier it is to write JMS based applications using Spring.&lt;br /&gt;&lt;br /&gt;Hope you find this article useful.&lt;br /&gt;Happy coding!&lt;br /&gt;&lt;br /&gt;Download the source code of the sample application : &lt;a href="https://sites.google.com/site/javaunplugged/spring/OrderApp.rar?attredirects=0&amp;amp;d=1"&gt;OrderApp.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6453739388040562398?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6453739388040562398/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/03/get-started-with-spring-jms-using.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6453739388040562398'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6453739388040562398'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/03/get-started-with-spring-jms-using.html' title='Get started with Spring JMS using ActiveMQ'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='https://lh4.googleusercontent.com/-mTESZakSC-c/TW8gjI18bDI/AAAAAAAAAnA/JKPfLfmdetA/s72-c/activemq.jpg' height='72' width='72'/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-39582842135905570</id><published>2011-02-25T03:08:00.000-08:00</published><updated>2011-02-25T05:35:47.890-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Hudson'/><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Integrating Grails and Hudson</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;The best way to ensure consistent code quality and system stability is to integrate your application with a &lt;i&gt;Continuous Integration system. &lt;/i&gt;Hudson is one such CI system.&lt;br /&gt;&lt;br /&gt;In this tutorial, i will give a small demo on how to integrate Grails with Hudson. I will assume you are familiar with the grails technology and have some basic idea of what continuous integration systems are.&lt;br /&gt;Note: This is not a tutorial on Hudson!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Step 1: Download and deploy hudson.war&lt;/b&gt;&lt;br /&gt;Download hudson from the &lt;a href="http://hudson-ci.org/"&gt;hudson home page&lt;/a&gt;. Deploy the war file in your favorite web-server. I am using tomcat for the purpose of this demo. Place hudson.war in the %tomcat_installation%/webapps folder.&lt;br /&gt;&lt;br /&gt;Tip: You may want to set an environment variable HUDSON_HOME pointing to a location of your choice where all the hudson jobs, plugins, reports, and all configurations would be stored. By default, hudson places everything in the user's home directory.&lt;br /&gt;&lt;br /&gt;Start the web-server and browse to the url : &lt;i&gt;http://localhost:8080/hudson&lt;/i&gt; (assuming your server is running on 8080) &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 2: Install plugins&lt;/b&gt;&lt;br /&gt;On the panel on the left side, navigate to 'Hudson -&amp;gt; Manage Hudson -&amp;gt; Manage Plugins'&lt;b&gt; &lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;/b&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;In the 'Available' tab, look for the following&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-bkfQ_LSxBTU/TWc-p13RBHI/AAAAAAAAAmQ/l6eJq0cCBtY/s1600/hudson_manage.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-bkfQ_LSxBTU/TWc-p13RBHI/AAAAAAAAAmQ/l6eJq0cCBtY/s1600/hudson_manage.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;plugins, tick the checkboxes and install them. &lt;br /&gt;&lt;br /&gt;1. Grails plugin&lt;br /&gt;2. Groovy plugin&lt;br /&gt;3. Email-ext plugin&lt;br /&gt;4. Violations plugin (for static code analysis)&lt;br /&gt;5. Cobertura plugin (for code coverage)&lt;br /&gt;&lt;br /&gt;Restart the web-server after installations.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We will discuss why we need these plugins shortly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 3: Configure Hudson&lt;/b&gt;&lt;br /&gt;Navigate to 'Hudson -&amp;gt; Manage Hudson -&amp;gt; Configure System'.&lt;br /&gt;Set up 'JDK' , 'Grails Builder'. i.e. set up the JAVA_HOME and GRAILS_HOME paths in Hudson to your local installations.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 4:&amp;nbsp; Create and configure a new job for your Grails application&lt;/b&gt;&lt;br /&gt;Navigate to 'Hudson -&amp;gt; New Job' and choose 'Build a free-style software project'.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-Pta02hKaOr0/TWdBcJeB8PI/AAAAAAAAAmY/C_cZ2ISUZLE/s1600/hudson_createjob.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-Pta02hKaOr0/TWdBcJeB8PI/AAAAAAAAAmY/C_cZ2ISUZLE/s1600/hudson_createjob.jpg" /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;To configure the job Open the job, and click on 'Configure' on the left-hand-side panel.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;1. Source code Management: Configure the source code. If you are using SVN or CVS, add the urls, credentials and ensure that the src code is available. &lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;2. Build Triggers: You need to decide when you would want to run the builds against the src code. You have options such as 'Polling the SCM' which would keep polling the SCM and fire a build everytime there is a checkin ( you can also specify a cron expression to configure the polling frequency) or you could choose to build the project at regular periods. etc.&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-ja1rvI24uUE/TWda9thB4OI/AAAAAAAAAmk/Q5SYrd_pkGs/s1600/hudson_build.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-ja1rvI24uUE/TWda9thB4OI/AAAAAAAAAmk/Q5SYrd_pkGs/s1600/hudson_build.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;3. Build: Add a build step (choose 'Build with Grails').&lt;br /&gt;Choose the grails installation you had setup in Step 3.&lt;br /&gt;Next choose a target - such as 'compile'.&lt;br /&gt;You can add multiple targets seperated by commans.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can choose the port number, the work directory etc. &lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/-yi3WWu2vi_0/TWddWw1R07I/AAAAAAAAAms/OPQxePgbYy0/s1600/hudson_run.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="320" src="http://4.bp.blogspot.com/-yi3WWu2vi_0/TWddWw1R07I/AAAAAAAAAms/OPQxePgbYy0/s320/hudson_run.jpg" width="218" /&gt;&lt;/a&gt;Let's say you want to run both: 'grails clean' and 'grails compile' commands:&lt;br /&gt;So your target should now look like: 'clean compile'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You can also setup the 'Post-build Actions' to generate&lt;br /&gt;JUnit test reports, and send email notifications once the &lt;br /&gt;build is done. Save the configuration. You're ready to go!&lt;br /&gt;&lt;br /&gt;Click 'Hudson -&amp;gt; 'Project name' -&amp;gt; Build Now. &lt;br /&gt;&lt;br /&gt;You can check the output of the build process by clicking on the link shown in the 'Build History' panel on the left. Then click 'Console Output'.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 5: Static code analysis&lt;/b&gt;&lt;br /&gt;Grails has a plugin by CodeNarc for static code analysis. You will first need to install this plugin in your grails application using the command:&lt;br /&gt;&lt;blockquote&gt;grails install-plugin CodeNarc&lt;/blockquote&gt;And add the following to the Config.groovy file, so that we can use the 'Violations' plugin in hudson to analyze the xml reports generated by this plugin.&lt;/div&gt;&lt;div class="" style="clear: both; text-align: left;"&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;blockquote&gt;codenarc {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reportName = 'CodeNarc.xml'&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reportType = 'xml'&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/blockquote&gt;&lt;a href="http://1.bp.blogspot.com/-Rbt5NZYiiPg/TWd86oLdW1I/AAAAAAAAAmw/PKQx7-LQGp0/s1600/hudson_codenarc.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="119" src="http://1.bp.blogspot.com/-Rbt5NZYiiPg/TWd86oLdW1I/AAAAAAAAAmw/PKQx7-LQGp0/s320/hudson_codenarc.jpg" width="320" /&gt;&lt;/a&gt;We would also need to add an additional build step in our job. Add the target 'codenarc' and save the configuration.&lt;br /&gt;&lt;br /&gt;Also, in the the 'Post-build Actions' section, check the box agains 'Report Violations' and specify the name of the xml file as follows:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-3UD8fleoPwI/TWeDMnVDh4I/AAAAAAAAAm4/RI8q_49ZrMo/s1600/hudson_codenarc2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-3UD8fleoPwI/TWeDMnVDh4I/AAAAAAAAAm4/RI8q_49ZrMo/s1600/hudson_codenarc2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-9BRUJ3j551s/TWeACjWoVAI/AAAAAAAAAm0/BLrGcop0E3M/s1600/hudson_codenarc2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b&gt;Step 6: Publishing test results and code coverage results&lt;/b&gt;&lt;br /&gt;For running tests, we would need to add a new target here 'test-app'. If you want to use code coverage as well. You first need to install the code-coverage plugin which can be done as follows:&lt;br /&gt;&lt;blockquote&gt;grails install-plugin code-coverage &lt;/blockquote&gt;Then set up a new build step and add "test-app -coverage -xml" -with the quotes- in the target field. ( if you don't want to run code-coverage just use 'test-app' as the target )&lt;br /&gt;&lt;br /&gt;Also, set up the code coverage report publication in the 'Post-build Actions' section. The code-coverage xmls can be found in the 'target/test-reports/cobertura/*.xml' folder.&lt;br /&gt;&lt;br /&gt;That's all you need to do, and a build will be fired everytime there's a checkin, tests would be run, test reports would be published, code-coverage reports would be generated and code analysis reports would also be generated. Well, that's quite a bit for half-an-hour's effort :) Neat!&lt;br /&gt;&lt;br /&gt;There's a lot of plugins available in Hudson that you can explore to suite your needs.&lt;br /&gt;&lt;br /&gt;Hope you find this article useful.&lt;br /&gt;&lt;br /&gt;Happy coding!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-39582842135905570?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/39582842135905570/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/integrating-grails-and-hudson.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/39582842135905570'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/39582842135905570'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/integrating-grails-and-hudson.html' title='Integrating Grails and Hudson'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-bkfQ_LSxBTU/TWc-p13RBHI/AAAAAAAAAmQ/l6eJq0cCBtY/s72-c/hudson_manage.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6378065967226986178</id><published>2011-02-24T02:38:00.000-08:00</published><updated>2011-02-24T03:13:41.955-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Exploring BootStrap.groovy</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;You may come across a scenario where you want to carry out a task every time your application starts or stops.&lt;br /&gt;&lt;br /&gt;You just need to peep inside your grails-app/conf folder to find a groovy file called BootStrap.groovy.&lt;br /&gt;BootStrap.groovy is a simple plain groovy script with two important closures : 'init' and 'destroy'. These closures are called upon when the application starts up and shuts down respectively.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;But. BUT.. The 'destroy' closure is not guaranteed to be called at all times. The destroy closure will be called if your application exits gracefully. But if the web-server crashes or the machine reboots chances are, the 'destroy' closure would not be called. So please do not rely heavily on this one.&lt;br /&gt;&lt;br /&gt;Let's consider a scenario where you want to send out an email to your team every time your application starts up or shuts down.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 1: Install the mail plugin&lt;/b&gt;&lt;br /&gt;Let's use the &lt;a href="http://www.grails.org/plugin/mail"&gt;grails mail plugin&lt;/a&gt; to send emails.&lt;b&gt; &lt;/b&gt;To install the plugin use the command:&lt;br /&gt;&lt;blockquote&gt;grails install-plugin mail&lt;/blockquote&gt;&lt;br /&gt;&lt;b&gt;Step 2: Initialize mail properties in Config.groovy&lt;/b&gt;&lt;br /&gt;Add some parameters to your Config.groovy file:&lt;br /&gt;&lt;blockquote&gt;grails {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mail {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host = "smtpserver"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; port = 123&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; username = 'username'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; password = 'password'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; props = ["mail.smtp.auth":"true",&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "mail.smtp.socketFactory.port":"465",&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "mail.smtp.socketFactory.fallback":"false"]&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;grails.mail.default.from = "abc@xyz.com"&lt;br /&gt;&lt;b&gt;grails.mail.disabled=true&lt;/b&gt;&lt;br /&gt;app.startup.mail.to = "abc@xyz.com"&lt;br /&gt;environments {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; production {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails.serverURL = "http:///localhost:8080/BootStrapTest"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;grails.mail.disabled=false&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; development {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails.serverURL = "http://localhost:8080/BootStrapTest"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; test {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails.serverURL = "http://localhost:8080/BootStrapTest"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;} &lt;/blockquote&gt;[ We want to&amp;nbsp; send emails only if the application starts-up or shuts-down in production mode (ofcourse) ]&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 3: Update BootStrap.groovy&lt;/b&gt;&lt;br /&gt;Edit the BootStrap.groovy file to send emails as follows:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;import org.codehaus.groovy.grails.commons.ConfigurationHolder;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.text.SimpleDateFormat;&lt;br /&gt;&lt;br /&gt;class BootStrap {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def mailService&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def init = { servletContext -&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; println "Application starting up... "&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def toMail = ConfigurationHolder.config.app.startup.mail.to&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def mailSubject = "Application starting up on ${InetAddress.localHost.hostName}"; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; String mailBody = '''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application started at ''' + new SimpleDateFormat("dd-MMM-yyy hh:mm:ss").format(new Date()) + '''&amp;lt;br/&amp;gt;''' +&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;lt;a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '&amp;gt; ''' +&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConfigurationHolder.config.grails.serverURL + '''&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Note : This is an automatically generated email.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sendNotification(toMail, mailSubject, mailBody )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def destroy = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; println "Application shutting down... "&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def toMail = ConfigurationHolder.config.app.startup.mail.to&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def mailSubject = "Application starting up on ${InetAddress.localHost.hostName}"; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; String mailBody = '''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application shutting down at ''' + new SimpleDateFormat("dd-MMM-yyy hh:mm:ss").format(new Date()) + '''&amp;lt;br/&amp;gt;''' +&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;lt;a href=' ''' + ConfigurationHolder.config.grails.serverURL + ''' '&amp;gt; ''' +&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConfigurationHolder.config.grails.serverURL + '''&amp;lt;/a&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Note : This is an automatically generated email.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sendNotification(toMail, mailSubject, mailBody )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def sendNotification(String toMail, String mailSubject, String mailBody)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; println "### sending email ###"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; mailService.sendMail {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; to toMail&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; subject mailSubject&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; body mailBody&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Well, that's about it!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 4: Create a war and deploy it&lt;/b&gt; &lt;br /&gt;Create a war file using the command:&lt;br /&gt;&lt;blockquote&gt;grails war&lt;/blockquote&gt;Deploy the war in your favourite web-server and test the print statements and emails.&lt;br /&gt;&lt;br /&gt;You could do any startup/shutdown tasks, cleanup etc you want to do using these two closures.&lt;br /&gt;Hope you find this article useful.&lt;br /&gt;&lt;br /&gt;Happy coding!&lt;br /&gt;&lt;br /&gt;Download the sample code : &lt;a href="https://sites.google.com/site/javaunplugged/grails/bootstrap.rar?attredirects=0&amp;amp;d=1"&gt;bootstrap.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6378065967226986178?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6378065967226986178/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/bootstrapping-your-grails-application.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6378065967226986178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6378065967226986178'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/bootstrapping-your-grails-application.html' title='Exploring BootStrap.groovy'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6504093524654348302</id><published>2011-02-21T00:39:00.000-08:00</published><updated>2011-02-21T00:39:28.392-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails Troubleshoot'/><title type='text'>Problem setting the grails proxy ?</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;b&gt;&lt;u&gt;Issue&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;If you are using grails on a windows box, you may face an issue while setting the grails proxy using the 'grails add-proxy' command. This is because of the way windows treats any paramters that you pass using the -- option and sets the host name and the port to boolean values.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Resolution&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;You just need to pass your parameters in double quotes.&lt;br /&gt;Example: &lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;grails add-proxy proxy1 "--host=someproxyserver" "--port= 8080"&lt;br /&gt;grails set-proxy proxy1&lt;/blockquote&gt;Pretty easy!&lt;br /&gt;&lt;br /&gt;(Your proxy settings can be found in the %user_home%\.grails\ProxySettings file.&lt;br /&gt;&amp;nbsp;You can try changing this file with correct values manually also)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6504093524654348302?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6504093524654348302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/problem-setting-grails-proxy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6504093524654348302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6504093524654348302'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/problem-setting-grails-proxy.html' title='Problem setting the grails proxy ?'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-385910921870759569</id><published>2011-02-10T20:56:00.000-08:00</published><updated>2011-02-10T20:57:01.630-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails Troubleshoot'/><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>JDBCException due to stale connections in Grails</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;u&gt;&lt;b&gt;Issue&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;If your grails application remains idle for too long or the database connections remain idle for too long or the database is restarted you may see the following exceptions:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;ERROR org.hibernate.util.JDBCExceptionReporter  - No more data to read from socket &lt;br /&gt;ERROR org.hibernate.util.JDBCExceptionReporter  - OALL8 is in an inconsistent state &lt;br /&gt;ERROR org.hibernate.util.JDBCExceptionReporter  - Io exception: Broken pipe &lt;br /&gt;ERROR org.hibernate.util.JDBCExceptionReporter  - Already closed. &lt;/blockquote&gt;These issues are intermittent, that is, they &lt;i&gt;may&lt;/i&gt; disappear when you try hitting again. &lt;br /&gt;This issue occurs because the application tries to use a database connection that has become stale and should have been evicted.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Resolution&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;This problem can be fixed by overriding the default datasource with a DBCP BasicDataSource and setting some DBCP properties.&lt;br /&gt;&lt;br /&gt;For my applicatin, i have encrypted my database password used a codec.&lt;br /&gt;This is how my DataSource.groovy looks:&lt;br /&gt;dataSource {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; loggingSql = false&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; username = "USERNAME"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; password = "qDKJkZFGtPK2lj54M11OMQ00"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dbCreate = "update"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; driverClassName = "oracle.jdbc.OracleDriver"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; passwordEncryptionCodec = "com.myapp.encryption.DESCustomCodec"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; url = "jdbc:oracle:thin:@ldap...."&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;Grails uses the class specified in the property 'passwordEncryptionCodec' to decode the database password.&lt;br /&gt;&lt;br /&gt;To fix the jdbc exceptions we are facing, we need to override the datasource bean in \grails-app\conf\springspring\&lt;b&gt;resources.groovy &lt;/b&gt;as follows:&lt;br /&gt;&lt;blockquote&gt;import org.apache.commons.dbcp.BasicDataSource&lt;br /&gt;import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH&lt;br /&gt;import org.codehaus.groovy.grails.commons.ApplicationHolder as AH&lt;br /&gt;&lt;br /&gt;beans = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ssoAuthenticationProvider(SSOAuthenticationProvider) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; userDetailsService = ref("userDetailsService")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataSource(BasicDataSource) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; def application = AH.application&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(CH.config.dataSource.passwordEncryptionCodec){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; def codec = application.classLoader.loadClass(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CH.config.dataSource.passwordEncryptionCodec)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; password = codec.decode(CH.config.dataSource.password)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; password = CH.config.dataSource.password&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; username = CH.config.dataSource.username&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; driverClassName = CH.config.dataSource.driverClassName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; url = CH.config.dataSource.url&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; minEvictableIdleTimeMillis=1800000&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; timeBetweenEvictionRunsMillis=1800000&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; numTestsPerEvictionRun=3&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; testOnBorrow=true&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; testWhileIdle=true&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; testOnReturn=true&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(driverClassName.contains('oracle'))&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; validationQuery="SELECT 1 FROM DUAL"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/blockquote&gt;If a passwordEcryptionCodec is specified, the corresponding class is loaded and the password is decoded and then assigned.&amp;nbsp; We then populate the username, driverClassName, and jdbc url of the new bean.&lt;br /&gt;We also set the dbcp properties&lt;br /&gt;&lt;b&gt;minEvictableIdleTimeMillis&lt;/b&gt;: The minimum amount of time an object may sit idle in the pool before it  is eligible for eviction by the idle object evictor&lt;br /&gt;&lt;b&gt;numTestsPerEvictionRun&lt;/b&gt;:&amp;nbsp; The number of objects to examine during each run of the idle object  evictor thread &lt;br /&gt;&lt;b&gt;testOnBorrow&lt;/b&gt;: The indication of whether objects will be validated before being  borrowed from the pool&lt;br /&gt;&lt;b&gt;testWhileIdle&lt;/b&gt;: The indication of whether objects will be validated by the idle object  evictor&lt;br /&gt;&lt;b&gt;testOnReturn&lt;/b&gt;:The indication of whether objects will be validated before being  returned to the pool.&lt;br /&gt;&lt;b&gt;validationQuery: &lt;/b&gt;The SQL query that will be used to validate connections from this pool  before returning them to the caller&lt;br /&gt;&lt;br /&gt;Find more DBCP options &lt;a href="http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html"&gt;here&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;Note: if you are using different databases for uat, dev and production, you may have to accordingly set the validationQuery per environment. ( For eg, the above query may not work for a dev environment which uses hsql database) Or you could choose to override the bean only in a production database. Whatever suits you best.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-385910921870759569?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/385910921870759569/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/jdbcexception-due-to-stale-connections.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/385910921870759569'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/385910921870759569'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/jdbcexception-due-to-stale-connections.html' title='JDBCException due to stale connections in Grails'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-8082070356923014482</id><published>2011-02-10T19:47:00.000-08:00</published><updated>2011-03-03T19:11:27.633-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails Troubleshoot'/><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>java.lang.NoClassDefFoundError when sending async emails in Grails</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;u&gt;&lt;b&gt;Issue&lt;/b&gt;&lt;/u&gt; &lt;br /&gt;Exception while sending emails in production environment.&lt;br /&gt;&lt;br /&gt;If you are trying to send emails from your application asynchronously, using the Mail plugin, in the production environment you may see an exception like:&lt;br /&gt;&lt;blockquote&gt;java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest&amp;nbsp;&lt;/blockquote&gt;This is because of a bug in the Mail plugin. This plugin uses the MockHttpServletRequest class from the spring framework test jar which is not bundled when you run the 'grails prod war' command.&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Resolution&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;Copy the org.springframework.test-3.0.3.RELEASE.jar from %GRAILS_HOME%\lib into your application's lib folder. By doing this, the plugin will find the MockHttpServletRequest class that it relies on at runtime in production.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-8082070356923014482?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/8082070356923014482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/javalangnoclassdeffounderror-when.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/8082070356923014482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/8082070356923014482'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/javalangnoclassdeffounderror-when.html' title='java.lang.NoClassDefFoundError when sending async emails in Grails'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-2205255974892595358</id><published>2011-02-09T23:37:00.000-08:00</published><updated>2011-02-10T01:31:12.157-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REST'/><category scheme='http://www.blogger.com/atom/ns#' term='Spring'/><title type='text'>RESTful webservices with Spring</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;REST (Representational State Transfer) is a stateless architectural style wherein resources are identified and manipulated. by their URIs and transferred over HTTP. A REST web service sends any parameters, data needed by the server within the HTTP headers and body.&lt;br /&gt;In this article, we will build REST webservices with Spring and a client using the Spring RestTemplate to consume the webservice. I will not be going into the details of these technologies, the aim is to rapidly come up with a basic CRUD web-service.&lt;br /&gt;&lt;br /&gt;I will be assuming, that you are familiar with the basics of Spring, DI and IoC, REST, and the basic web-application structure in java. &lt;br /&gt;&lt;br /&gt;Without much ado, let's begin!&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;If you've already built a Spring MVC application, writing a REST web-service should come easily. You can find a tutorial on how to build a simple Spring MVC application &lt;a href="http://java-diaries.blogspot.com/2011/02/get-started-with-spring-mvc.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I will be using the STS IDE (SpringSource Tool Suite). You can use any eclipse installation, or infact just create a similar package structure yourself. The source of the sample application demonstrated in this article is available for download at the bottom of this page.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 1: Create a Spring MVC template project&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Select&amp;nbsp;&lt;b&gt; &lt;/b&gt;File -&amp;gt; New -&amp;gt; Spring Template Project -&amp;gt; Spring MVC Project.&lt;br /&gt;Choose an appropriate project name, and a base package name eg. CMSService, com.test.cmsservice&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-_4HAptLQGGs/TVNu-ax8vrI/AAAAAAAAAlQ/Uq4nCx-gX8E/s1600/spring_mvc_template.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-_4HAptLQGGs/TVNu-ax8vrI/AAAAAAAAAlQ/Uq4nCx-gX8E/s1600/spring_mvc_template.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The IDE will create a basic web-application skeleton for you to work with.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 2: Add maven dependencies&lt;/b&gt;&lt;br /&gt;You will find a maven pom.xml at the base of the project. We would need some additional libraries. Add the following dependencies to your pom.xml:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-beans&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-context-support&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;exclusions&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;exclusion&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;quartz&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;quartz&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/exclusion&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/exclusions&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-core&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-test&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-orm&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-oxm&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.springframework&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;spring-web&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;${org.springframework-version}&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;cglib&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;cglib-nodep&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;2.1_3&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;javax.xml.bind&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;jaxb-api&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;2.0&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;/blockquote&gt;If you are not using maven, you will have to download and add the necessary jars to your classpath manually.&amp;nbsp; &lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 3: Create a domain class&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Create a new package com.test.cmsservice.domain and a simple Customer domain class that has a couple of properties and corresponding getters and setters.&lt;br /&gt;&lt;blockquote&gt;package com.test.cmsservice.domain;&lt;br /&gt;import javax.xml.bind.annotation.XmlRootElement;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;@XmlRootElement(name="customer")&lt;/b&gt;&lt;br /&gt;public class Customer {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Long id;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String emailAddress;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String phoneNumber;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Customer(){}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Customer(Long id, String firstName, String lastName, String emailAddress, String&amp;nbsp;&amp;nbsp; phoneNumber){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.id = id;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.firstName = firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.lastName = lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.emailAddress = emailAddress;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.phoneNumber = phoneNumber;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Long getId() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return id;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setId(Long id) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.id = id;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getFirstName() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setFirstName(String firstName) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.firstName = firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getLastName() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setLastName(String lastName) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.lastName = lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getEmailAddress() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return emailAddress;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setEmailAddress(String emailAddress) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.emailAddress = emailAddress;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getPhoneNumber() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return phoneNumber;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setPhoneNumber(String phoneNumber) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.phoneNumber = phoneNumber;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Note the @XmlRootElement annotation at the class level. When a class is so annotated, its value is represented as an XML root element in an XML document. Use of these annotations enables these classes to be mapped to schema with minor changes to the code.&lt;br /&gt;&lt;br /&gt;We will also create a CustomerList class that acts as a wrapper class for sending across a list of all the customers in the system.&lt;br /&gt;&lt;blockquote&gt;package com.test.cmsservice.domain;&lt;br /&gt;&lt;br /&gt;import java.io.Serializable;&lt;br /&gt;import java.util.List;&lt;br /&gt;import javax.xml.bind.annotation.XmlElement;&lt;br /&gt;import javax.xml.bind.annotation.XmlRootElement;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;@XmlRootElement(name="customers")&lt;/b&gt;&lt;br /&gt;public class CustomerList implements Serializable{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private List&amp;lt;Customer&amp;gt; customers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public CustomerList(){}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public CustomerList(List&amp;lt;Customer&amp;gt; customers){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.customers = customers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@XmlElement(name="customer")&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public List&amp;lt;Customer&amp;gt; getCustomers() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return customers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setCustomers(List&amp;lt;Customer&amp;gt; customers) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.customers = customers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;&lt;b&gt;STEP 4: Create a service class&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In a working application, you'd store the customers in a database, but to focus on REST, i will be storing the customers in a static list in the service class (Find a tutorial on Spring Hibernate integration &lt;a href="http://java-diaries.blogspot.com/2011/02/spring-hibernate-integration.html"&gt;here&lt;/a&gt;)&lt;br /&gt;&lt;blockquote&gt;package com.test.cmsservice.service;&lt;br /&gt;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.HashMap;&lt;br /&gt;import java.util.List;&lt;br /&gt;import com.test.cmsservice.domain.Customer;&lt;br /&gt;&lt;br /&gt;public class CustomerService {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static HashMap&amp;lt;Long, Customer&amp;gt; customers = new HashMap&amp;lt;Long, Customer&amp;gt;(); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.put(1L, new Customer(1L, "Stephen", "Smith", "Stephen.Smith@xyz.abc", "34545634"));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.put(2L, new Customer(2L, "Mary", "Johnson", "Mary.Johnson@abc.xyz", "32445433"));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Customer getCustomer(Long customerId){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return customers.get(customerId);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void saveCustomer(Customer customer){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.put(customer.getId(), customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public List&amp;lt;Customer&amp;gt; getCustomers(){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return new ArrayList(customers.values());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void deleteCustomer(Long customerId){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.remove(customerId);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void updateCustomer(Customer customer){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.put(customer.getId(), customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Pretty basic.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 5: Create a controller class&lt;/b&gt;&lt;br /&gt;This is where the interesting part lies. Create a package com.test.cmsservice.web and a class CustomerController as follows:&lt;br /&gt;&lt;blockquote&gt;package com.test.cmsservice.web;&lt;br /&gt;&lt;br /&gt;/*import statements ...*/&lt;br /&gt;&lt;br /&gt;&lt;b&gt;@Controller&lt;/b&gt;&lt;br /&gt;&lt;b&gt;@RequestMapping("customer")&lt;/b&gt;&lt;br /&gt;public class CustomerController {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Autowired&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CustomerService customerService;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@RequestMapping(method=RequestMethod.GET)&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ModelAndView getAllCustomers(ModelMap model)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;Customer&amp;gt; customers = customerService.getCustomers();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CustomerList customerList = new CustomerList(customers);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return new ModelAndView("&lt;b&gt;customerView&lt;/b&gt;", "customers", customerList);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @RequestMapping(&lt;b&gt;value="/{id}"&lt;/b&gt;, method = &lt;b&gt;RequestMethod.GET&lt;/b&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ModelAndView getCustomer(&lt;b&gt;@PathVariable Long id&lt;/b&gt;){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Customer customer = customerService.getCustomer(id);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return new ModelAndView("customerView", "customer", customer);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @RequestMapping(method = &lt;b&gt;RequestMethod.POST&lt;/b&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public View addCustomer(@RequestBody Customer customer)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customerService.saveCustomer(customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return new RedirectView("/customerView/"+ customer.getId());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;&amp;nbsp;The @Controller annotation enables detection when the spring container scans the application at load time and informs the container that the class will behave as a Controller. &lt;br /&gt;&lt;br /&gt;The @RequestMapping annotation marks a function that can handle HTTP requests. A request that matches the URL pattern and HTTP method provided in  the 'value' and 'method' attributes of the @RequestMapping annotation would be delegated to the  corresponding function. In this case, we have used the @RequestMapping annotation at the class level as well. The attributes that are common to all the methods can be specified at the class level. &lt;br /&gt;&lt;br /&gt;The @PathVariable annotation provides a&amp;nbsp; means to access the parameters passed in the URI. For eg, an HTTP request to fetch the details of a customer with id '1' would look like this:&lt;br /&gt;&amp;nbsp; http://localhost:CMSService/customer/1&lt;br /&gt;The {id} part of the URI is made available to the method using the @PathVariable annotation.&lt;br /&gt;&lt;br /&gt;The CustomerService instance is autowired and will be passed on to the Controller class when the spring context is loaded.&lt;br /&gt;Also notice, that the controller returns a ModelAndView object with a view name 'customerView'. This view is configured in the spring context xml, which we will look at shortly.&lt;br /&gt;&lt;br /&gt;[Note: I have implemented only the GET an POST methods, you can also try deleting or updating the customers using the RequestMethod.DELETE and RequestMethod.PUT method attributes in the @RequestMapping annotation on methods.]&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 6: Set up the configuration files.&lt;/b&gt; &lt;br /&gt;If you are using the STS IDE, you would not need to set up your web.xml file, since this is already done for you. This is how your web.xml should look.&lt;br /&gt;&lt;blockquote&gt;&amp;lt;!-- Creates the Spring Container shared by all Servlets and Filters --&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;listener&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;listener-class&amp;gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/listener&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- Processes application requests --&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-name&amp;gt;appServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-class&amp;gt;&lt;b&gt;org.springframework.web.servlet.DispatcherServlet&lt;/b&gt;&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;init-param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-name&amp;gt;&lt;b&gt;contextConfigLocation&lt;/b&gt;&amp;lt;/param-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-value&amp;gt;/WEB-INF/spring/appServlet/&lt;b&gt;servlet-context.xml&lt;/b&gt;&amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/init-param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/servlet&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-name&amp;gt;&lt;b&gt;appServlet&lt;/b&gt;&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;url-pattern&amp;gt;/&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/servlet-mapping&amp;gt;&lt;/blockquote&gt;&amp;nbsp;This basically sends all the requests that are recieved to Spring's DispatcherServlet (which is the Front controller) We are also specifying that the base spring context file is 'servlet-context.xml' file. Let's look at the servlet-context.xml next.&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- DispatcherServlet Context: defines this servlet's request-processing &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; infrastructure --&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- Enables the Spring MVC @Controller programming model --&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&amp;lt;annotation-driven /&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; up static resources in the ${webappRoot}/resources directory --&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;resources mapping="/resources/**" location="/resources/" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:bean id="&lt;b&gt;jaxbMarshaller&lt;/b&gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="classesToBeBound"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:value&amp;gt;com.test.cmsservice.domain.&lt;b&gt;Customer&lt;/b&gt;&amp;lt;/beans:value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:value&amp;gt;com.test.cmsservice.domain.&lt;b&gt;CustomerList&lt;/b&gt;&amp;lt;/beans:value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:bean id="&lt;b&gt;customerView&lt;/b&gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.web.servlet.view.xml.MarshallingView"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:constructor-arg ref="jaxbMarshaller" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:bean&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.web.servlet.view.&lt;b&gt;ContentNegotiatingViewResolver&lt;/b&gt;"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="mediaTypes"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:map&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:entry key="xml" value="application/xml" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:entry key="html" value="text/html" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:map&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="&lt;b&gt;viewResolvers&lt;/b&gt;"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:bean&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.web.servlet.view.&lt;b&gt;BeanNameViewResolver&lt;/b&gt;" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:bean id="viewResolver"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.web.servlet.view.&lt;b&gt;UrlBasedViewResolver&lt;/b&gt;"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="viewClass"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; value="org.springframework.web.servlet.view.JstlView" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="prefix" value="/WEB-INF/views/" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="suffix" value=".jsp" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:bean&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/beans:bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- Imports user-defined @Controller beans that process client requests --&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&amp;lt;beans:import resource="controllers.xml" /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;lt;/beans:beans&amp;gt;&lt;/blockquote&gt;The &amp;lt;annotation-driven&amp;gt; tag informs the container that it needs to look for annotated classes. &lt;br /&gt;&lt;br /&gt;Next we define a jaxbMarshaller bean, to marshall and unmarshall the domain classes. A list of domain classes to be handled is passed next. We also specify a couple of viewResolvers for each mediaType.&lt;br /&gt;&lt;br /&gt;Next we define a MarshallingView bean named 'customerView'. This bean takes the &lt;b&gt;jaxbMarshaller &lt;/b&gt;bean to transform the object into an xml document. This is the view name being used to construct the ModelAndView objects returned by the controller methods. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next we define a &lt;b&gt;ContentNegotiationViewResolver&lt;/b&gt;. What is content negotiation? It is the concept where the server returns a specific representation of the objects depending upon the content-type it recieves in the request. The ContentNegotiationViewResolver can switch the views depending on the request type (specified by the &lt;i&gt;Accept&lt;/i&gt; request header) Several types are supported such as HTML, XML, JSON, ATOM, PDF etc. In this case we have defined 2 supported 'mediaTypes' : xml and html.We also define viewResolvers for each mediaType.&lt;br /&gt;&lt;br /&gt;[Note: If you want to render a jsp file instead of the xml format, create a customer.jsp file in /WEB-INF/views, and simply change the view name&amp;nbsp; in the controller class from :&lt;br /&gt;&lt;br /&gt;return new ModelAndView("&lt;b&gt;customerView&lt;/b&gt;", "customer", customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; to&lt;br /&gt;return new ModelAndView("&lt;b&gt;customer&lt;/b&gt;", "customer", customer);&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;UrlBasedViewResolver &lt;/b&gt;viewResolver will then find the file customer.jsp in the /WEB-INF/views folder&lt;br /&gt;&lt;br /&gt;and render the object. ]&lt;br /&gt;&lt;br /&gt;Next, we import the controllers.xml file which specifies the base package for component scanning (to look for annotated classed)&lt;br /&gt;&lt;blockquote&gt;&amp;lt;context:component-scan base-package="com.test.cmsservice" /&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;b&gt;STEP 7: Build and Run!&lt;/b&gt;&lt;br /&gt;You are now ready to test your Spring REST webservice. Right click the project and click 'Run on Server'.&lt;br /&gt;Browse to &lt;u&gt;&lt;span style="color: blue;"&gt;http://localhost:8080/CMSService/customer&lt;/span&gt;&lt;/u&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: black;"&gt;, you should be able to see something like this:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-y6lBcBBclBU/TVOMqV2OzMI/AAAAAAAAAlU/60JUvoWrPNs/s1600/REST-ws.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-y6lBcBBclBU/TVOMqV2OzMI/AAAAAAAAAlU/60JUvoWrPNs/s1600/REST-ws.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: black;"&gt;Also try this:&lt;/span&gt; &lt;u&gt;http://localhost:8080/CMSService/customer/2&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;If you are not using the IDE, you can use curl or write a small Spring REST based client which i will explain next.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 8: Write a Spring REST web-service client&lt;/b&gt;&lt;br /&gt;Writing a web-service client using Spring REST&lt;b&gt; &lt;/b&gt;is pretty easy. The most important class is the &lt;b&gt;RestTemplate&lt;/b&gt; class which is meant to be used the way we use JdbcTemplate, JmsTemplate etc. &lt;br /&gt;&lt;br /&gt;For this demo, i am adding a client-context.xml to the same project inside the /WEB_INF/spring folder.&lt;br /&gt;client-context.xml&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&amp;lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="messageConverters"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&amp;lt;property name="marshaller" ref="jaxbMarshaller" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="unmarshaller" ref="jaxbMarshaller" /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean class="org.springframework.http.converter.FormHttpMessageConverter" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id="&lt;b&gt;jaxbMarshaller&lt;/b&gt;" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="classesToBeBound"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;value&amp;gt;com.test.cmsservice.domain.&lt;b&gt;Customer&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;value&amp;gt;com.test.cmsservice.domain.&lt;b&gt;CustomerList&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt; &lt;/blockquote&gt;The restTemplate bean is passed the jaxbMarshaller bean which it will use to marshall and unmarshall resources that it sends or recieves. The jaxbMarshaller is passed a list of the domain class.&lt;br /&gt;&lt;br /&gt;The RestTemplate has a couple of useful methods that you can use to POST, GET, DELETE and PUT data.&lt;br /&gt;We will use a few of them in our client code:&lt;br /&gt;&lt;blockquote&gt;package com.test.cmsservice.client;&lt;br /&gt;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.support.ClassPathXmlApplicationContext;&lt;br /&gt;import org.springframework.web.client.RestTemplate;&lt;br /&gt;import com.test.cmsservice.domain.Customer;&lt;br /&gt;import com.test.cmsservice.domain.CustomerList;&lt;br /&gt;&lt;br /&gt;public class RestClient {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext("&lt;b&gt;client-context.xml&lt;/b&gt;");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;RestTemplate &lt;/b&gt;restTemplate = (RestTemplate)ctx.getBean("&lt;b&gt;restTemplate&lt;/b&gt;");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; addCustomer(restTemplate);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; getAllCustomers(restTemplate);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void addCustomer(RestTemplate restTemplate){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("addCustomer called");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Customer customer = new Customer(3L, "Bill","Gates", "bill@microsoft.com","234");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;restTemplate.postForLocation&lt;/b&gt;(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; "http://localhost:8080/CMSService/customer", customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void getAllCustomers(RestTemplate restTemplate){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; CustomerList customers = (CustomerList)&lt;b&gt; restTemplate.getForObject&lt;/b&gt;(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; "http://localhost:8080/CMSService/customer", CustomerList.class);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(Customer customer:&amp;nbsp; customers.getCustomers()){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Customer: "+ customer.getId());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Name: "+ customer.getFirstName() + " " + customer.getLastName());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Email: "+ customer.getEmailAddress());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;The client code is pretty straightforward. We are first adding a customer using the &lt;b&gt;postForLocation&lt;/b&gt; method which takes the web-service url and the object to be POSTed.&lt;br /&gt;We then fetch the list of all the customers by using the method &lt;b&gt;getForObject &lt;/b&gt;which returns a CustomerList object.&lt;br /&gt;Try your hand at the &lt;b&gt;put&lt;/b&gt;, &lt;b&gt;delete&lt;/b&gt;, &lt;b&gt;exchange &lt;/b&gt;methods of the RestTemplate class for deleting and updating the customers using the webservice.&lt;br /&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/spring/CMSService.rar?attredirects=0&amp;amp;d=1"&gt;Download the source code of the demo&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;Hope you find this article helpful.&lt;br /&gt;Happy coding!&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-2205255974892595358?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/2205255974892595358/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/restful-webservices-with-spring.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/2205255974892595358'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/2205255974892595358'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/restful-webservices-with-spring.html' title='RESTful webservices with Spring'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-_4HAptLQGGs/TVNu-ax8vrI/AAAAAAAAAlQ/Uq4nCx-gX8E/s72-c/spring_mvc_template.jpg' height='72' width='72'/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-7432952471357389414</id><published>2011-02-07T21:11:00.000-08:00</published><updated>2011-02-07T21:13:37.785-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Groovy'/><title type='text'>Read from a database and write into an xml using Groovy</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;You may come across a use-case where you need to read some data from a database and generate an xml report or file that needs to be sent across to be processed by some other system.&lt;br /&gt;&lt;br /&gt;Doing this using a java program could be relatively overwhelming; with Groovy you can do this in a matter of minutes!&lt;br /&gt;&lt;br /&gt;Let's take a look at how you can convert data returned from a sql query into an xml file.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;For this example, i am fetching some employee details from a MySQL database. The details of the database are read from a property file. The database i have used for this sample has two tables Employee and Department.&lt;br /&gt;&lt;blockquote&gt;import groovy.sql.Sql&lt;br /&gt;import groovy.xml.MarkupBuilder&lt;br /&gt;import java.text.SimpleDateFormat&lt;br /&gt;import java.util.Properties&lt;br /&gt;&lt;br /&gt;class DatabaseToXML {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp; static void main(args) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp; SimpleDateFormat runDateformat = new SimpleDateFormat("yyyyMMdd")&lt;br /&gt;&amp;nbsp; String runDate = runDateformat.format(new Date())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp; &lt;b&gt;def props = new Properties()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new File('application.properties').withInputStream {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stream -&amp;gt; props.load(stream)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; String outFolderName = (props["OUTPUT_FOLDER"]==null)?"."+File.separatorChar : props["OUTPUT_FOLDER"]&lt;br /&gt;&amp;nbsp; File outputFolder = new File(outFolderName)&lt;br /&gt;&amp;nbsp; if(!outputFolder.exists())&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outputFolder.mkdir()&lt;br /&gt;&lt;br /&gt;&amp;nbsp; String fileDate = runDateformat.format(new Date())&lt;br /&gt;&amp;nbsp; String empFileName = outFolderName + File.separatorChar + "employees_" + runDate + ".xml"&lt;br /&gt;&lt;br /&gt;def sql = &lt;b&gt;Sql.newInstance&lt;/b&gt;(props["DB_URL"],props["USERNAME"],props["PASSWORD"], props["DRIVERCLASS"])&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;nbsp; def empQuery = """&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT e.id, e.first_name, e.last_name, e.email_address, e.designation,&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; d.dept_name FROM employee e, department d&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where d.id = e.dept_id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; """&lt;br /&gt;&lt;br /&gt;&amp;nbsp; def xml = new &lt;b&gt;MarkupBuilder&lt;/b&gt;(new FileWriter(new File(empFileName)))&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &lt;b&gt;xml.employees&lt;/b&gt;(){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run_date(fileDate)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;sql.eachRow&lt;/b&gt;( empQuery as String&amp;nbsp; ) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row -&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;employee&lt;/b&gt;(){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; first_name(row.first_name)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_name(row.last_name)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; email_address(row.email_address)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; designation(row.designation)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dept_name(row.dept_name)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; }&lt;br /&gt;} &lt;/blockquote&gt;That was easy! The 'Sql.newInstance ...' statement connects to the database using the parameters passed to it and stores it in the variable 'sql'. We then use the sql.eachRow to fetch the rows from the database. To create the xml file we are using the MarkupBuilder class that supports Groovy's builder pattern. Find more options &lt;a href="http://groovy.codehaus.org/Processing+XML"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To make this script you would need to add the mysql connector lib to the classpath.&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;SET CLASSPATH=./lib/mysql-connector-java-5.1.12-bin.jar&lt;/blockquote&gt;Run the script using the command :&lt;br /&gt;&lt;blockquote&gt;groovy DatabaseToXML.groovy&lt;/blockquote&gt;&amp;nbsp;The xml file in my case looks like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TVDP0ZTFKsI/AAAAAAAAAlM/JPkQOunLq9U/s1600/groovy_xml_file.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_558g1ZlipEY/TVDP0ZTFKsI/AAAAAAAAAlM/JPkQOunLq9U/s1600/groovy_xml_file.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Download the source code for the above example here: &lt;a href="https://sites.google.com/site/javaunplugged/groovy/db_to_xml.rar?attredirects=0&amp;amp;d=1"&gt;db_to_xml.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Hope you find this useful. Happy coding!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-7432952471357389414?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/7432952471357389414/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/read-from-database-and-write-into-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/7432952471357389414'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/7432952471357389414'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/read-from-database-and-write-into-xml.html' title='Read from a database and write into an xml using Groovy'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_558g1ZlipEY/TVDP0ZTFKsI/AAAAAAAAAlM/JPkQOunLq9U/s72-c/groovy_xml_file.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-4336400031352916507</id><published>2011-02-07T10:18:00.000-08:00</published><updated>2011-02-07T10:25:03.826-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Add search to your grails application using the searchable plugin</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;If you're building a web application, you're most certainly going to need some 'search' capabilities in your application. The searchable plugin is an amazing plugin that is built using Compass search engine and lucene. This plugin enables you to make your domain classes without writing almost any code. In this article we will be discussing how you can install and use this plugin, and also discuss some issues around it.&lt;br /&gt;&lt;br /&gt;We will be creating a simple grails application, a couple of domain classes, and then we will explore the 'searchable' plugin. (find the source code of the sample application at the end of the article)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let's get started!&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 1:&amp;nbsp; Create a grails app and install the searchable plugin&lt;/b&gt;&lt;br /&gt;We will create a very simple basic application. To learn how to install and get started with grails please take a look at the blog article '&lt;a href="http://java-diaries.blogspot.com/2011/01/create-your-first-grails-application.html"&gt;Build your first grails application&lt;/a&gt;'.&lt;br /&gt;Open a command prompt and create your sample application:&lt;br /&gt;&lt;blockquote&gt;grails create-app empsys &lt;br /&gt;cd empsys &lt;br /&gt;grails install-plugin searchable &lt;/blockquote&gt;&lt;b&gt;STEP 2: Create your domain classes&lt;/b&gt;&lt;br /&gt;Now let's create some domain classes which we would make 'searchable'. To create the domain class use the command:&lt;br /&gt;&lt;blockquote&gt;grails create-domain-class Employee&lt;br /&gt;grails create-domain-class Department&lt;/blockquote&gt;Let's add some fields to our domain classes.&lt;br /&gt;Employee.groovy &lt;br /&gt;&lt;blockquote&gt;package empsys&lt;br /&gt;&lt;br /&gt;class Employee {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;static searchable = true&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;i&gt;static embedded = ['dept']&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Department dept&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String empCode&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String firstName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String lastName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String emailAddress&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String phoneNumber&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String designation&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Date birthDate&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Date joiningDate&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static constraints = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; empCode(blank: false, unique: true)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; firstName(blank: false)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; lastName(blank: false)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; emailAddress(blank: false, email:true)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;} &lt;/blockquote&gt;&lt;br /&gt;Department.groovy&lt;br /&gt;&lt;blockquote&gt;package empsys&lt;br /&gt;&lt;br /&gt;class Department {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;static searchable = true&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String deptCode&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String deptName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String description&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static constraints = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;Note the statement 'static searchable = true'. This is necessary if you want to be able to search the objects of the domain class. (we will shortly discuss the 'static embedded' statement) &lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 3: Create controllers and views for the domain classes&lt;/b&gt;&lt;br /&gt;To create the controller and views for your class use this command:&lt;br /&gt;&lt;blockquote&gt;grails generate-all empsys.Department &lt;br /&gt;grails generate-all empsys.Employee&lt;/blockquote&gt;&lt;br /&gt;At this point, you can run the application using the command: &lt;b&gt;grails run-app&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;You can now test the plugin, run the application, create some test data i.e. add some departments and employees to your system. Browse to : &lt;span style="color: blue;"&gt;http://localhost:8080/empsys/searchable &lt;span style="color: black;"&gt;and try searching for the employees ( and departments) you just added to your system. How cool is that!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TU7p-c3jA3I/AAAAAAAAAlA/RyTwWSsd8d4/s1600/search_page.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="242" src="http://3.bp.blogspot.com/_558g1ZlipEY/TU7p-c3jA3I/AAAAAAAAAlA/RyTwWSsd8d4/s640/search_page.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span style="color: black;"&gt;Tip: you may want to change your datasource.groovy to save into an hsqldb file instead of the default memdb or you'll loose all your test data. (&lt;/span&gt;&lt;/span&gt;url = "jdbc:hsqldb:file:devDb")&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 5: Customize the controller and the views&lt;/b&gt;&lt;br /&gt;In addition to the 'searchable' controller which searches all the 'searchable' domain classes, you'd typically need a search box and a button on the '/employee/list' page to search for employees. You can easily do this, by using the views and controllers generated by the plugin. The controllers and views generated by the plugin can be found at:&lt;br /&gt;&lt;div style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&lt;span style="color: blue;"&gt;%USER_HOME%\.grails\1.3.4\projects\empsys\plugins\searchable-0.5.5.1\grails-app&lt;/span&gt;&lt;/div&gt;Copy the content of SearchableController.java, change the function name from 'index' to say 'search' and copy it to your EmployeeController.java.&lt;br /&gt;&lt;blockquote&gt;/* add this import statement for the searchable plugin to work */ &lt;br /&gt;import org.compass.core.engine.SearchEngineQueryParseException&lt;br /&gt;&lt;br /&gt;class EmployeeController {&lt;br /&gt;static String WILDCARD = "*"&lt;br /&gt;def searchableService&lt;br /&gt;&lt;br /&gt;.........&amp;nbsp; &lt;br /&gt;def search = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (!params.q?.trim()) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [:]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;String searchTerm = WILDCARD+ params.q + WILDCARD&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [searchResult: &lt;b&gt;Employee.search(searchTerm, params)]&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } catch (SearchEngineQueryParseException ex) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [parseException: true]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp; def indexAll = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thread.start {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; searchableService.index()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; render("bulk index started in a background thread")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def unindexAll = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; searchableService.unindex()&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; render("unindexAll done")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;......&lt;br /&gt;} &lt;/blockquote&gt;[Note: We've made a small change to the default search functionality provided here. We are appending the search term with a wildcard '*' so that even if the user enters part of the employee's first name or last name or any other field, the employee is featured in the search results. ]&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Let's add a search box and a button on the employee/list.gsp page. Add the following code to \views\employee\list.gsp file after the navigation buttons:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;div class="nav"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;span class="menuButton"&amp;gt;&amp;lt;a class="home" href="${createLink(uri: '/')}"&amp;gt;&amp;lt;g:message code="default.home.label"/&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;span class="menuButton"&amp;gt;&amp;lt;g:link class="create" action="create"&amp;gt;&amp;lt;g:message code="default.new.label" args="[entityName]" /&amp;gt;&amp;lt;/g:link&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;div class="body"&amp;gt;&lt;br /&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;g:form action="search" method="get"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;g:textField name="q" value="${params.q}"/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;g:submitButton name="search"/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/g:form&amp;gt; ......&lt;/b&gt;&lt;/blockquote&gt;We also need to create a new gsp view to display the results of the search. This page takes most of its code from the &lt;span style="color: blue;"&gt;%USER_HOME%\.grails\1.3.4\projects\empsys\plugins\searchable-0.5.5.1\grails-app\views\searchable\index.gsp &lt;span style="color: black;"&gt;file and the views\employee\list.gsp page. I am not copying the code here since it's pretty long and would make the article look ugly :) But you can find the source attached at the bottom of the post.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Once you have fixed your views, you should be able to see something like this:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TU-e8hFTr9I/AAAAAAAAAlI/caWV0tyVnyo/s1600/search_box.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_558g1ZlipEY/TU-e8hFTr9I/AAAAAAAAAlI/caWV0tyVnyo/s1600/search_box.jpg" /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;And the search result page would look like this:&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TU-e8KL34cI/AAAAAAAAAlE/5vA0PlE5KVY/s1600/search_results.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_558g1ZlipEY/TU-e8KL34cI/AAAAAAAAAlE/5vA0PlE5KVY/s1600/search_results.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;/div&gt;&lt;br /&gt;It's time to now go back to the Employee.groovy file&lt;br /&gt;We had added a line&amp;nbsp; '&lt;b&gt;&lt;i&gt;static embedded = ['dept']&lt;/i&gt;&lt;/b&gt;' to our Employee domain class. Here we are making use of 'composition'. Since the Department class is also 'searchable' and we have used composition, you will be able to search for the all employees belonging to a certain category using the above screen. i.e. If your employee belongs to a department with department name 'Finance', on searching the text 'Finance' the employee will show up in the results.&lt;br /&gt;You can read more about the searchable-reference and searchable-components &lt;a href="http://www.grails.org/Searchable+Plugin+-+Mapping+-+Conventions"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The plugin provides loads of options to customize the search to suit your requirements for eg: query mechanisms, builders, mappings etc. There are a couple of mappings you can add to the 'static searchable' closure. eg: if you want to exclude some fields from the search or you want to boost a certain field while searching, update your closure as follows:&lt;br /&gt;&lt;blockquote&gt;&lt;code&gt;static searchable = {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; /* we don't wish the id and version to be 'searched' &lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; or added to the index */&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except = ['id', 'version']&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; empCode boost:2.0&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/code&gt;&lt;/blockquote&gt;In this article, we've implemented a very basic search functionality for our domain classes. There's a lot of stuff in this plugin that you can explore and use in your application.&lt;br /&gt;&lt;br /&gt;Hope you find this article useful.&lt;br /&gt;&lt;br /&gt;Happy coding !&lt;br /&gt;&lt;br /&gt;Download the source code of the sample search application : &lt;a href="https://sites.google.com/site/javaunplugged/grails_search/empsys.rar?attredirects=0&amp;amp;d=1"&gt;empsys.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-4336400031352916507?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/4336400031352916507/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/add-search-to-your-grails-application.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/4336400031352916507'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/4336400031352916507'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/add-search-to-your-grails-application.html' title='Add search to your grails application using the searchable plugin'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_558g1ZlipEY/TU7p-c3jA3I/AAAAAAAAAlA/RyTwWSsd8d4/s72-c/search_page.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6392231343932856326</id><published>2011-02-01T23:48:00.000-08:00</published><updated>2011-02-06T08:52:46.217-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Spring'/><category scheme='http://www.blogger.com/atom/ns#' term='Hibernate'/><title type='text'>Spring Hibernate Integration</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In this article, I will be demonstrating how you can integrate hibernate with your Spring application. We will not be discussing hibernate and Spring in any detail. The focus is to get you started with an Spring application that persists data into a database using the hibernate ORM layer.&lt;br /&gt;&lt;br /&gt;I will safely assume that you are familiar with Spring, hibernate, SQL, maven. For the purpose of this demo, i will be using the &lt;a href="http://www.mysql.com/"&gt;MySQL&lt;/a&gt; database, &lt;a href="http://www.springsource.com/developer/sts"&gt;STS&lt;/a&gt; (SpringSource Tool Suite) or Eclipse with Spring support plugins installed. If you are not using any IDE, you just need to create some folders and config files yourself. But i see no reason why you shouldn't since it will really speed your development process.&lt;br /&gt;(find the source code of the sample application at the end of the page)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So Let's get started!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;b&gt;&amp;nbsp;STEP 1: Create a Spring hibernate Utility project&lt;/b&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;I am using STS for creating my project; STS is a customized eclipse environment. You can use your favorite eclipse installation, for sake of ease, install the Spring plugins and you're ready to go. If you are going the manual way, nothing to worry about. Just a few folders and config files to be created.&lt;br /&gt;&lt;br /&gt;Choose the 'Spring Template Project' from the File -&amp;gt; New menu or the dashboard.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TUpwB7-NQsI/AAAAAAAAAko/uCA_8eWmxS0/s1600/template_menu.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_558g1ZlipEY/TUpwB7-NQsI/AAAAAAAAAko/uCA_8eWmxS0/s1600/template_menu.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TUpvjhtVFhI/AAAAAAAAAkk/6s-cZZ7b95I/s1600/Spring_template_project.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;From the Template Selection dialog, choose 'Simple Spring Hibernate Utility Project'&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TUpwhVLD5vI/AAAAAAAAAks/M_ouh7RjruM/s1600/hibernate_template.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_558g1ZlipEY/TUpwhVLD5vI/AAAAAAAAAks/M_ouh7RjruM/s1600/hibernate_template.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Type in a project name and a base package name ( eg. EStore and com.test.estore respectively) and press Finish. You will see that the following code structure has been generated for you&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TUpxaVRqSjI/AAAAAAAAAkw/R9tQjQaOKfk/s1600/hib_code_structure.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_558g1ZlipEY/TUpxaVRqSjI/AAAAAAAAAkw/R9tQjQaOKfk/s1600/hib_code_structure.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;A few configuration files will be genereated here. You will find a pom.xml at the base folder of the project. This is where we will add the maven dependencies in a short while. You will also find the app-context.xml which is the base spring config file at /src/main/resources/META-INF/spring. Any property files needed for the project can be placed inside /src/main/resources/META-INF/properties.&lt;br /&gt;&lt;br /&gt;The maven configuration file - pom.xml will already have all the dependencies for using Spring and hibernate. Since we are using the mysql database, we will need to add the following dependency to use the mysql connector:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;mysql&amp;lt;/groupId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;mysql-connector-java&amp;lt;/artifactId&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;5.1.12&amp;lt;/version&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;/blockquote&gt;If you are not using the IDE, you will have to add a couple of dependencies to your pom.xml. Please refer the pom.xml in the source code attached at the end of the article.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 2: Create a domain class&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;We will create a simple domain class called Product in a seperate package 'com.test.estore.model'. Each domain class maps to a table in the database. Let's take a look at the code and i then walk through the code.&lt;br /&gt;&lt;blockquote&gt;&lt;b&gt;@Entity&lt;br /&gt;@Table(name = "Product")&lt;/b&gt;&lt;br /&gt;public class Product {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Integer productId;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String productCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String description;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Double price;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Id&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @GeneratedValue(strategy = GenerationType.AUTO)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Column(name = "ID")&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Integer getProductId() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return productId;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setProductId(Integer productId) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.productId = productId;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Column(name = "NAME", nullable = false, length = 50)&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getName() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setName(String name) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.name = name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt; @Column(name = "CODE", nullable = false, unique = true, length = 20)&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getProductCode() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return productCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setProductCode(String productCode) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.productCode = productCode;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Column(name = "DESCRIPTION")&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getDescription() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return description;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setDescription(String description) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.description = description;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Column(name = "PRICE")&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Double getPrice() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return price;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setPrice(Double price) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.price = price;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;}&lt;/blockquote&gt;As you see, Product is a very simple POJO with a few additional annotations. The &lt;b&gt;@Entity&lt;/b&gt; annotation specifies that this class is persistent and the &lt;b&gt;@Table&lt;/b&gt; annotation specifies the name of the table to which it will be mapped. Please note that the @Table annotation is not compulsory.&lt;br /&gt;&lt;br /&gt;The&lt;b&gt; @Column &lt;/b&gt;annotation specifies the name of the column to be mapped with the field in the class. Again, it is NOT necessary to specify the @Column annotation unless you want the column name to be different than the field name. Every field in the class will be by default persisted into a column of the table, unless specifically annotated with the @Transient annotation. In addition to the name, you can also specify whether the column is nullable, unique, length restrictions etc.&lt;br /&gt;&lt;br /&gt;The &lt;b&gt;@Id&lt;/b&gt; annotation specified the &lt;i&gt;identity&lt;/i&gt; property of the class. In hibernate, every table needs to have this unique key. There are different strategies available for generating the values for this field. In this case, we are using GenrationType.Auto.&lt;br /&gt;&lt;br /&gt;These annotations can be added over the fields of the getters/setters. The placement of the @Id annotation determines which strategy will be used. In this particular case for eg, since the @Id annotation is placed over the property getter/setter, the property access strategy is used instead of the field access.&lt;br /&gt;(Field access means: hibernate will access the field directly using reflection.&lt;br /&gt;&amp;nbsp;Property access: hibernate will use the get/set pair to access the value - property access is the default)&lt;br /&gt;&lt;br /&gt;Let's now move to the next step and create the data access layer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 3: Create the DAO layer&lt;/b&gt;&lt;br /&gt;Now that our model class is ready, let's create the DAO interface and it's implementation in a package say : 'com.test.estore.dao'&lt;br /&gt;IProductDAO.java &lt;br /&gt;&lt;blockquote&gt;package com.test.estore.dao;&lt;br /&gt;import com.test.estore.model.Product;&lt;br /&gt;&lt;br /&gt;public interface IProductDAO {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void save(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void update(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void delete(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Product findByProductCode(String code);&lt;br /&gt;}&lt;/blockquote&gt;&amp;nbsp;ProductDAO.java&lt;br /&gt;&lt;blockquote&gt;package com.test.estore.dao;&lt;br /&gt;&amp;nbsp;/*import statements*/&lt;/blockquote&gt;&lt;blockquote&gt;&lt;b&gt;@Repository("productDao")&lt;/b&gt;&lt;br /&gt;public class ProductDAO implements IProductDAO {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;private HibernateTemplate hibernateTemplate;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;b&gt;@Autowired&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void setSessionFactory(SessionFactory sessionFactory) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;hibernateTemplate = new HibernateTemplate(sessionFactory);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void save(Product product) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;hibernateTemplate.save(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void update(Product product) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;hibernateTemplate.update(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void delete(Product product) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;hibernateTemplate.delete(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public Product findByProductCode(String code) {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;List products = hibernateTemplate.find(&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;"from Product where productCode = ?", code);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return (Product) products.get(0);&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br /&gt;}&lt;/blockquote&gt;The ProductDAO class is quite self explanatory, there're just two points worth writing about.&lt;br /&gt;1. Notice the &lt;b&gt;@Repository&lt;/b&gt; annotation on the ProductDAO class. @Repository is a specialized @Component type. @Repository indicates that the class acts as a repository or a data access object. This enables transparent exception translation. And it also provides a more clear scope for applying a pointcut or an aspect for future support.&lt;br /&gt;&lt;br /&gt;The @Autowired annotation indicates that the sessionFactory bean is injected into the ProductDAO by the spring container.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 4: Create the service layer&lt;/b&gt;&lt;br /&gt;Now that we have the DAO layer in place, it's time to setup the service layer. The service layer is pretty straightforward:&lt;br /&gt;IProductService.java&lt;br /&gt;&lt;blockquote&gt;package com.test.estore.service;&lt;br /&gt;import com.test.estore.model.Product;&lt;br /&gt;&lt;br /&gt;public interface IProductService {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void save(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void update(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void delete(Product product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Product findByProductCode(String code);&lt;br /&gt;}&lt;/blockquote&gt;ProductService.java&lt;br /&gt;&lt;blockquote class=""&gt;package com.test.estore.service;&lt;br /&gt;&lt;br /&gt;/* import statements */ .... &lt;br /&gt;&lt;br /&gt;@Service("productService")&lt;br /&gt;public class ProductService {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; @Autowired&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ProductDAO productDAO;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; public void save(Product product){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("save called");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; productDAO.save(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; public void update(Product product){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; productDAO.update(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; public void delete(Product product){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; productDAO.delete(product);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; public Product findByProductCode(String code)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return productDAO.findByProductCode(code);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;&lt;b&gt;STEP 5: Configuration files&lt;/b&gt;&lt;br /&gt;The most important configuration file in this application is the base Spring context xml. For the sake of clarity, we will place all the hibernate related configuration in a separate configuration file - hibernate.xml. We will define the datasource bean and the sessionFactory bean in hibernate.xml. We will be storing the database details in a property file and read the property file in the context xml. Let's take a look at these configs:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;application.properties&lt;br /&gt;&lt;blockquote&gt;jdbc.driverClassName=com.mysql.jdbc.Driver&lt;br /&gt;jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate&lt;br /&gt;jdbc.username=root&lt;br /&gt;jdbc.password=password&lt;/blockquote&gt;&amp;nbsp;hibernate.xml&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="location"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;value&amp;gt;&lt;b&gt;application.properties&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id="&lt;b&gt;dataSource&lt;/b&gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.jdbc.datasource.DriverManagerDataSource"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="driverClassName" value="${jdbc.driverClassName}" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="url" value="${jdbc.url}" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="username" value="${jdbc.username}" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="password" value="${jdbc.password}" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id="&lt;b&gt;sessionFactory&lt;/b&gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="dataSource" ref="&lt;b&gt;dataSource&lt;/b&gt;" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="&lt;b&gt;annotatedClasses&lt;/b&gt;"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;value&amp;gt;&lt;b&gt;com.test.estore.model.Product&lt;/b&gt;&amp;lt;/value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/list&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name="hibernateProperties"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;props&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;prop key="hibernate.dialect"&amp;gt;org.hibernate.dialect.MySQLDialect&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;prop key="hibernate.show_sql"&amp;gt;true&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;prop key="&lt;b&gt;hibernate.hbm2ddl.auto&lt;/b&gt;"&amp;gt;&lt;b&gt;create&lt;/b&gt;&amp;lt;/prop&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/props&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/bean&amp;gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/blockquote&gt;In the above config, we read the database details from the property file and create the dataSource bean. Here we don't need any hibernate.cfg.xml file, we declare the hibernate sessionFactory bean in the xml file to which we pass the dataSource bean. We also list down the list of all the 'annotatedClasses' in the application i.e. all the model classes (just like we specify the mapping files in the hibernate.cfg.xml file).&lt;br /&gt;Also in this case, we want to create the table when we run the application the first time, and hence the value of 'hibernate.hbm2ddl.auto' has been set to 'create'. This is all we need to do to set up the configs for our database.&lt;br /&gt;&lt;br /&gt;Next we need to setup the core spring config - app-context.xml&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xmlns:context="http://www.springframework.org/schema/context"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xsi:schemaLocation="http://www.springframework.org/schema/beans&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/context&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; http://www.springframework.org/schema/context/spring-context-2.5.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&amp;lt;import resource="hibernate.xml"/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;context:component-scan base-package="com.test.estore" /&amp;gt;&lt;/b&gt;&lt;br /&gt;&amp;lt;/beans&amp;gt;&lt;/blockquote&gt;The first line imports the hibernate config file. Also, we need to tell the Spring container that it needs to look for annotated classes. To do this we need to use the component-scan tag and pass to it the base package - this is where the container starts it's search from.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 6: Write the test application&lt;/b&gt;&lt;br /&gt;Now that we're done with almost everything, we need to test the application. All we need is a simple class with the main method that loads the spring context and tries to persist a Product into the database&lt;br /&gt;&lt;blockquote&gt;package com.test.estore.app;&lt;/blockquote&gt;&lt;blockquote&gt;/* import statements */ .. &lt;br /&gt;&lt;br /&gt;public class RunEStore {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ProductService productService = (ProductService) ctx.getBean("productService");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Product product1 = new Product();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; product1.setName("maggie noodles");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; product1.setDescription("noodles made out of flour");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; product1.setProductCode("MAGG_NOODLES");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; product1.setPrice(30D);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; productService.save(product1);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println("Product saved: "&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; + productService.findByProductCode("MAGG_NOODLES").getName());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;So, we're ready to go.. wait , not yet!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 7: Where's the database?&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;We first need to create the 'spring_hibernate' schema that we specified in the application.properties file :&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TUryhlv9_6I/AAAAAAAAAk4/jIUYT8yqhCU/s1600/mysql_databse.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_558g1ZlipEY/TUryhlv9_6I/AAAAAAAAAk4/jIUYT8yqhCU/s1600/mysql_databse.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;Now we're ready to go. Run the RunEStore class, a table called 'Product' would be created and a row would be added to the table.&lt;br /&gt;&lt;br /&gt;This was a very very basic example. You would mostly use hibernate in a Spring MVC project; you need to follow the same steps, just a few configurations would need to change and additions to the pom.xml file would be needed.&lt;br /&gt;There's a lot more to Spring hibernate such as exception handling, one-to-many, many-to-many relationships. Another article, another day.&lt;br /&gt;&lt;br /&gt;Hope you found this demo useful.&lt;br /&gt;Happy coding! &lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/goog_41711787"&gt; &lt;/a&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/spring_hibernate/EStore.rar?attredirects=0&amp;amp;d=1"&gt;Download the sample application : EStore.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6392231343932856326?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6392231343932856326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/spring-hibernate-integration.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6392231343932856326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6392231343932856326'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/spring-hibernate-integration.html' title='Spring Hibernate Integration'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_558g1ZlipEY/TUpwB7-NQsI/AAAAAAAAAko/uCA_8eWmxS0/s72-c/template_menu.jpg' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-1565285920773601910</id><published>2011-02-01T03:11:00.000-08:00</published><updated>2011-02-03T07:07:14.467-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Spring'/><title type='text'>Get started with Spring MVC</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;Spring MVC, one of the modules of the core Spring framework,&amp;nbsp; is a web-application development framework. As the name suggests, Spring MVC is an implementation of the Model(M), View(V), Controller(C) design pattern, which is based on request delegation and separation-of-concerns.&lt;br /&gt;I am assuming, that if you have reached this article and are about to use it, you would be familiar with the basics of web-application development and the Spring framework in general. And Java of course ;)&lt;br /&gt;&lt;br /&gt;I will briefly introduce you to the basics of how Spring MVC works and then will build a simple web-application (find the sample application source code for download at the end of the page).&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #134f5c;"&gt;&lt;b&gt;&lt;u&gt;Spring MVC web-flow&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;Spring MVC uses a request delegation model, where all the incoming requests are delegated to a 'Front Controller' called &lt;i&gt;DispatcherServlet&lt;/i&gt;. This servlet has no business logic; it's sole purpose is to route the request across the system. The servlet uses different strategies (HandlerMapping) to determine the &lt;i&gt;controller &lt;/i&gt;to which the incoming request needs to be delegated. The request is then sent to the controller determined by the HandlerMapping. The controller would typically call service class methods that contain all the business logic, and after all the necessary processing is done, the controller returns a logical &lt;i&gt;view name and a model&lt;/i&gt; back to the DispatcherServlet. The DispatcherServlet, again uses several strategies (ViewResolver) to determine which View needs to be used to render the model. The model is rendered using the view returned from the ViewResolver and the response is sent back to the user.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TUeszmY2_7I/AAAAAAAAAjA/kSDr3F3Rjz8/s1600/SpringMVC.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_558g1ZlipEY/TUeszmY2_7I/AAAAAAAAAjA/kSDr3F3Rjz8/s1600/SpringMVC.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;These components are wired together using the Spring IoC and Dependency Injection mechanisms.&lt;br /&gt;&lt;br /&gt;There are two approaches that you can use to build your web application.&lt;br /&gt;1. use xml configuration files.&lt;br /&gt;2. use annotated java classes.&lt;br /&gt;Although you cannot completely give up configuration files, you can save your self quite a bit of xml slurping by using java annotations.&lt;br /&gt;You can also mix and match and use a hybrid of both the approaches. Whatever suites you best.&lt;br /&gt;&lt;br /&gt;Since annotation based controllers are gaining popularity and are favored by most java developers, i will be using this approach through most part of the demo.&lt;br /&gt;&lt;br /&gt;So, it's time to get started!&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #134f5c;"&gt;&lt;u&gt;&lt;b&gt;Spring MVC sample applicaiton : Customer Management System&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;There are a lot of IDEs available with Spring support that enable you to fasten your development process.&lt;br /&gt;I will be using the SpringSource Tool Suite that can be downloaded from &lt;a href="http://www.springsource.com/developer/sts"&gt;here&lt;/a&gt;. STS is an enhanced eclipse environment specially designed for Spring support. But you can also use your eclipse installation, and install some plugins for Spring support.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP1: Download and unzip STS and create a new project using the 'Spring Template Project'&lt;/b&gt; option on the dashboard. This template will generate quite a bit of boilerplate code for you, so you can concentrate on your application logic.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TUevr7wrr0I/AAAAAAAAAjE/82IcosT_DOY/s1600/Spring_template_project.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_558g1ZlipEY/TUevr7wrr0I/AAAAAAAAAjE/82IcosT_DOY/s1600/Spring_template_project.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator"&gt;Choose 'Spring MVC project' from the Template Selection screen. Type a project name and a top-level package name&lt;/div&gt;&lt;div class="separator"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div align="center" class="separator" style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TUfFipLC__I/AAAAAAAAAjI/bYQRvJIAbIQ/s1600/template-selection.jpg"&gt;&lt;span style="text-decoration: none;"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator"&gt;&lt;b&gt;STEP 2: Configuration files. &lt;/b&gt;You will find a couple of xml configuration files have already been created for you. Let's take a look at the most important ones.&lt;/div&gt;&lt;div class="separator"&gt;&lt;b&gt;a) web.xml&lt;/b&gt;&lt;/div&gt;&lt;div class="separator"&gt;The DispatcherServletis declared in web.xml. You also need to specify the url-pattern to determine which requests are to be routed to the DispatcherServlet. In this case, all requests will be routed to DispatcherServlet (&lt;b&gt;'/'&lt;/b&gt;). The DispatcherServlet needs to load the spring ApplicationContext, and the base configuration xml name is passed via the init-param. If nothing is passed, it will look for a file names &amp;lt;servlet-name&amp;gt;-context.xml in the classpath by default. Here's a snippet from the web.xml file:&lt;/div&gt;&lt;blockquote&gt;&lt;div class="separator"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;servlet&amp;gt;&lt;/div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;lt;servlet-name&amp;gt;appServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-class&amp;gt;org.springframework.web.servlet.&lt;b&gt;DispatcherServlet&lt;/b&gt;&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;init-param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param-value&amp;gt;/WEB-INF/spring/appServlet/&lt;b&gt;servlet-context.xml&lt;/b&gt;&amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/init-param&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/servlet&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;servlet-name&amp;gt;appServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;url-pattern&amp;gt;&lt;b&gt;/&lt;/b&gt;&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/servlet-mapping&amp;gt;&lt;/blockquote&gt;&lt;div class=""&gt;&amp;nbsp;Note: If you are not using the STS, you will need to add these servlet mappings to your applications web.xml file yourself. (as i said, the IDE saves does a lot of writing for you.)&lt;/div&gt;&lt;div class="separator"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="separator"&gt;&lt;b&gt;b) servlet-context.xml&lt;/b&gt;&lt;/div&gt;&lt;div class="separator"&gt;This is the base configuration file of your Spring MVC application. As mentioned earlier, we will be using the annotation based mechanism for declaring our Controllers, we need to specify this in the servlet-context.xml by specifying the annotation-driven tag. Also, we will be using the InternalResourceViewResolver to resolve the view to be used to render the model using a logical view name. So if the logical view name is 'index', the application will look for the following file: &lt;i&gt;/WEB-INF/views/index.jsp&lt;/i&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div class="separator"&gt;&amp;nbsp;&amp;lt;annotation-driven /&amp;gt;&lt;/div&gt;&lt;div class="separator"&gt;&lt;br /&gt;&lt;/div&gt;&amp;lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="&lt;b&gt;prefix&lt;/b&gt;" value="&lt;b&gt;/WEB-INF/views/&lt;/b&gt;" /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;beans:property name="&lt;b&gt;suffix&lt;/b&gt;" value="&lt;b&gt;.jsp&lt;/b&gt;" /&amp;gt;&lt;br /&gt;&amp;lt;/beans:bean&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;br /&gt;&amp;lt;beans:import resource="controllers.xml" /&amp;gt;&lt;/blockquote&gt;In the controllers.xml file, you will see an entry&lt;br /&gt;&lt;blockquote&gt;&amp;lt;context:component-scan base-package="com.test.cms.web" /&amp;gt;&lt;/blockquote&gt;This entry specifies that the Spring container needs to perform a component scan to look for annotated java classes, and this scan needs to begin from the base-package. Thus this basically tells the application, that it should expected @Controller annotated classes.&lt;br /&gt;&lt;br /&gt;Note: If you are not using the STS Spring MVC template, you will have to write your configuration files yourself. The IDE saves you quite a few keystrokes. Strongly recommended.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TUhBfVclJyI/AAAAAAAAAjc/AkbRPgRtZTY/s1600/Hello-world.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/_558g1ZlipEY/TUhBfVclJyI/AAAAAAAAAjc/AkbRPgRtZTY/s1600/Hello-world.jpg" /&gt;&lt;/a&gt;&lt;b&gt;STEP 3: Write your first Controller &amp;amp; Test!&lt;/b&gt;&lt;br /&gt;A simple controller would already be created in your project - HomeController. Infact, at this point, you can try to run the application. Right click the project, click 'Run as' , and then 'Run on server'. When the server has started successfully, try to access http://localhost:8080/CMSApp/, you should be able to see something like this -&amp;gt;&lt;br /&gt;&lt;br /&gt;If you are not using Maven, you would need these libs in your classpath:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TUkLjNaKgGI/AAAAAAAAAjo/4Lf5C_b6yB8/s1600/springmvc-libs.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_558g1ZlipEY/TUkLjNaKgGI/AAAAAAAAAjo/4Lf5C_b6yB8/s1600/springmvc-libs.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;If you are not using the STS IDE, you simply need to create a controller class that looks like this:&lt;br /&gt;&lt;br /&gt;&amp;nbsp;@Controller&lt;br /&gt;public class HomeController {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @RequestMapping(value="/", method=RequestMethod.GET)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String home() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; logger.info("Welcome home!");&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return "home";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;and a jsp file named 'home.jsp' in /WEB-INF/views/home.jsp that prints a welcome message.&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TUkLjNaKgGI/AAAAAAAAAjo/4Lf5C_b6yB8/s1600/springmvc-libs.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;But how real is our application right now? Not very. Let's add some business logic to it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 4:Create a service and a domain class&lt;/b&gt;&lt;br /&gt;We will create a few simple pages to add customers to the system, and list all the existing customers. The application can be enhanced further to support all the CRUD operations. But &lt;i&gt;create &lt;/i&gt;and &lt;i&gt;list &lt;/i&gt;should be just fine to begin with.&lt;br /&gt;&lt;br /&gt;Since we are here to learn about Spring MVC, i will keep the service class really simple. The service class will contain the business logic to list and save customers. For simplicity sake, we will not be persisting any data to a database, instead we will add the customers to a static list; avoiding any database setup.&lt;br /&gt;&lt;br /&gt;A very basic Customer class that looks like this:&lt;br /&gt;&lt;br /&gt;The service class would typically lie in a separate package say : com.test.cms.service&lt;br /&gt;A snippet from CustomerService.java&lt;br /&gt;&lt;blockquote&gt;public class Customer {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String gender;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String emailAddress;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String phoneNumber; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getFirstName() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void setFirstName(String firstName) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; this.firstName = firstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;br /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; //remaining getters and setters&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;br /&gt;}&lt;/blockquote&gt;The service class looks like this:&lt;br /&gt;&lt;blockquote&gt;public class CustomerService {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static ArrayList&amp;lt;Customer&amp;gt; customers = new ArrayList&amp;lt;Customer&amp;gt;();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void addCustomer(Customer customer) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customers.add(customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ArrayList&amp;lt;Customer&amp;gt; listCustomers() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return customers;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;CustomerService is a simple class with functions to add a new customer to a statically maintained arraylist and to return a list of all existing customers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 5: Create a Controller for handling multiple actions.&lt;/b&gt;&lt;br /&gt;This is where the heart of the tutorial lies. The CustomerController class is annotated with the @Controller annotation so that it is scanned by the Spring container at startup. &lt;br /&gt;&lt;blockquote&gt;&lt;b&gt;@Controller&lt;/b&gt;&lt;br /&gt;public class CustomerController {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@Autowired&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CustomerService customerService;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;@RequestMapping(value = "/customer", method = RequestMethod.GET)&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String listCustomers(ModelMap model) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ArrayList&amp;lt;Customer&amp;gt; customers = customerService.listCustomers();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.addAttribute("customers", customers);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return "listCustomers";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @RequestMapping(value = "/customer/create", method = RequestMethod.GET)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String createCustomer(ModelMap model) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Customer customer = new Customer();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.addAttribute(customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return "createCustomer";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @RequestMapping(value = "/customer/create", method = RequestMethod.POST)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String addCustomer(@ModelAttribute("customer") Customer customer) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; customerService.addCustomer(customer);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return "redirect:/customer";&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;and add the following to servlet-context.xml&lt;br /&gt;&lt;blockquote&gt;&amp;lt;beans:bean id="customerService" class="com.test.cms.service.CustomerService" &amp;gt;&lt;br /&gt;&amp;lt;/beans:bean&amp;gt;&lt;/blockquote&gt;The customerService has been autowired&amp;nbsp; into the controller class. The container will look for a bean of type customerService and wire it with the controller.&lt;br /&gt;Next, notice the @RequestMapping annotation. Any request that matches the URL pattern and request method provided in the 'value' and 'method' attributes would be delegated to the corresponding method.&lt;br /&gt;When the user hits http://localhost:8080/cmsapp/customer, the listCustomers() method is executed.&lt;br /&gt;This method has access to a ModelMap object to which objects can be added using the addAttribute method, and can be passed on to the 'view'. There are a handful of other approaches you could use. For example @RequestParam which you can use to read parameters directly from the request. To access a particular model attribute from the incoming 'post' request, the @ModelAttribute is used. The logical name of the view is returned which leads to the view jsp to be used to render the attributes added to the ModelMap. In this case, we have the listCustomer.jsp and createCustomer.jsp view files. Let's take a look at the jsp files next&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 6: Create the view files &lt;/b&gt;&lt;br /&gt;The view files are simple jsp files, which render the model that is passed on to them.&lt;br /&gt;The list.jsp file iterates through the 'customers' list passed to it and formats them into a table. A snippet below:&lt;br /&gt;&lt;blockquote&gt;&amp;lt;table border="1"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;th&amp;gt;Phone&amp;lt;/th&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;c:forEach items="${customers}" var="customer"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;${customer.firstName} ${customer.lastName }&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;${customer.emailAddress}&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;${customer.phoneNumber}&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/c:forEach&amp;gt;&lt;br /&gt;&amp;lt;/table&amp;gt;&lt;/blockquote&gt;And the createCustomer.jsp file has a form and a submit button.&lt;br /&gt;&lt;blockquote&gt;&amp;lt;form:form method="POST" commandName="customer"&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;table&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;First Name :&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;form:input path="firstName" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;Last Name :&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;form:input path="lastName" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;Gender :&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;form:radiobutton path="gender" value="male" label="M" /&amp;gt; &amp;lt;form:radiobutton&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; path="gender" value="female" label="F" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;Email Address :&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;form:input path="emailAddress" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;Phone Number :&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;form:input path="phoneNumber" /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;td colspan="2"&amp;gt;&amp;lt;input type="submit"&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/table&amp;gt;&lt;br /&gt;&amp;lt;/form:form&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;We're now finally ready to run our application!&lt;br /&gt;&lt;br /&gt;&lt;b&gt;STEP 7: Run the web-application&lt;/b&gt;&lt;br /&gt;Right click your project and run the application on the tc server. Browse to http://localhost:8080/CMSApp/customer/create and you should see :&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TUjoMn6ePqI/AAAAAAAAAjk/qHWwik5tuko/s1600/create-customer.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_558g1ZlipEY/TUjoMn6ePqI/AAAAAAAAAjk/qHWwik5tuko/s1600/create-customer.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Enter the details and press the submit button and you would see a list of existing customers&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TUjoMG5WsbI/AAAAAAAAAjg/D0cXoOrOawM/s1600/list-customer.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="207" src="http://2.bp.blogspot.com/_558g1ZlipEY/TUjoMG5WsbI/AAAAAAAAAjg/D0cXoOrOawM/s320/list-customer.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;There you go! You have your first Spring MVC application up and running.&lt;br /&gt;&lt;br /&gt;This is a very basic application. Spring provides a lot of capabilities that you can explore. You can add validations to your fields, use themes, add locale support; the list is long.&lt;br /&gt;&lt;br /&gt;Hope you find this article useful.&lt;br /&gt;Happy coding!&lt;br /&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/spring-mvc/SpringMVC_CMSApp.rar?attredirects=0&amp;amp;d=1"&gt;Download the sample application : CMSApp.rar&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-1565285920773601910?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/1565285920773601910/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/02/get-started-with-spring-mvc.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/1565285920773601910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/1565285920773601910'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/02/get-started-with-spring-mvc.html' title='Get started with Spring MVC'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_558g1ZlipEY/TUeszmY2_7I/AAAAAAAAAjA/kSDr3F3Rjz8/s72-c/SpringMVC.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-7308670761469050406</id><published>2011-01-24T03:29:00.000-08:00</published><updated>2011-02-01T22:54:06.674-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Build your first grails application</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Grails is an open source web application framework, that has been built with the aim to enable rapid application development (RAD). Grails uses groovy as it's primary programming language. It uses Convention over Configuration to speed up the process of web application development. Grails takes its inspiration from Ruby on Rails but for the java world. Grails runs on the JVM and brings the power of the scripting world since it uses groovy which is a dynamic language built on top of java. Read more about grails and it's features &lt;a href="http://java-unplugged.blogspot.com/2011/01/welcome-grails-to-your-life.html" style="color: blue;"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;In this article we will quickly build an application using Grails. You will be surprised to see that you have your web-app running within 15 minutes, save data to the database, even deploy you application on your favourite application server! Enough talking, let's get into action.&lt;br /&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;( find a download link for the source code of the sample plugin at the bottom of the page)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #a64d79;"&gt;&lt;u&gt;&lt;b&gt;System Requirements&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;br /&gt;1. JDK 1.5 or above (for Grails 1.2 and above)&lt;br /&gt;2. JAVA_HOME enviroment variable should be set, pointing to the correct jdk installation.&lt;br /&gt;3. &lt;a href="http://grails.org/Download" style="color: blue;"&gt;download&lt;/a&gt; and unzip the grails zip file into a folder of your choice.&lt;br /&gt;4. create an environment variable GRAILS_HOME which points to the folder created in the step above.&lt;br /&gt;5. Add GRAILS_HOME/bin and JAVA_HOME/bin to the PATH environment variable.&lt;br /&gt;&lt;br /&gt;&lt;div style="color: #a64d79;"&gt;&lt;u&gt;&lt;b&gt;STEP 1: Create your web-application:&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;br /&gt;1. Open a command prompt at the location where you want to create your application.&lt;br /&gt;2. Type the following command to create an application named 'estore'&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;grails create-app estore &lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TTxY8C2WpXI/AAAAAAAAAiA/U44lswn8A3s/s1600/create-app.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/_558g1ZlipEY/TTxY8C2WpXI/AAAAAAAAAiA/U44lswn8A3s/s400/create-app.jpg" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;Change directory to your application:&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;cd estore&lt;/blockquote&gt;We can see that the script creates the basic structure of the web-app, reducing the time and effort of the developers. Grails also downloads some plugins such as hibernate, tomcat etc. In case you face any issues you probably need to check your internet connection or setup you proxy.&lt;br /&gt;&lt;br /&gt;Also, notice the environment (highlighted in the screenshot). The environment is set to 'development' mode.&lt;br /&gt;Grails lets you configure your application for different environments, 'development' is the default environment. We will discuss these enviromnents in a short while.&lt;br /&gt;&lt;br /&gt;The create-app command will create the following structure for you:&lt;br /&gt;&lt;br /&gt;&lt;table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; margin-left: 1em; text-align: right;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_558g1ZlipEY/TTxl8TxF4aI/AAAAAAAAAiE/Q7aeNDri1po/s1600/folder-structure.jpg" imageanchor="1" style="clear: right; margin-bottom: 1em; margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_558g1ZlipEY/TTxl8TxF4aI/AAAAAAAAAiE/Q7aeNDri1po/s1600/folder-structure.jpg" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="color: purple; text-align: center;"&gt;Grails application structure&lt;/td&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;As you see, grails has already done a lot of work for you! In fact, you already have a web application that you can try launching using the command 'grails app'. But as it is right now, the web-application would not be of much use.&lt;br /&gt;Let's take a closer look at what grails has created for us. The folder with which you are going to work the most is 'grails-app'. Inside this folder, grails has created for us folders where we place our model (M-domain), views (V) and controllers. In addition to these, there's a folder for the service classes, taglibs, message property files (i18n) and configuration files. &lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Domain&lt;/b&gt;&lt;/u&gt;: This is where you'll place your domain classes (i.e. the models).&lt;br /&gt;These are POJOs with some additional attributes such as &lt;span style="color: blue;"&gt;'mapping'&lt;/span&gt; which specifies the relationship the class holds with other domain objects, &lt;span style="color: blue;"&gt;'constraints'&lt;/span&gt; which specifies the constraints on the fields of the domain objects, etc. You don't need to provide getters and setters for all the fields. &lt;br /&gt;&lt;u&gt;&lt;b&gt;Controller&lt;/b&gt;&lt;/u&gt;&lt;b&gt;: &lt;/b&gt;This is where your controller classes will lie.Controllers handle requests and generate the response either themselves or delegate the request to a view. Every controller class name must end with 'Controller'. You can easily create a controller by creating a class who's name ends with 'Controller'. Or you could use the commands provided by grails which we will see in a moment.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Services&lt;/b&gt;&lt;/u&gt;: This is where you should place business logic. Every service class ends with 'Service'. You can create a service by simply creating a class who's name ends with 'Service' inside the service folder or you can use the commands provided by grails. To use this service in a controller or domain class you simply need to declare a variable named the same as your service, and the service will be automatically injected by spring at application startup. These services are by default singleton, i.e. the same instance of the service is used across the application. However, the scope of the service can be changed&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Views&lt;/b&gt;&lt;/u&gt;: Views in grails are .gsp files which are very similar to jsp files. Controllers use 'convention mechanism' to delegate the request to the appropriate view. Grails uses SiteMesh for its layouts. Thus you can very easily specify the basic layout of your application in the grails-app/views/layout/main.gsp. You will see that you already have the main.gsp file generated for you when you create a fresh new application.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Taglibs&lt;/b&gt;&lt;/u&gt;: This is where you can add your  custom tags but without the need to update any TLDs or any other  configuration. Remember 'convention over configuration'. You can create a  taglib by simply creating a class ending with 'TagLib' in the taglib  folder or use the grails command which we will discuss just in a while.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Configuration Files&lt;/b&gt;&lt;/u&gt;: Although grails follows the 'Convention over configuration' paradigm, you need some basic configuration for your databases, working directory, logging, emails etc. All your configuration files would be placed inside 'conf'. There are a couple of configuration files but for now lets take a closer look at the important configuration files.&lt;br /&gt;&lt;br /&gt;1. DataSource.groovy: As discussed earlier, grails allows you to configure your application for different environments using just a single file. Thus you don't need to maintain multiple copies of your property file per environment. You can place all your configuration inside one file, and you could even provide some common properties that remain unchanged across environments. Grails provides you with an in-memory HSQL database so you can quickly get started with your application and leave the task of choosing, installing and setting up a database for later. You can change the HSQL configuration to save data to a file, so you can see your test data in between application runs.&lt;br /&gt;change the url to:&lt;br /&gt;&lt;blockquote class="" style="text-align: left;"&gt;&amp;nbsp;url = "jdbc:hsqldb:file:testDb"&lt;/blockquote&gt;/* NOTE: For this tutorial, i have changed my applications url to the above, so that i don't have to re-generate my test data.&amp;nbsp; */&lt;br /&gt;&lt;br /&gt;&amp;nbsp;2. Config.groovy: This is the config file where you can specify any application specific properties, environment specific log4j properties etc. The properties specified in this file can be read from any application code using ConfigurationHolder.&lt;br /&gt;For example, the following property:&lt;br /&gt;&amp;nbsp;email.notification=test@test.com&lt;br /&gt;can be read using the following code:&lt;br /&gt;&lt;blockquote class="" style="text-align: left;"&gt;&amp;nbsp; import org.codehaus.groovy.grails.commons.ConfigurationHolder&lt;br /&gt;&amp;nbsp; ....&lt;br /&gt;&amp;nbsp; ....&lt;br /&gt;&amp;nbsp;sendNotification(&lt;span style="color: blue;"&gt;ConfigurationHolder.config&lt;/span&gt;.email.notification, 'some test email')&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;3. BootStrap.groovy: This file has two methods, init &amp;amp; destroy, which are invoked on application startup and shutdown. So if you need to run some initialization code at startup or some clean up tasks at shutdown, this file is you will need.&lt;br /&gt;Now that you are familiar with the structure of your grails application, let's move ahead ...&lt;br /&gt;&lt;br /&gt;&lt;u style="color: #a64d79;"&gt;&lt;b&gt;STEP 2: Create your first domain class&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;Let's create a domain class &lt;b&gt;Product&lt;/b&gt; for our estore:&lt;br /&gt;&lt;blockquote&gt;grails create-domain-class product&lt;/blockquote&gt;/*( NOTE: For the purpose of this tutorial, i am using the default package, i.e. i have removed any package structure, i.e. removed the 'package estore' line from the file and placed the Product.groovy file at grails-app/domain directly.)*/&lt;br /&gt;&lt;br /&gt;Let's add some fields to the Product class so that it looks like this:&lt;br /&gt;&lt;blockquote&gt;package com.estore&lt;br /&gt;&lt;br /&gt;class Product {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;String name&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;String description&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Double price&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static constraints = {&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;description(nullable: true)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;name(blank: false)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;price(min:0D)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;As you can see here, we have added a few fields to the Product class and in addition also added some constraints. By default, all fields are Non-nullable. So here, we make description a nullable field, and also add a constraint to the minimum value of the price field.&lt;br /&gt;&lt;br /&gt;&lt;b style="color: #a64d79;"&gt;&lt;u&gt;STEP 3: Create a controller and views for the domain class&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;We would need some pages in our application to add/update/delete/list all the products in our store. There are two ways we can do this&lt;br /&gt;&lt;b&gt;1. Create a controller: &lt;/b&gt;Generate the controller and use scaffolding to generate the views at run-time.&lt;br /&gt;Run the command:&lt;br /&gt;&lt;blockquote&gt;grails create-controller com.estore.Product &lt;/blockquote&gt;Now open ProductController.groovy, and add the following code to it:&lt;br /&gt;&lt;blockquote&gt;package com.estore&lt;br /&gt;&lt;br /&gt;class ProductController {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def scaffold = Product&lt;br /&gt;}&lt;/blockquote&gt;Here, we are using the 'scaffolding' mechanism, where grails generates the views at run-time.&amp;nbsp; At this point, you are ready to run the application and test it using 'grails run-app'. But, i would suggest you first read the second way to do this before you try this one.&lt;br /&gt;&lt;div style="text-align: center;"&gt;OR &lt;/div&gt;&lt;b&gt;2. Generate controller and views: &lt;/b&gt;Generate both the controller and the views on your disk.&lt;br /&gt;Run the command:&lt;br /&gt;&lt;blockquote&gt;grails generate-all com.estore.Product&lt;/blockquote&gt;Here, you will find that 4 files have been generated at grails-app\views\product namely create.gsp, list.gsp, edit.gsp and show.gsp. And the controller will have multiple 'actions'. These actions will be called depending on the URI that is requested.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Run the application using the command:&lt;br /&gt;&lt;blockquote&gt;grails run-app&lt;/blockquote&gt;Browse to : http://localhost:8080/&lt;b style="color: purple;"&gt;estore&lt;/b&gt;/&lt;b&gt;&lt;span style="color: #cc0000;"&gt;product&lt;/span&gt;&lt;/b&gt;/&lt;b&gt;&lt;span style="color: #38761d;"&gt;create&lt;/span&gt;&lt;/b&gt; &lt;br /&gt;(&lt;br /&gt;Notice the URI:&lt;br /&gt;&amp;nbsp;&amp;nbsp; estore- &lt;b&gt;&lt;span style="color: purple;"&gt;application &lt;/span&gt;&lt;/b&gt;name&lt;br /&gt;&amp;nbsp;&amp;nbsp; product- &lt;b&gt;&lt;span style="color: red;"&gt;controller &lt;/span&gt;&lt;/b&gt;name&lt;br /&gt;&amp;nbsp; create- &lt;b&gt;&lt;span style="color: #38761d;"&gt;action &lt;/span&gt;&lt;/b&gt;name&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;You should be able to see something like this:&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TT1Pd4kEeiI/AAAAAAAAAiQ/JcTLx6WMncA/s1600/run-app.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_558g1ZlipEY/TT1Pd4kEeiI/AAAAAAAAAiQ/JcTLx6WMncA/s1600/run-app.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;[ Try this: Try creating a product with a negative price value or a blank aname. Without writing a single line of code, you see your domain-class constraints being validated on the UI! That's grails for you :) ]&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TT1Kz3ZN-hI/AAAAAAAAAiI/l4089-deU78/s1600/service.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b style="color: #a64d79;"&gt;&lt;u&gt;STEP 4: Adding relationships to your domain class&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Let's enhance our store, and add categories to it. Products can belong to a certain category. And a category can have multiple products. So here we have a many-to-one relationship.&lt;br /&gt;Create a new domain class called Category:&lt;br /&gt;&lt;blockquote&gt;grails create-domain-class com.estore.Category &lt;/blockquote&gt;Add the following code to Category.groovy&lt;br /&gt;&lt;blockquote&gt;package com.estore&lt;br /&gt;&lt;br /&gt;class Category {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String name&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; String description&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static hasMany = [products: Product]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static constraints = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; name(blank:false)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; description(nullable:true)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/blockquote&gt;And add the following line to Product.groovy&lt;br /&gt;&lt;blockquote&gt;Category category&lt;br /&gt;static belongsTo = Category &lt;/blockquote&gt;Note: If you don't add this line to Product.groovy, the deletes would not be cascaded. We will not be getting into the details of how GORM works since it's out of the scope of this article. &lt;br /&gt;&lt;br /&gt;Now create the views and controller for the Category domain class&lt;br /&gt;&lt;blockquote&gt;grails generate-all com.estore.Category&lt;/blockquote&gt;Repeat the above command for com.estore.Product as well, since we've added a new field to the class, we need this to be reflected in the view.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Run your app, you should see there are two controllers, and your product/create page looks like this:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TT1d2nTsSRI/AAAAAAAAAiU/blGiXGqvdfk/s1600/product_list.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_558g1ZlipEY/TT1d2nTsSRI/AAAAAAAAAiU/blGiXGqvdfk/s1600/product_list.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;For better presentation, make a tiny change to grails-app/views/product/create.gsp ( and editp.gsp)&lt;br /&gt;Change&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;lt;g:select name="category.id" from="${com.estore.Category.list()}" optionKey="id" value="${productInstance?.category?.id}"&amp;nbsp; /&amp;gt;&lt;br /&gt;&lt;br /&gt;to &lt;br /&gt;&lt;br /&gt;&amp;lt;g:select name="category.id" from="${com.estore.Category.list()}" optionKey="id" &lt;b&gt;&lt;span style="color: blue;"&gt;optionValue="name"&lt;/span&gt;&lt;/b&gt; value="${productInstance?.category?.id}"&amp;nbsp; /&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TT1Kz3ZN-hI/AAAAAAAAAiI/l4089-deU78/s1600/service.jpg" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;&lt;b style="color: #a64d79;"&gt;&lt;u&gt;STEP 5: Create a service class&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;b style="color: #a64d79;"&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;div style="color: black;"&gt;&lt;span style="color: #a64d79;"&gt;&lt;span style="color: black;"&gt;You can create a service class by either creating a class ending with 'Service' in the services folder or&amp;nbsp; use the command:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote&gt;&lt;div style="color: black;"&gt;&lt;span style="color: #a64d79;"&gt;&lt;span style="color: black;"&gt;&lt;span style="color: blue;"&gt;grails create-service utility&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/blockquote&gt;To use this service in CategoryController.groovy just add this line to the file:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;class CategoryController {&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def utilityService&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....&lt;/blockquote&gt;&amp;nbsp;That's all you need to be able to use a service. You should add all your business logic to the service class and call it from the controller and domain classes.&lt;br /&gt;&lt;br /&gt;So, that didn't take long now, did it ? ;) &lt;br /&gt;&lt;br /&gt;Hope you find this article useful. Happy coding!&lt;br /&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/first_grails_app/estore.rar?attredirects=0&amp;amp;d=1"&gt;Download source code (estore.rar)&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-7308670761469050406?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/7308670761469050406/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/01/create-your-first-grails-application.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/7308670761469050406'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/7308670761469050406'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/01/create-your-first-grails-application.html' title='Build your first grails application'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_558g1ZlipEY/TTxY8C2WpXI/AAAAAAAAAiA/U44lswn8A3s/s72-c/create-app.jpg' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-6648907070294701206</id><published>2011-01-20T03:13:00.000-08:00</published><updated>2011-02-01T22:54:25.986-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Welcome Grails to your life!</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;We all know how painful web development in java can be! Maintaing endless configuration xml files, writing hibernate layers &amp;amp; complex build scripts for compiling &amp;amp; packaging can make your experience of developing a web app in java nightmarish. But we would not want to sacrifice the capabilities and power of the Java platform for the sake of ease and agility. &lt;br /&gt;&lt;br /&gt;And this is where web frameworks like Grails fit in. These framework take care of the overhead tasks such as configuration, builds, integration, and enable us to concentrate on our business requirement.&amp;nbsp; &lt;br /&gt;If you are looking for a framework that supports agile development and enables RAD (Rapid application Development), Grails could be your first choice!&lt;br /&gt;&lt;br /&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Grails is an open source web framework that uses &lt;b&gt;Groovy &lt;/b&gt;as it's primary programming language. Grails was inspired by Ruby on Rails, but mainly for java developers. This does not mean, you should not try this framework if you're not from a java background, but Java developers would find themselves productive from day 1.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Features of Grails that enable RAD are:&lt;/b&gt;&lt;br /&gt;&lt;ol start="1" type="1"&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;b style="color: blue;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Convention over Configuration&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;: You don't have to maintain huge complex configuration files to      setup your application. Grails uses conventions to determine the role of      classes instead the application and how they interact with the each      other.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;b style="color: blue;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Groovy&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;: Groovy is a agile      dynamic scripting language built on top of the Java language. Groovy code is      very similar and much terser than Java, which means almost-zero learning      curve, and lesser code to write and maintain. Groovy is a dynamic      language, uses meta-programming. And it brings the power of scripting      languages to the java world.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Grails doesn't suggest you to use &lt;b style="color: blue;"&gt;MVC&lt;/b&gt;, to forces you to use      MVC (which is great, btw!). But using the MVC pattern in Grails is much      simpler than in any other web framework. There is absolutely NO need for      any configuration. You don’t have to specify in any configuration files      which class is a model or a controller, or specify which view file will be      used for a certain action. Grails determines all the information by the      virtue of the class names and the location of the files.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;b style="color: blue;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Code Generation &amp;amp; synthesis&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;: Grails generates considerable amount of code making the process      faster and easier for the developer. It not only generates code, but also      allows code synthesis. In case of code generation, some bits of code is      actually generated on your disk. But in case of code synthesis, the      code/feature is available dynamically at runtime. Grails makes use of Groovy's      meta-programming capabilities to enable code synthesis. One of the ways in      which Grails does this is GORM( Grails Object Relational Mapping) . &lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="MsoNormal" style="margin-left: 36pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;If you have a model (M of MVC) class say Author that has fields ‘firstName’, ‘lastName’, grails will add dynamic finder methods that you can use. Eg:-&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 36pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Author&lt;/span&gt;&lt;span lang="EN-US" style="font-family: Arial; font-size: 10pt;"&gt;.findByFirstName("Michael")&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 36pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Author&lt;/span&gt;&lt;span lang="EN-US" style="font-family: Arial; font-size: 10pt;"&gt;.findByLastNameLike("M%"), &lt;/span&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 36pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Author&lt;/span&gt;&lt;span lang="EN-US" style="font-family: Arial; font-size: 10pt;"&gt;.findByFirstNameIlikeAndLastName("m%", “Davis”) &lt;/span&gt;&lt;/div&gt;&lt;ol start="5" type="1"&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;b style="color: blue;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;DRY&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt; (Don't repeat      yourself):&amp;nbsp; Enables you to reuse code as much as possible. It also      enables you to reuse java code.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Grails makes use of technologies such as &lt;b&gt;&lt;span style="color: blue;"&gt;Hibernate, Spring,      JDBC, Sitemesh, Ajax&lt;/span&gt;.&lt;/b&gt; These compoments are already accepted and test      technologies, which means strong support and faster development.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&lt;span style="color: blue;"&gt;GORM&lt;/span&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;(Grails Object Relational Mapping) is Grail’s ORM implementation.      But as in case of hibernate, you don’t need to write any hbm configuration      files (although you can if you want to) you can also use java for writing      your domain classes. GORM provides many dynamic methods that come in very      handy and also nearly eliminates the need to know the details of how      hibernate works (although I personally believe, it makes a world of a      difference if you do), easy way to add validations to your domain object,      clear and precise way of specifying the relationship between your domain      objects.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Comes with a &lt;b&gt;&lt;span style="color: blue;"&gt;built-in web server&lt;/span&gt; &lt;/b&gt;which leads to higher      productivity during the development phase.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;It has an in-memory database. You can push the choice of a database      for your application to as late in the product life cycle as you wish and      continue development and testing using the in-memory db.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Building&lt;/span&gt;&lt;b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt; &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;the web-application is      as easy as a single command at the command prompt!&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;It has an &lt;b style="color: blue;"&gt;extensible plugin system&lt;/b&gt; which has lead to strong      community support and acceptance. Again, this enables code reuse. There is      a sea of plugins to choose from eg. Logging, security, Yahoo UI toolkit      etc.&lt;/span&gt;  &lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;&amp;nbsp;Good unit, integration and functional &lt;b style="color: blue;"&gt;testing support&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal" style="margin-bottom: 6pt;"&gt;&lt;span style="font-family: Arial; font-size: 10pt;"&gt;Most suited for &lt;b style="color: blue;"&gt;CRUD &lt;/b&gt;(create-retrieve-update-delete) type of      applications.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;I was personally surprised to see the speed at which I was able to come up with a stable application using Grails. If you are using the agile development methodology, you will be able to appreciate the benefits of using Grails. Grails as a technology brings the 'agile methodology' to web-application development. You will be able to reliably and efficiently change an entire functionality or feature in a matter of 2 to 3 days, that you may otherwise take a week in plain java. This enables you to seek&amp;nbsp; feedback from your customers, quickly implement the changes and get back to them to repeat the cycle. This will lead to increased customer satisfaction &amp;amp; better clarity.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Although the framework does not guarantee a stable and robust application, but surely leaves you enough time and resources to ensure that you can strive for one. So the next time you have to develop an application with stringent time lines, you know what to try ;) &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span lang="EN-US"&gt;Having said that, you have to &lt;a href="http://java-unplugged.blogspot.com/2011/01/get-started-with-grails.html" style="color: #666666;"&gt;&lt;b&gt;TRY&lt;/b&gt;&lt;/a&gt; it, to believe it.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-6648907070294701206?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/6648907070294701206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/01/welcome-grails-to-your-life.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6648907070294701206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/6648907070294701206'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/01/welcome-grails-to-your-life.html' title='Welcome Grails to your life!'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6262460674252256073.post-8738492095334325202</id><published>2011-01-17T02:25:00.000-08:00</published><updated>2011-02-01T22:54:38.406-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Grails'/><title type='text'>Create your first grails plugin</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;Crucial to the success of grails, is its support for plugins. Grails was designed keeping pluggability in mind right from the start. Ease in development, testing, building and distribution of plugins has led to a larger community interest in the framework, with loads of plugins available for developers to choose from. &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;If you’ve worked with grails, chances are you’ve used at least one grails plugin such as hibernate, logging, acegi, grails UI, mail, searchable etc. Grails maintains a plugin repository that you can search to find a plugin most suitable for your requirements. But have you ever found yourself in a situation where you’re rewriting the same code in multiple views, or duplicating code between controllers and services and even between multiple grails applications? If yes, this could be your chance of trying your hand at a plugin! &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;I’m presenting a very basic tutorial to create a very simple grails plugin, with a service and a taglib that can be reused across grails applications.( find a download link for the source code of the sample plugin at the bottom of the page)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;This sample is tested using the following specifications:&lt;/span&gt;&lt;/div&gt;&lt;ol start="1" style="font-family: Arial,Helvetica,sans-serif; margin-top: 0cm;" type="1"&gt;&lt;li class="MsoNormal"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;grails version: &lt;b&gt;grails-1.3.4&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="MsoNormal"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;JDK version: &lt;b&gt;jdk1.6&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif; margin-left: 18pt;"&gt;&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;Let’s get started.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif;"&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif; margin-left: 18pt; text-indent: -18pt;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;1. Choose a folder for your plugin source code.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="font-family: Arial,Helvetica,sans-serif; margin-left: 18pt; text-indent: -18pt;"&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;2. To create a plugin named ‘customplugin’, use the following command at the command prompt.&lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;grails create-plugin customplugin&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="font-size: small;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;This would create a structure such as this:&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://3.bp.blogspot.com/_558g1ZlipEY/TTP5RBAUxuI/AAAAAAAAAhg/Q_C5-VTmt_M/s1600/plugin_structure.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/_558g1ZlipEY/TTP5RBAUxuI/AAAAAAAAAhg/Q_C5-VTmt_M/s1600/plugin_structure.JPG" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;The plugin structure is almost exactly the same as a normal grails application. Thus you can add controllers, services, views, taglibs to your plugin just as you'd add them to a grails application.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;You will find an additional file CustompluginGrailsPlugin.groovy at the root folder. This is the file that contains all the information or metadata for your plugin.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TTQLe1K1F-I/AAAAAAAAAh0/yzz2gfFtzfE/s1600/plugin_metadata.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_558g1ZlipEY/TTQLe1K1F-I/AAAAAAAAAh0/yzz2gfFtzfE/s1600/plugin_metadata.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;You can specify the version number, grails version that your plugin will work with, auther name, etc. Your plugin could depend on other plugins, for instance, you could write a plugin that works with database events which might need the hibernate plugin, such dependencies can be specified in this file. There are also several hooks (events) that are provided, which you can use to perform certain actions or load classes. For eg, if your plugin wants to make some changes/additions to the web.xml, you could plug that code in the doWithWebDescriptor closure provided. For the purpose of this tutorial, we are going to stick to the basics; we won't be using any of these hooks ( accounts for another article.. goes into my diary..)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;3. Now let's create a service in the plugin. Use the following commands&lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; cd customplugin&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; grails create-service conversion&lt;/span&gt;&lt;span lang="EN-US" style="font-size: small;"&gt;&lt;span style="font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&lt;span style="font-size: small;"&gt;4.&amp;nbsp; Add the following code to the conversionService. This service has two functions for converting value in celcius into fahrenheit, an&lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class ConversionService {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static transactional = false&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def convertCelciusToFahrenheit = { tempInCelcius -&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def tempInFah = ((9.0/5.0) * tempInCelcius )+ 32&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return tempInFah&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def convertFahrenheitToCelcius = { tempInFah -&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def tempInCelcius = (tempInFah - 32) * (5.0/9.0)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return tempInCelcius&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;}&lt;/span&gt;&lt;/blockquote&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;span style="font-size: small;"&gt;&lt;a href="http://1.bp.blogspot.com/_558g1ZlipEY/TTP7_r1T9FI/AAAAAAAAAhk/Tc0AJdTx8lc/s1600/service.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;5. Let's also add a tag library to the plugin. This library can be used to display navigation bread crumbs at the top of all the pages in the application.&lt;/span&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails create-tag-lib BreadCrumb&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;&amp;nbsp;Add the following code to the file BreadCrumbTagLib:&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;class BreadCrumbTagLib {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;static namespace="brcr"&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def breadCrumb = {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; "&amp;lt;a href='${request.getContextPath()}'&amp;gt;Home&amp;lt;/a&amp;gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(controllerName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; " -&amp;gt; &amp;lt;a href='${request.getContextPath()}&lt;/span&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;/${controllerName}'&amp;gt;&lt;/span&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt; &amp;nbsp;&amp;nbsp; \\&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ${controllerName}&amp;lt;/a&amp;gt;"&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(actionName)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; " -&amp;gt; ${actionName} "&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;/span&gt;&lt;/blockquote&gt;&amp;nbsp;Notice the namespace in the above code. If you do not provide any namespace, you should be able to use 'breadCrumb' using &amp;lt;g:breadCrumb&amp;gt;. But if you want to change that to something specific, you can provide a namespace. For this example, we would need to use:&lt;br /&gt;&amp;nbsp; &amp;lt;brcr: breadCrumb /&amp;gt;&lt;br /&gt;&lt;br /&gt;6. We've added enough code to our plugin, let's package it now, use this command at the root folder of the plugin code:&lt;br /&gt;&lt;blockquote style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails package-plugin&lt;/blockquote&gt;&amp;nbsp;You will see a file named grails-customplugin.zip at the root folder &lt;br /&gt;7. To test if we're able to use the plugin correctly, let's now create a small grails application :&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails create-app myapp &lt;/span&gt;&lt;/blockquote&gt;&lt;br /&gt;8. To install the plugin created in step 6:&lt;br /&gt;&lt;blockquote&gt;&lt;div style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; cd myapp&lt;/div&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;grails install-plugin &amp;lt;path_to_the_plugin&amp;gt;\grails-customplugin.zip &lt;/span&gt;&lt;/blockquote&gt;9.&amp;nbsp; We will now create a simple controller to test the conversionService from the controller. We can simply consume the service by declaring a conversionService variable in the controller just as we do in a usual grails application. To create the controller:&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; grails crete-controller convert&lt;/span&gt;&lt;/blockquote&gt;&amp;nbsp; add the following code to the controller:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; class ConvertController {&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def &lt;b&gt;conversionService&lt;/b&gt;&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def index = {&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; redirect(action:'convert')&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def convert = {&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; def result&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(params.degrees)&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result = convertionService.convertCelciusToFahrenhiet(params.degrees.toInteger())&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; params.result= result&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [result: result, degrees: params.degrees]&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;" /&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;10. Create the convert.gsp file in myapp/grails-app/views/convert folder which will take an input from the user in celcius, pass it on to the controller, which will call the service from the plugin and convert the value into fahrenheit and will send back the result to the view. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; convert.gsp&lt;br /&gt;&lt;blockquote&gt;&amp;nbsp;&lt;span style="font-size: small;"&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Times,&amp;quot;Times New Roman&amp;quot;,serif; font-size: small;"&gt;&amp;lt;g:form action="convert" method="post" controller="convert"&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;Celcius : &amp;lt;input type="text"&amp;nbsp; class="text" size="60" id="degrees" name="degrees" value="${degrees}"/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;span class="button"&amp;gt;&amp;lt;g:submitButton name="convert" class="save" value="Convert" /&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div&amp;gt;&lt;br /&gt;&amp;lt;g:if test="${result}"&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;&amp;nbsp;${degrees} celcius is equal to ${result} fahrenheit&lt;br /&gt;&amp;lt;/g:if&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/g:form&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/blockquote&gt;To use the taglib, add &amp;lt;brcr:breadCrumb&amp;gt; to the layout file 'main.gsp' just before &amp;lt;g:layoutBody&amp;gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;11. Run the application using &lt;span style="color: blue;"&gt;grails run-app&lt;/span&gt; and browse to &lt;span style="color: blue;"&gt;http://localhost:8080/myapp/convert&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_558g1ZlipEY/TTQGznBGfuI/AAAAAAAAAhw/LiCNgknq-pI/s1600/gsp_page.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/_558g1ZlipEY/TTQGznBGfuI/AAAAAAAAAhw/LiCNgknq-pI/s1600/gsp_page.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&amp;nbsp; &lt;br /&gt;&lt;br /&gt;There you go! you're first grails plugin in action&lt;br /&gt;&lt;br /&gt;This is a very simple example for getting started with a basic skeleton for a plugin.&lt;br /&gt;Hope you find this article helpful&lt;br /&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/create_grails_plugin/customplugin.rar?attredirects=0&amp;amp;d=1"&gt;Download plugin source code (customplugin.rar)&lt;/a&gt;&lt;br /&gt;&lt;a href="https://sites.google.com/site/javaunplugged/create_grails_plugin/myapp.rar?attredirects=0&amp;amp;d=1"&gt;Download application source code (myapp.rar)&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6262460674252256073-8738492095334325202?l=java-diaries.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-diaries.blogspot.com/feeds/8738492095334325202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-diaries.blogspot.com/2011/01/create-your-first-grails-plugin.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/8738492095334325202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6262460674252256073/posts/default/8738492095334325202'/><link rel='alternate' type='text/html' href='http://java-diaries.blogspot.com/2011/01/create-your-first-grails-plugin.html' title='Create your first grails plugin'/><author><name>java-diaries</name><uri>http://www.blogger.com/profile/10390649769639548799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_558g1ZlipEY/TTP5RBAUxuI/AAAAAAAAAhg/Q_C5-VTmt_M/s72-c/plugin_structure.JPG' height='72' width='72'/><thr:total>1</thr:total></entry></feed>
