0% found this document useful (0 votes)
92 views

Spring 3 MVC Tutorial

This document provides steps to set up a basic Spring MVC application in Eclipse including: 1) creating an Eclipse project, 2) adding required JAR files, 3) creating controller and view code, 4) configuring the Spring application context and web.xml files, and 5) running the project on Tomcat to test. The summary outlines the key components and configurations needed to get a simple "hello world" Spring MVC app running.

Uploaded by

Nikhitha Pai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Spring 3 MVC Tutorial

This document provides steps to set up a basic Spring MVC application in Eclipse including: 1) creating an Eclipse project, 2) adding required JAR files, 3) creating controller and view code, 4) configuring the Spring application context and web.xml files, and 5) running the project on Tomcat to test. The summary outlines the key components and configurations needed to get a simple "hello world" Spring MVC app running.

Uploaded by

Nikhitha Pai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring MVC 3.

0 Tutorial
Posted at December 29, 2009 In this post, I am going to give you a start up code base for a Spring MVC based app!ication" If you #ant to go bac$ and refer to the previous version of Spring MVC then this artic!e #i!! he!p you" I am assuming that the reader is a!ready a#are of %ava & and Spring basic concepts !i$e annotations, dependency in'ection etc" Spring "0 has e(tensive!y used the popu!ar annotation feature of %ava &" )s #e move ahead in this artic!e, I*!! mention more ne# things #herever app!icab!e" It #i!! ta$e fo!!o#ing five steps to ma$e everything #or$ on ec!ipse and tomcat" +" 2" " ." &" ,c!ipse pro'ect set up )ddition of re-uired 'ars Creation of re-uired code e!ements /unning pro'ect on tomcat 0esting everything

1et us go through each step in detai!" 1. Eclipse Project Set Up: Some of us may thin$ that this is an e!ementary, but I am mentioning it for those #ho are ne# to ec!ipse" In this step #e create a dynamic #eb pro'ect #ith name SpringMVC 0utoria!" 2ou can use any ,c!ipse version3 most of the recent versions are having these steps simi!ar" I am using ,c!ipse 4a!i!eo for %,, 5ec!ipse6'ee6ga!i!eo6S/+6 #in 2"7ip from ec!ipse do#n!oad site8" 0his contains many advanced features, #hich he!p in deve!opment as #e!! as dep!oyment of app!ication on different servers" ,c!ipse menu 9i!e 6: ;e# 6: Pro'ect #i!! open this #indo#" Se!ect Dynamic <eb Pro'ect from it and c!ic$ on ne(t"

It #i!! open fo!!o#ing #indo# #here you specify the pro'ect name as =SpringMVC 0utoria!* and se!ect the server on #hich you #ant to dep!oy app!ication" I am se!ecting tomcat >"0 as it is avai!ab!e on my machine" 2ou can configure server as per avai!abi!ity" C!ic$ ne(t on fo!!o#ing screen"

;o# change the source fo!der from =src* to =src?main?'ava*" 0his #i!! he!p us to $eep main and test 'ava c!asses separate" 1ater bui!d process becomes easy #ith bui!d too!s !i$e ant and maven" It is a!so maintainab!e" )!so on same screen, change the defau!t output fo!der" 0his generates the c!asses at right p!ace 5src?main?#ebapp?<,@6I;9?c!asses8 #ith respect to #eb archive 5#ar8" C!ic$ ne(t button after that"

Configure #eb modu!e here" <e $eep it as =src?main?#ebapp*"

C!ic$ing on finish button #i!! generate comp!ete pro'ect structure for us" It #i!! a!so generate #eb"(m! #hich #e*!! modify !ater" 2. Addition o !e"uired #ars: 1et us compare the 'ars #e used in Spring 2"& and in current version of Spring #hich is "0" Spring 2.$ #ars: aopa!!iance6+"0"'ar commons6!ogging6+"+"+"'ar serv!et6api62"&"'ar spring6beans62"&"&"'ar spring6conte(t62"&"&"'ar spring6conte(t6support62"&"&"'ar spring6core62"&"&"'ar spring6#eb62"&"&"'ar spring6#ebmvc62"&"&"'ar Spring 3.0 #ars: aopa!!iance6+"0"'ar commons6!ogging6+"+"+"'ar serv!et6api62"&"'ar org.springframework.asm-3.0.0.RELEASE.jar org"springframe#or$"beans6 "0"0"/,1,)S,"'ar org"springframe#or$"conte(t6 "0"0"/,1,)S,"'ar org"springframe#or$"conte(t"support6 "0"0"/,1,)S,"'ar org"springframe#or$"core6 "0"0"/,1,)S,"'ar

