Spring MVC Fast Tutorial: Setup

You'll need java 1.5, ant and tomcat. We're going to:

  1. create a web app skeleton
  2. add build files
  3. add required JARs

Web App Skeleton

Create a new folder 'springmvc'.

Inside, create these folders: 'classes', 'jsp', 'WEB-INF'.

In 'jsp', add 'index.jsp':

<html>
  <body>
    <p>Hi</p>
  </body>
</html>

In 'WEB-INF', add 'web.xml' and create the folder 'src':

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <welcome-file-list>
    <welcome-file>
      jsp/index.jsp
    </welcome-file>
  </welcome-file-list>
 
</web-app>

Add 'springmvc' folder to Tomcat webapps's folder, launch Tomcat and test (your Tomcat port may differ): http://localhost:8180/springmvc/

Build files

In 'WEB-INF', add 'build.properties': (update Tomcat path)

appserver.home=/usr/share/tomcat5.5
appserver.lib=${appserver.home}/common/lib

In 'WEB-INF', add 'build.xml':

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build">
 
  <property file="build.properties"/>
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="classes"/>
 
  <path id="build.classpath">
      <fileset dir="lib">
          <include name="*.jar"/>
      </fileset>
      <fileset dir="${appserver.lib}"> <!-- servlet API classes: -->
          <include name="servlet*.jar"/>
      </fileset>
      <pathelement path="${build.dir}"/>
  </path>		
 
  <target name="build">
      <mkdir dir="${build.dir}"/>
      <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
          <src path="${src.dir}"/>
          <classpath refid="build.classpath"/>
      </javac>
  </target>
 
  <target name="clean" description="Clean output directories">
      <delete>
          <fileset dir="${build.dir}">
              <include name="**/*.class"/>
          </fileset>
      </delete>
  </target>
 
</project>

JARs

In 'WEB-INF', create the folder 'lib'.

Add into it these JARs from Spring distribution (with dependencies):

  • spring-framework/dist/spring.jar
  • spring-framework/dist/modules/spring-webmvc.jar
  • spring-framework/lib/jakarta-taglibs/standard.jar
  • spring-framework/lib/jakarta-commons/commons-logging.jar
  • spring-framework/lib/j2ee/servlet-api.jar
  • spring-framework/lib/j2ee/jstl.jar

Summary

 

Feedback

Beware with the xml. Is not well formed, but such a thing this like this can make you loose some of your valuable time xD
Okada
May 7, 2008
#1
Thank you Okada, it's fixed now.
Jérôme Jaglale
May 8, 2008
#2
build.xml and build.properties go at the same level of 'WEB-INF' and not inside it.
Nestor Urquiza
September 11, 2008
#3
I think (I posted it also in the first part, apparently still to be accepted) that a cleaner way to organize the proejct would be maintaining src folder at same level as WEB-INF. In addition when deploying into a server as a war or exploded directory it makes sense to include only the jsp, xml classes and jars and never the src. Below is my proposal for the Ant build.xml (Maven provides a more organized way to handle dependencies and in general your project BTW)
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build">
 
  <property file="build.properties"/>
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="WEB-INF/classes"/>
 
  <path id="build.classpath">
      <fileset dir="WEB-INF/lib">
          <include name="*.jar"/>
      </fileset>
      <fileset dir="${appserver.lib}">
          <include name="*servlet*.jar"/>
      </fileset>
      <pathelement path="${build.dir}"/>
  </path>		
 
  <target name="build" depends="compile">
      <jar destfile="fasttutorial.war">
         <fileset dir="." includes="WEB-INF/**/*, jsp/**/*"/>
      </jar>
  </target>
  
  <target name="compile">
      <mkdir dir="${build.dir}"/>
      <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">
          <src path="${src.dir}"/>
          <classpath refid="build.classpath"/>
      </javac>
  </target>
 
  <target name="clean" description="Clean output directories">
      <delete>
          <fileset dir="${build.dir}">
              <include name="**/*.class"/>
          </fileset>
      </delete>
  </target>
 
</project>
Nestor Urquiza
September 11, 2008
#4
Hi, I´m new into Spring, I´m working with Java, Struts, Jsp, Ajax, etc, and the question I have is Why I should learn Spring ?? I don't understand very well why should learn this framework... Any help would be thanked !!!
Juan Pablo
September 12, 2008
#5
@Juan Pablo: I have logged at
http://thinkinginsoftware.blogspot.com/
my opinion about question: Should I learn Spring Framework?
Nestor Urquiza
September 12, 2008
#6
@Juan Pable: I have blogged in http://thinkinginsoftware.blogspot.com/ about the question: Should I learn Spring Framework?

