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
Sep 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
Sep 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
Sep 12, 2008
#5
@Juan Pablo: I have logged at
http://thinkinginsoftware.blogspot.com/
my opinion about question: Should I learn Spring Framework?
Nestor Urquiza
Sep 12, 2008
#6
@Juan Pable: I have blogged in http://thinkinginsoftware.blogspot.com/ about the question: Should I learn Spring Framework?

Sep 12, 2008
#7
Nice representation. But it helpful if any JAVA class included there..! I mean to say combination of bean. _ Shankar
Shankar Gangadhari
Oct 11, 2008
#8
zzzzz
zzzzzzzzz
Nov 6, 2008
#9
fffff
fffffff
Feb 14, 2009
#10
Is it possible to subscribe to this page using atom or rss? Thanks!
Java
Feb 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
Feb 27, 2009
#12
Try "ant clean" then "ant"
Jérôme Jaglale
Feb 27, 2009
#13
can you help me by showing me how to run your great examples on jetty 6 server pls
milhad
Apr 8, 2009
#14
I dont see any Spring framework example in this code. Its just standard mvc framework.
anon
Apr 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
Jun 30, 2009
#18
Hi it wld be helpful if u post some example for MultiActionController... Thanks
AK
Jul 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
Aug 23, 2009
#20
very nice
sreenivas
Aug 24, 2009
#21
amazing tutorial for someone wanting to jump start to spring...really helped me..
Aswin
Oct 9, 2009
#22
amazing tutorial for someone wanting to jump start to spring...really helped me..
arun kumar tiwari
Nov 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
Nov 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
Jan 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
Jan 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
Mar 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
Mar 31, 2010
#28
sdfghjkl;'
asdfghjk
Apr 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
Jun 13, 2010
#32
could you people give any link for webservices for begginers.
Yugandhar
Jun 30, 2010
#33
TEST

Jul 4, 2010
#34
Thanks! very useful :-).....Thanks to Nestor Urquiza too
Shaik Sulaiman
Jul 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
Jul 19, 2010
#36
Nice n helpfullll!!
santy
Aug 3, 2010
#37
Thanks very useful for beginners.please include examples in netbean also
Raseena
Sep 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
Sep 16, 2010
#39
Very nice
Thanks.

Oct 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
Nov 13, 2010
#41
first part is ok
Jayanath Buddhika
Nov 21, 2010
#42
Very good ,thanks !
zberic
Jan 3, 2011
#43
nice .. ill try these
kipli
Mar 9, 2011
#44
nice

Mar 15, 2011
#45
This is bullshit
daniel
Apr 18, 2011
#46
Great site
Nan
Apr 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
Jun 9, 2011
#50
Very nice.
Gayan Virajith
Aug 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
Aug 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
Sep 6, 2011
#53
I am not understand spring framework, it is difficult
Jane
Nov 29, 2011
#54
Try vannila.com..highly helpful for beginners
Tanniishha
Jan 20, 2012
#55
Spring MVC is backwords and mundane. No decent company should use it.
S
Jan 30, 2012
#56
<b>hey nice tutorial</b>
JC
Apr 6, 2012
#57
I'm extremely new to Spring , and was searching for a concise tutorial and this looks like it. I'm starting to setup now.Hope for the best.
Ruth
Apr 9, 2012
#58
Hey can u explain for Google App Engine
Femina
Apr 13, 2012
#59
im using Eclipse 3.7 - GAE - Im able to view my first jsp page but from that its not going to further controllers with return "id" wat should i do?
Femina
Apr 13, 2012
#60
"I am not understand" and "Spring is mundane". Seriously, WTF people.

You should first get an education, go to university, and afterwards learn whatever you want to learn. I WOULD NEVER hire someone like you.
Jordi
May 23, 2012
#61
Thank you for posting this
Alex
Jun 25, 2012
#62
For those of you who are experiencing this issue:

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

type Status report

message /springmvc/

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

Make sure that after you drop the files in the directory that you restart tomcat. Anytime you make a change in tomcat such as adding or deleting files, you need to restart it for those changes to take effect.
Marshall
Jul 11, 2012
#63
Nice..
Nilesh Kumar
Jul 30, 2012
#64
thanks
raheel
Aug 4, 2012
#65
Excellent study material for the self study , Thanks!! Quick speedy one to learn the Implementation part of basics


Gd Work man!!



Subhash Kumar Jha
Aug 14, 2012
#66
Nice Material....
Mandakini
Sep 28, 2012
#67
Hi,
I want to Ask you if there is anybody who could bind an action in jsp to a method of the controller? How can we do it please I'm really blocked :(
hicham
Oct 5, 2012
#68
> Make sure that after you drop the files in the directory that you restart tomcat. Anytime you make a change in tomcat such as adding or deleting files, you need to restart it for those changes to take effect.

Actually, that's not completely true. Tomcat will detect changes to the webapps directory and load or re-load applications found there. Especially simple apps, like this one, should load just fine.

However, I am still getting a 404 error. Yes, I tried bouncing Tomcat, no help. I am using Tomcat 7.0.35. Not sure if the newer generation of Tomcat matters here, but something isn't working. Not what I expected, give how simple the skeleton app is.
Mark Norton
Feb 7, 2013
#69
I figured out my problem. The URL was wrong.
Mark Norton
Feb 7, 2013
#70
Launching tomcat for the first time only , its giving the error" resource not available" .
Tomcat is running. But even unable to print "HI".
Any Help will be appreciated.
Shalu
Jul 4, 2013
#71
I couldn't find these JARs from Spring framework-4.0.0.M2

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
kuhan
Jul 30, 2013
#72