<?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-7982602471247472304</id><updated>2011-04-21T20:15:03.221-07:00</updated><category term='jboss'/><category term='javarebel'/><category term='eclipse'/><category term='spring'/><category term='tutorial'/><title type='text'>javadrips</title><subtitle type='html'>random things about java and programming in general</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javadrips.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7982602471247472304/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://javadrips.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>javadrips</name><uri>http://www.blogger.com/profile/15602953853680745867</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://2.bp.blogspot.com/_Qe5NUfA2r5c/SQB7FkjZRjI/AAAAAAAAAAM/68hjlzhrJbc/S220/me_aventura.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7982602471247472304.post-7167357404187327223</id><published>2008-11-28T14:01:00.000-08:00</published><updated>2008-11-30T08:25:15.921-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='tutorial'/><title type='text'>Tutorial-like spring application with excel support. Step 1 - hello world</title><content type='html'>&lt;p&gt;Recently our marketing decided that they urgently  need an application which will retrieve certain statistics from our database and return them as excel files. As long as our main application historicly does not really use any web framework (thus evolving into a framework on its own), I've decided that it is a really good time to continue introduction of &lt;a href="http://www.springframework.org/"&gt;Spring framework&lt;/a&gt; to our codebase. Spring framework is quite clean and useful &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;dependency injection&lt;/a&gt; framework which also has a lot of stuff (like &lt;a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html"&gt;JdbcTemplate&lt;/a&gt; class and whole data access package) which really make a life of a developer easier. Dependency injection here basicly means that we can write special configuration file which allow us to create instances of classes we need with all the necessary parameters set via set methodsor constructor parameters - really simple and cool. &lt;/p&gt;&lt;p&gt;So the two main things which made this application not absolutely trivial were 1) the application should be integrated with JNDI DataSource deployed in the JBoss application server 2)  it should return statistics as excel file. But before starting doing that we need to create a trivial Spring application which will be a base for further development. To start working with Spring in a web application we should add necessary spring libraries (in our case they are - spring-core.jar, spring-web.jar, spring-webmvc.jar and spring.jar) and add the following code to the web.xml configuration file:&lt;/p&gt;&lt;pre class="xml" name="code"&gt;
&amp;lt;listener&amp;gt;
   &amp;lt;listener-class&amp;gt;
  org.springframework.web.context.ContextLoaderListener
&amp;lt;/listener-class&amp;gt;
 &amp;lt;/listener&amp;gt;
 
 &amp;lt;servlet&amp;gt;
   &amp;lt;servlet-name&amp;gt;dispatch&amp;lt;/servlet-name&amp;gt;
   &amp;lt;servlet-class&amp;gt;
  org.springframework.web.servlet.DispatcherServlet
