In this tutorial we are going to develop Spring 3 MVC form example.
This application will help you learn the concept of Spring 3 MVC forms.
Spring 3 MVC Form xample
In this tutorial we are going to develop Spring 3 MVC form example. This application will help you learn the concept of Spring 3 MVC forms. After completing this tutorial you will e a le to understand the Spring 3 MVC ased applications and use the !nowledge gained here in developing more complex applications using Spring 3 MVC module. In this application there are two "sp files. #ne "sp file for allowing the user to enter $ser %ame and &mail and another "sp file to display the same values into another 'S( page. )e have also developed a controller to process the re*uest and then return the Model and view for rendering the output. The Simple+ormController."ava class is developed y us to wor! as controller here. This is the annoted controller used in the application. ,ou can download and open the pro"ect in &clipse I-& and then run it in eclipse. ,ou will e needing the tomcat server to run the application from &clipse. In this example application uses the following "ar files.
A ove "ar files are included in the example pro"ect. Step !" File#$%ew#$&ress 'ther#$(e)#$Select *ynamic (e) &ro+ect and &ress %ext Step ," %ow &nter (ro"ect name /Spring3 xample/ and (ress %ext . After (ress +inish. Step 3" Create index."sp under (e)Content#
012 page language3"java" contentType3"text/html; charset=ISO-8859-1" page&ncoding3"ISO-8859-1"14 05-#CT,(& html ($67IC /899)3C99-T- :TM7 ;.<= Transitional99&%/ /http.99www.w3.org9T>9html;[Link]/4 0html4 0head4 0meta http8e*uiv3"Content-Type" content3"text/html; charset=ISO-8859-1"4 0title4Spring 3? MVC &xamples09title4 09head4 0 ody4 0h=4Spring 3? MVC &xamples09h=4
0ul4 0li40a href3" orms/s!mple"orm#html"4Simple +orm09a409li4 09ul4 09 ody4 09html4
Step -" %ow create a form class in src folder . The code of ./ser.+ava. is
pac0age [Link]@ pu)lic class $ser A private String user%ame@ private String email@ pu)lic void set$ser%ameBString user%ameCA [Link]%ame3user%ame@
D
pu)lic String get$ser%ameBCA return user%ame@
D
pu)lic void set&mailBString emailCA this.email3email@
D
pu)lic String get&mailBCA return email@
D D Step 1" %ow create Controller class . SimpleFormController.+ava. under "ava >[Link] folder. This is the annoted class used as Controller in the application. The code /SimpleFormController.+ava. is.
pac0age [Link]@ import [Link].$ser@ import [Link]!.[Link]@ import [Link]!.[Link]@ import [Link]!.we . [Link] ute@ import [Link]!.we . [Link].>e*uestMapping@ import [Link]!.we . [Link].>e*uestMethod@
2Controller
pu)lic class Simple+ormController A 2>e*uestMappingBvalue 3 /9simple+[Link]/? method 3 >e*uestMethod.$%TC pu)lic void simple+ormBModel modelC A [Link] uteBnew $serBCC@
D
2>e*uestMappingBvalue 3 /[Link]/? method 3 >e*uestMethod.&OSTC pu)lic void simpleB2ModelAttri ute $ser user? Model modelC A [Link] uteB/user/? userC@
D D The annotation 2Controller ma!es this class a controller. The 2>e*uestMapping annotation is used to map a usiness method to the re*uest uri. In the following code snippet [Link] uri is mapped to the simple+ormB..C method. The method 3 >e*uestMethod.E&T is for mapping the E&T method. :ere is full code of simple+ormB..C function mapping.
2>e*uestMappingBvalue 3 /9simple+[Link]/? method 3 >e*uestMethod.$%TC pu)lic void simple+ormBModel modelC A [Link] uteBnew $serBCC@
D )hen user clic!s on the su mit utton following method of Contoller is executed.
2>e*uestMappingBvalue 3 /[Link]/? method 3 >e*uestMethod.&OSTC pu)lic void simpleB2ModelAttri ute $ser user? Model modelC A [Link] uteB/user/? userC@
)ith the help of 2>e*uestMapping annotation you can map re*uest uri to a method in the contoller class and then Spring framewor! will execute the mapped method. Step 3" %ow create views folder under )e Content84)&68I%+. After cerate folder views inside cerate /simple+orm."sp/ file root )e Content9 )&68I%+9views 9simple+orm."sp. The code simple+orm."sp as .
012 page language3"java" contentType3"text/html; charset=ISO-8859-1" page&ncoding3"ISO-8859-1"14 012tagli prefix3" orm" uri3"http'//(((#spr!n) rame(or*#or)/ta)s/ orm" 14 05-#CT,(& html ($67IC /899)3C99-T- :TM7 ;.<= Transitional99&%/ /http.99www.w3.org9T>9html;[Link]/4 0html4 0head4 0meta http8e*uiv3"Content-Type" content3"text/html; charset=ISO-8859-1"4 0title4Insert title here09title4 09head4 0 ody4 [Link] action3" ormo+tp+t#html" command%ame3"+ser" 4 0ta le align3"center" 4 0tr4 0td4$ser %ame .09td4 [Link] path3"+ser,ame" 9409td4 09tr4 0tr4 0td4&mail .09td4 [Link] path3"ema!l" 9409td4 09tr4 0tr4 0td409td4
0td40input type3"s+-m!t" value3"S+-m!t" 9409td4 09tr4 09ta le4 09form.form4 09 ody4 09html4
Step 4" %ow create /formoutput."sp/ under )e Content9 )&68I%+9views 9formoutput."sp. The code /formoutput."sp/ is
012 page language3"java" contentType3"text/html; charset=ISO-8859-1" page&ncoding3"ISO-8859-1"14 012tagli prefix3"core" uri3"http'//java#s+n#com/jsp/jstl/core" 14 05-#CT,(& html ($67IC /899)3C99-T- :TM7 ;.<= Transitional99&%/ /http.99www.w3.org9T>9html;[Link]/4 0html4 0head4 0meta http8e*uiv3"Content-Type" content3"text/html; charset=ISO-8859-1"4 0title4Insert title here09title4 09head4 0 ody4 0ta le align3"center" 4 0tr4 0td4)elcome.09td4 [Link] value3/[Link]%ameD/ 9409td4 09tr4 0tr4 0td4,our &mail.09td4 [Link] value3/[Link]/ 9409td4
09tr4 09ta le4 09 ody4 09html4
Step 4" Again modify we .xml file as.
0Gxml version3"1#." encoding3"/T"-8"G4 0we 8app version3"0#5" xmlns3"http'//java#s+n#com/xml/ns/j0ee" xmlns.xsi3"http'//(((#(1#or)/0..1/234Schema-!nstance" xsi.schema7ocation3"http'//java#s+n#com/xml/ns/j0ee http'//java#s+n#com/xml/ns/j0ee/(e--app5055#xs6"4 0display8name4Spring3&xample09display8name4 0servlet4 0servlet8name4dispatcher09servlet8name4 [Link]!.we .servlet.-ispatcherServlet09servlet8class4 0load8on8startup4=09load8on8startup4 09servlet4 0servlet8mapping4 0servlet8name4dispatcher09servlet8name4 0url8pattern49forms9H09url8pattern4 09servlet8mapping4 0welcome8file8list4 0welcome8file4index."sp09welcome8file4 09welcome8file8list4 09we 8app4
we .xml get configure for spring framewor! . Step 5" %ow create /[Link]/ that use view>esolver configuration . The code dispatcher8
[Link] is.
0Gxml version3"1#." encoding3"/T"-8"G4 0 eans xmlns3"http'//(((#spr!n) rame(or*#or)/schema/-eans" xmlns.xsi3"http'//(((#(1#or)/0..1/234Schema-!nstance" xmlns.p3"http'//(((#spr!n) rame(or*#or)/schema/p" xmlns.context3"http'//(((#spr!n) rame(or*#or)/schema/context" xmlns.mvc3"http'//(((#spr!n) rame(or*#or)/schema/mvc" xsi.schema7ocation3"
http'//(((#spr!n) rame(or*#or)/schema/-eans http'//(((#spr!n) rame(or*#or)/schema/-eans/spr!n)--eans-1#.#xs6 http'//(((#spr!n) rame(or*#or)/schema/context http'//(((#spr!n) rame(or*#or)/schema/context/spr!n)-context-1#.#xs6
http'//(((#spr!n) rame(or*#or)/schema/mvc
http'//(((#spr!n) rame(or*#or)/schema/mvc/spr!n)-mvc-1#.#xs6
"4 0context.component8scan ase8pac!age3"net#rose!n6!a#controllers"94 0 ean id3"v!e(7esolver" class3"or)#spr!n) rame(or*#(e-#servlet#v!e(#Internal7eso+rce8!e(7esolver" 4 0property name3"pre !x"4 0value49)&68I%+9views909value4 09property4 0property name3"s+ !x"4 0value4."sp09value4 09property4 09 ean4 09 eans4
Step 6" %ow will e run application and see the output as
Step !7" Again clic! /Simple +orm/ hyperlin! display as simple+orm .
&nter $ser %ame /$ser%ame / and &mail / $ser%[Link]/ and press Su mit . display output as
In this example we created a simple form ased application using Spring 3 MVC module of Spring framewor!. *ownload Code -ownload the code of the application discussed here from the following url. -ownload example code