org"springframe#or$"e(pression6 "0"0"/,1,)S,"'ar org"springframe#or$"#eb6 "0"0"/,1,)S,"'ar org.springframework.web.servlet-3.0.0.RELEASE.jar %ar names in ita!ics are ne# re-uirements as compared to previous versions" AasmA"'ar is ne#!y added in this stac$" Instead of A#ebmvcA"'ar no# #e have A#eb"serv!etA"'ar" )dd these 'ars to =src?main?#ebapp ?<,@6I;9?!ib* fo!der" 0hese 'ars can be do#n!oaded from Spring*s site or from Maven repository" 3. Creation o !e"uired Code Ele%ents: Bsing above t#o steps, #e have created the s$e!eton #e re-uire to #rite our Spring MVC app!ication" ;o# #e identify the code e!ements #e re-uire to ma$e simp!e he!!o #or!d #eb app!ication #or$" Ce!!o<or!dContro!!er"'ava he!!o#or!d"'sp SpringMVC 0utoria!6serv!et"(m! Some Changes to #eb"(m! &src&%ain&ja'a&co%&%(org&spring%'ctutorial&)e*&controller& +ello,orldController.ja'a: 0his is the contro!!er c!ass or #hat #e ca!! =C* in the MVC pattern" If #e ta$e a c!oser !oo$ at the code be!o# then #e can notice fo!!o#ing things" 6 It is not e(tending from any frame#or$ c!ass but using annotation DContro!!er" 6 0here is no specific method to be used to hand!e re-uest" <e have our business method he!!o<or!d58 #hich is doing the re-uest hand!ing" 6 /e-uest processing method he!!o<or!d58 is mapped to re-uest pattern Ehe!!o<or!d 5actua!!y it #i!! be =httpFEE!oca!hostFG0G0ESpringMVC 0utoria!Ehe!!o<or!d"htm*8" 0here are more annotations around contro!!er, re-uest mapping, and session hand!ing etc" <e !eave those for more detai!ed discussion"
package com.myorg.springmvctutorial.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.apache.commons.logging. og; import org.apache.commons.logging. og!actory; "Controller public class #ello$orldController% protected final og logger &

og!actory.get og'getClass'((;

"RequestMapping')*hello$orld)( public ModelAndView hello$orld'(% logger.info')Return View)(; return new ModelAndView')helloworld.+sp)(; , ,

&src&%ain&)e*app&-ello)orld.jsp: 0his is a simp!e htm! page #hich #i!! confirm that everything is integrated and #or$ing fine"
-html. -head. -title.#ello $orld-*title. -*head. -body. -h/.0his is 1pring MVC 2 0utorial-*h/. -p.3ur 0utorial is successful.-*p. -*body. -*html.

&src&%ain&)e*app&,E./012 &SpringMVC3Tutorial/ser'let.3%l: 0his fi!e contains a!! bean definitions" In our e(amp!e, it shou!d contain (m! !i$e this H
-45ml version&)/.6) encoding&)70!89)4. -beans 5mlns&)http:**www.springframework.org*schema*beans) 5mlns:5si&)http:**www.w2.org*;66/*<M 1chema8instance) 5si:schema ocation&)http:**www.springframework.org*schema*beans http:**www.springframework.org*schema*beans*spring8beans8;.=.5sd). -bean name&)*helloworld.htm) class&)com.myorg.springmvctutorial.web.controller.#ello$orldController) *. -*beans.

.ut )e are going to -a'e so%et-ing di erent. 0nstead o e3plicit de initions li4e t-is5 )e use ollo)ing t)o eatures 6 6 ur!" 6 /e-uestMapping annotation used in contro!!er c!ass above #hich maps he!!o<or!d58 method to Ehe!!o<or!d )uto#iring feature of spring"

)!so this (m! #i!! be va!idated using ne#!y provided (sds in Spring "0"
-45ml version&)/.6) encoding&)70!89)4. -beans 5mlns&)http:**www.springframework.org*schema*beans) 5mlns:5si&)http:**www.w2.org*;66/*<M 1chema8instance) 5mlns:p&)http:**www.springframework.org*schema*p) 5mlns:conte5t&)http:**www.springframework.org*schema*conte5t) 5si:schema ocation&) http:**www.springframework.org*schema*beans http:**www.springframework.org*schema*beans*spring8beans82.6.5sd http:**www.springframework.org*schema*conte5t http:**www.springframework.org*schema*conte5t*spring8conte5t82.6.5sd). -conte5t:component8scan base8package&)com.myorg.springmvctutorial.web.controller) *. -*beans.

&src&%ain&)e*app&,E./012 &)e*.3%l: It cannot have on!y the conte(t root definition no#" ,ntire Spring MVC #eb app!ication runs on DispatcherServ!et" <e need to define this serv!et in #eb"(m!" Cere is ho# #e define it"

-45ml version&)/.6) encoding&)70!89)4. -web8app 5mlns:5si&)http:**www.w2.org*;66/*<M 1chema8instance) 5mlns&)http:**+ava.sun.com*5ml*ns*+avaee) 5mlns:web&)http:**+ava.sun.com*5ml*ns*+avaee*web8app>;>=.5sd) 5si:schema ocation&)http:**+ava.sun.com*5ml*ns*+avaee http:**+ava.sun.com*5ml*ns*+avaee*web8app>;>=.5sd) id&)$ebApp>?@) version&);.=). -display8name.1pringMVC20utorial-*display8name. -servlet. -servlet8name.1pringMVC20utorial-*servlet8name. -servlet8class.org.springframework.web.servlet.@ispatcher1ervlet-*servlet8 class. -load8on8startup./-*load8on8startup. -*servlet. -servlet8mapping. -servlet8name.1pringMVC20utorial-*servlet8name. -url8pattern.A.htm-*url8pattern. -*servlet8mapping. -welcome8file8list. -welcome8file.inde5.htm-*welcome8file. -*welcome8file8list. -*web8app.

7. !unning Project on To%cat: 2ou can choose #hat you !i$e from fo!!o#ing options" 6 Bse tomcat integration #ith ec!ipse and dep!oy using right c!ic$ /un 6: /un on Server option" 6 ,(port pro'ect as a #ar using 9i!e 6: ,(port menu option and copy generated #ar fi!e in #ebapps directory of tomcat" 6 Bse any bui!d too! !i$e ant, maven to generate the #ar fi!e and copy it to #ebapps directory on tomcat" ur! $. Testing E'er(t-ing: 0o see if everything is integrated and #or$ing fine, access this httpFEE!oca!hostFG0G0ESpringMVC 0utoria!Ehe!!o<or!d"htm in bro#ser and #e see fo!!o#ing page disp!ayed"

You might also like