&amp;lt;/servlet-class&amp;gt;
   &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;
 &amp;lt;/servlet&amp;gt;
 
 &amp;lt;servlet-mapping&amp;gt;
   &amp;lt;servlet-name&amp;gt;dispatch&amp;lt;/servlet-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;/get/*&amp;lt;/url-pattern&amp;gt;
 &amp;lt;/servlet-mapping&amp;gt;
&lt;/pre&gt;

The whole concept behind that is quite trivial: we added a listener class which is being called when application is initialized, loads bean configuration from  configuration file (applicationContext.xml by default) and loads the [servletname]-servlet.xml configuration files for the servlets with [servletname] defined in web.xml. To make it work properly we also need to create an aplicationContext.xml file (which we will leave empty) and dispatch-servlet.xml file which configures the main servlet of the application:

&lt;pre class="xml" name="code"&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8" ?&amp;gt;
&amp;lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&amp;gt;

&amp;lt;beans&amp;gt;
&amp;lt;bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"&amp;gt;
  &amp;lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&amp;gt;
  &amp;lt;property name="prefix" value="/WEB-INF/jsp/"/&amp;gt;
  &amp;lt;property name="suffix" value=".jsp"/&amp;gt;
&amp;lt;/bean&amp;gt;
&amp;lt;bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&amp;gt;
  &amp;lt;property name="mappings"&amp;gt;
    &amp;lt;props&amp;gt;
      &amp;lt;prop key="/*"&amp;gt;dispatchController&amp;lt;/prop&amp;gt;
    &amp;lt;/props&amp;gt;
  &amp;lt;/property&amp;gt;
&amp;lt;/bean&amp;gt;
&amp;lt;bean id="dispatchController"
   class="com.goaiss.internal.stats.web.DispatchController"&amp;gt;
 &amp;lt;/bean&amp;gt;
&amp;lt;/beans&amp;gt;
&lt;/pre&gt;
&lt;p&gt;And again nothing really magical happens here, we define a view resolver - a class which responsible for locating a resulting view for a request - in the most simple case it is a jsp file. Than we tell application that all the urls coming to this servlet will be handled by a controller named dispatchController which we define in the last bean declaration. After doing that we just need to create a simple jsp file e.g. test.jsp, DispatchController class (yeah, that's true you need to occasionally write java code while dealing with Spring) which will be super tiny:&lt;/p&gt;&lt;p&gt;
&lt;pre class="java" name="code"&gt;
public class DispatchController extends MultiActionController {
   public ModelAndView actionTest(HttpServletRequest request,
                                   HttpServletResponse response)
                       throws Exception {
        return new ModelAndView("test");
   }
}
&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;and now we are ready to put a link to index.jsp file e.g. &amp;lt;a href="get/actionTest"&amp;gt;Hello world!&amp;lt;/a&amp;gt; and say "Hello world" in the most simple Spring application :) &lt;/p&gt;&lt;p&gt;Eclipse project for the this application can be found &lt;a href="http://javadrips.googlecode.com/files/step1.zip"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7982602471247472304-7167357404187327223?l=javadrips.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javadrips.blogspot.com/feeds/7167357404187327223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javadrips.blogspot.com/2008/11/tiny-tutorial-like-spring-application.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7982602471247472304/posts/default/7167357404187327223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7982602471247472304/posts/default/7167357404187327223'/><link rel='alternate' type='text/html' href='http://javadrips.blogspot.com/2008/11/tiny-tutorial-like-spring-application.html' title='Tutorial-like spring application with excel support. Step 1 - hello world'/><author><name>javadrips</name><uri>http://www.blogger.com/profile/15602953853680745867</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://2.bp.blogspot.com/_Qe5NUfA2r5c/SQB7FkjZRjI/AAAAAAAAAAM/68hjlzhrJbc/S220/me_aventura.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7982602471247472304.post-303051607708898773</id><published>2008-11-12T16:48:00.000-08:00</published><updated>2008-11-17T07:26:25.134-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='javarebel'/><category scheme='http://www.blogger.com/atom/ns#' term='jboss'/><title type='text'>setting up javarebel with eclipse and jboss</title><content type='html'>I like simple things and things which do not bother me and do not try to tell me how to do the things, that's why I'm using Linux and program in Java with Eclipse &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;IDE&lt;/span&gt;. And also I happen to be boring enough to like server side development. Sometimes it is a bit &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;embarrasing&lt;/span&gt; to answer &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;a question&lt;/span&gt; about my hobby, after &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;I already&lt;/span&gt; told that I do java &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_4"&gt;serverside&lt;/span&gt; for living. But anyway, there is a thing which annoys me that much, that I even thought about using script languages (&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;argh&lt;/span&gt;!) sometimes (unfortunately - they annoy me even more). Luckily there is a nice project called &lt;a href="http://www.zeroturnaround.com/javarebel/"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_6"&gt;JavaRebel&lt;/span&gt;&lt;/a&gt; (used for on-the-fly class reloading) which together with &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_7"&gt;JSP&lt;/span&gt; Weaver another project of the same company (which tries to do the same thing for &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_8"&gt;JSP&lt;/span&gt;) makes the situation better.

And as long as I tend to forget how to set the things up after I do it once - I will describe the whole process in a small tutorial.

&lt;p&gt;First I've created an empty workbench for eclipse, added &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_9"&gt;jboss&lt;/span&gt; server to the servers view and added a &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_10"&gt;petclinic&lt;/span&gt; project from Spring samples, so that default ant task will build an application, pack it to the war file and deploy it to my local &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_11"&gt;JBoss&lt;/span&gt; (that's how I usually configure the applications I work with), all in all - it looks like that:
&lt;/p&gt;&lt;a href="http://4.bp.blogspot.com/_Qe5NUfA2r5c/SRuD4XsNcZI/AAAAAAAAABQ/8df8nYLPa6g/s1600-h/brand-new-eclipse.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 190px;" src="http://4.bp.blogspot.com/_Qe5NUfA2r5c/SRuD4XsNcZI/AAAAAAAAABQ/8df8nYLPa6g/s320/brand-new-eclipse.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5267949193595285906" /&gt;&lt;/a&gt;
&lt;p&gt;So now the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_12"&gt;initeresting&lt;/span&gt; part of story - &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_13"&gt;JavaRebel&lt;/span&gt; is the &lt;a href="http://java.sun.com/developer/technicalArticles/J2SE/jvm_ti/"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_14"&gt;jvm&lt;/span&gt; agent&lt;/a&gt; which means that it is loaded with a &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_15"&gt;JVM&lt;/span&gt;, receives its events and controls it &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_16"&gt;magicly&lt;/span&gt; reloading changed classes on the fly. So after downloading it from &lt;a href="http://www.zeroturnaround.com/download/"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_17"&gt;zeroturnaround&lt;/span&gt; site&lt;/a&gt; you need to set up &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_18"&gt;JBoss&lt;/span&gt; so that the virtual machine it is running on has this agent. For doing that you need to select &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_19"&gt;JBoss&lt;/span&gt; server in Eclipse servers view (nobody likes running servers from a command line right? :)), double click on it, select "open launch configuration" and add &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_20"&gt;JavaRebel&lt;/span&gt; arguments to the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_21"&gt;VM&lt;/span&gt; arguments:&lt;/p&gt;&lt;p&gt;&lt;b&gt; &lt;span style="font-size:85%;"&gt;-&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_22"&gt;noverify&lt;/span&gt; -&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_23"&gt;javaagent&lt;/span&gt;:/path/to/&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_24"&gt;javarebel&lt;/span&gt;/lib/&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_25"&gt;javarebel&lt;/span&gt;.jar -Drebel.dirs=/paths/to/class/folders/bin&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style="font-size:85%;"&gt;/path/to/javarebel/lib/ - path to a folder where you unzipped javarebel library&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style="font-size:85%;"&gt;/path/to/class/folders/bin -  path where the compiled .class files bein placed (by default eclipse puts it to bin folder of your project)&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;It should look something like:&lt;/p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_Qe5NUfA2r5c/SRykwy0YByI/AAAAAAAAABY/5McZ5KXM-Zw/s1600-h/jboss-jvm-arguments.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 189px;" src="http://2.bp.blogspot.com/_Qe5NUfA2r5c/SRykwy0YByI/AAAAAAAAABY/5McZ5KXM-Zw/s320/jboss-jvm-arguments.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5268266822298765090" /&gt;&lt;/a&gt;
&lt;p&gt;After that, make sure that the automatic publishing is switched on (the same screen where the "open launch configuration" link was).  And you can launch &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_26"&gt;JBoss&lt;/span&gt;. The first message in the server console should show that &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_27"&gt;JavaRebel&lt;/span&gt; is successfully launched - e.g.&lt;/p&gt;&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_Qe5NUfA2r5c/SRypHQucs3I/AAAAAAAAABg/wtrvbxl3Vhw/s1600-h/jboss-console-message.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 123px;" src="http://4.bp.blogspot.com/_Qe5NUfA2r5c/SRypHQucs3I/AAAAAAAAABg/wtrvbxl3Vhw/s320/jboss-console-message.jpg" border="0" alt="" id="BLOGGER_PHOTO_ID_5268271606330602354" /&gt;&lt;/a&gt;It will be a bit different if you do not have a license though :)  The easiest way now to check if the voodoo has worked (for &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_28"&gt;petclinic&lt;/span&gt; of course) is to comment out some validation from org.springframework.samples.petclinic.validation.OwnerValidator than go to http://localhost:8080/petclinic/addOwner.do and try to add an empty one - the commented out validation will not be triggered and you'll see the following message in the server console:&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:78%;"&gt;&lt;strong&gt;INFO  [&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_29"&gt;STDOUT&lt;/span&gt;] &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_30"&gt;JavaRebel&lt;/span&gt;: Reloading class&lt;/strong&gt;&lt;strong&gt; 'org.springframework.samples.petclinic.validation.OwnerValidator'.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;That's really cool, &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_31"&gt;JavaRebel's&lt;/span&gt; hot redeploy works in 95% of &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_32"&gt;real life&lt;/span&gt; situations in my experience  (yeah it does have &lt;a href="http://javabyexample.wisdomplug.com/java-concepts/34-core-java/58-javarebel-says-no-to-restart-really.html"&gt;limitations&lt;/a&gt;) but it does not really bug me. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;The sad part of this story is &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_33"&gt;JSP&lt;/span&gt; Weaver which is still unable to handle most of real life &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_34"&gt;scriptlets&lt;/span&gt; (sometimes even the simple ones like &amp;amp;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_35"&gt;lt&lt;/span&gt;;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_36"&gt;jsp&lt;/span&gt;:&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_37"&gt;setproperty&lt;/span&gt; name="ad" property="language" value="&amp;amp;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_38"&gt;lt&lt;/span&gt;;%= &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_39"&gt;lang&lt;/span&gt; %&amp;gt;" /&amp;gt; are showstoppers for it). &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_40"&gt;Scriptlets&lt;/span&gt; are evil, I know, sometimes though they are lesser evil and I really hope it will be fixed in the near future so I could change "make the situation better" to "solve the problem" and add a &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_41"&gt;JSP&lt;/span&gt; Weaver installation chapter to this tutorial.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7982602471247472304-303051607708898773?l=javadrips.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javadrips.blogspot.com/feeds/303051607708898773/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javadrips.blogspot.com/2008/11/setting-up-javarebel-with-eclipse-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7982602471247472304/posts/default/303051607708898773'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7982602471247472304/posts/default/303051607708898773'/><link rel='alternate' type='text/html' href='http://javadrips.blogspot.com/2008/11/setting-up-javarebel-with-eclipse-and.html' title='setting up javarebel with eclipse and jboss'/><author><name>javadrips</name><uri>http://www.blogger.com/profile/15602953853680745867</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://2.bp.blogspot.com/_Qe5NUfA2r5c/SQB7FkjZRjI/AAAAAAAAAAM/68hjlzhrJbc/S220/me_aventura.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_Qe5NUfA2r5c/SRuD4XsNcZI/AAAAAAAAABQ/8df8nYLPa6g/s72-c/brand-new-eclipse.jpg' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