September 12, 2008
#7
Nice representation. But it helpful if any JAVA class included there..! I mean to say combination of bean. _ Shankar
Shankar Gangadhari
October 11, 2008
#8
zzzzz
zzzzzzzzz
November 6, 2008
#9
fffff
fffffff
February 14, 2009
#10
Is it possible to subscribe to this page using atom or rss? Thanks!
Java
February 14, 2009
#11
Hi I m getting error when try to implement this tutorial like ....springmvc.web.HelloWorldController] for bean with name '/hello_world.html' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]; nested exception is java.lang.ClassNotFoundException: springmvc.web.HelloWorldController org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138) please suggest me ...........even i check all the version of jars are correct............ thnsk in advance
ashish
February 27, 2009
#12
Try "ant clean" then "ant"
Jérôme Jaglale
February 27, 2009
#13
can you help me by showing me how to run your great examples on jetty 6 server pls
milhad
April 8, 2009
#14
I dont see any Spring framework example in this code. Its just standard mvc framework.
anon
April 29, 2009
#15
Nice. but most of the web applications dir structure, classes will be under WEB-INF level, not at the projectRoot level. springmvc WEB-INF classes lib src jsp build.xml
Ganugapati
May 12, 2009
#16
What is the purpose of build.properties? Would appserver.home be directory where i install tomcat? I am using windows xp, tomcat 6.0, would it be, for example c:\tomcat? but tomcat install directory does not have 'common' directory? It all seems to work fine in eclipse, even without the build.properties thanks for the tutorial
babo
May 17, 2009
#17
http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework
name123
June 30, 2009
#18
Hi it wld be helpful if u post some example for MultiActionController... Thanks
AK
July 14, 2009
#19
you can Google "Spring mvc tutorial" to get more info e.g. http://mhimu.wordpress.com/2007/11/27/spring-mvc-tutorial/ http://raibledesigns.com/wiki/Wiki.jsp?page=SpringControllers you can find many more, your searching skills will be your best friend!
LogixPlayer
August 23, 2009
#20
very nice
sreenivas
August 24, 2009
#21
amazing tutorial for someone wanting to jump start to spring...really helped me..
Aswin
October 9, 2009
#22
amazing tutorial for someone wanting to jump start to spring...really helped me..
arun kumar tiwari
November 8, 2009
#23
I am new to Spring,I have a question,For the Spring MVC is it required to learned IOC ,AOP and ORM(I know). Any body reply quickly.
Rakesh
November 13, 2009
#24
i have done basic setups for spring mvc application, but when i transfer the control from a jsp page to controller which is mapped in webapplicationcontext, url gets changed to corresponding mapping done for controller but i get resource not found exception. please let me know whether i missed some configuration
learner
January 18, 2010
#25
Great tutorial thanks. Needed something like this - the Spring reference manual is great at what it does but it's very hard to work out how to fit everything together.
Richard
January 20, 2010
#26
Great tut! Much more straight forward than the one on the official site. Anyway I am trying to set this up using Eclipse and JBoss and I keep keep getting the following when I run the project:

---------------
HTTP Status 404 - /springmvc/

type Status report

message /springmvc/

description The requested resource (/springmvc/) is not available.
---------------

Any help will be much appreciated.


JJ
March 1, 2010
#27
I got this problem of springmvc servlet not found in tomcat and the cause is axis2.jar and when I removed it from my tomcat lib, everything was just fine!!!!! I spent nearly 4 hours to figure this out!!!!!!!!!!!!!
Anonymous
March 31, 2010
#28
sdfghjkl;'
asdfghjk
April 28, 2010
#29
it's amazing and u r confident ed to me.

very much thanks
Ramk
May 4, 2010
#30
nice info, you may see this link http://bekasi-it.blogspot.com
Koharudin
May 6, 2010
#31
really good starting MVC Spring toolkit
Sanjay Sharma
June 13, 2010
#32
could you people give any link for webservices for begginers.
Yugandhar
June 30, 2010
#33
TEST

July 4, 2010
#34
Thanks! very useful :-).....Thanks to Nestor Urquiza too
Shaik Sulaiman
July 16, 2010
#35
Great tutorial I am following the steps of your tutorial but I am facing the same problem like JJ

---------------
HTTP Status 404 - /springmvc/

type Status report

message /springmvc/

description The requested resource (/springmvc/) is not available.
---------------

Any help will be much appreciated.

pravin
July 19, 2010
#36
Nice n helpfullll!!
santy
August 3, 2010
#37
Thanks very useful for beginners.please include examples in netbean also
Raseena
September 3, 2010
#38
Nice tutorial to begin with.

There is small correction neeeded in build.xml

Property should be added.
<property name="appserver.lib" value="lib"/>

appserver.lib is not defined so ant gives error while building.
I tried for one of the program to build.

Well explained tuts. Thanks
Mahendra
September 16, 2010
#39
Very nice
Thanks.

October 7, 2010
#40
im facing the following problem...any help would be appreciated.

HTTP Status 404 - /springmvc

--------------------------------------------------------------------------------

type Status report

message /springmvc

description The requested resource (/springmvc) is not available.

Thanks
helllo
November 13, 2010
#41
first part is ok
Jayanath Buddhika
November 21, 2010
#42
Very good ,thanks !
zberic
January 3, 2011
#43
nice .. ill try these
kipli
March 9, 2011
#44
nice

March 15, 2011
#45
This is bullshit
daniel
April 18, 2011
#46
Great site
Nan
April 30, 2011
#47
Please help me
---------------
HTTP Status 404 - /springmvc/

type Status report

message /springmvc/

description The requested resource (/springmvc/) is not available.
---------------

Any help will be much appreciated.
Guru
May 11, 2011
#48
amazing tutorial
Eagle Chen
May 19, 2011
#49
Nice tutorial
Ken
June 9, 2011
#50
Very nice.
Gayan Virajith
August 4, 2011
#51
@Juan Pablo -- for what it's worth, you should learn Spring because Employers are looking for people who Know Spring.

I'm only just starting to learn, but so far it looks to me like approximately four times more typing than doing it in "C"; never mind four times more places for a maintenance programmer look to find and fix bugs.

Maybe somebody needs to construct a tutorial where the real benefits(?) of Spring are more apparent.
Unca Alby
August 18, 2011
#52
For those who wonder how to convert downloaded project to eclipse dynamic web project.

http://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/
Hipi
September 6, 2011
#53
I am not understand spring framework, it is difficult
Jane
November 29, 2011
#54
Try vannila.com..highly helpful for beginners
Tanniishha
January 20, 2012
#55
Spring MVC is backwords and mundane. No decent company should use it.
S
January 30, 2012
#56