Spring MVC Form
Spring MVC Form
This application will help you learn the concept of Spring 3 MVC forms.
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;9loose.dtd/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
Step -" %ow create a form class in src folder . The code of ./ser.+ava. is
pac0age net.roseindia.form@ pu)lic class $ser A private String user%ame@ private String email@ pu)lic void set$ser%ameBString user%ameCA this.user%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 >esources.src folder. This is the annoted class used as Controller in the application. The code /SimpleFormController.+ava. is.
pac0age net.roseindia.controllers@ import net.roseindia.form.$ser@ import org.springframewor!.stereotype.Controller@ import org.springframewor!.ui.Model@ import org.springframewor!.we . ind.annotation.ModelAttri ute@ import org.springframewor!.we . ind.annotation.>e*uestMapping@ import org.springframewor!.we . ind.annotation.>e*uestMethod@
2Controller
pu)lic class Simple+ormController A 2>e*uestMappingBvalue 3 /9simple+orm.html/? method 3 >e*uestMethod.$%TC pu)lic void simple+ormBModel modelC A model.addAttri uteBnew $serBCC@
D
2>e*uestMappingBvalue 3 /9formoutput.html/? method 3 >e*uestMethod.&OSTC pu)lic void simpleB2ModelAttri ute $ser user? Model modelC A model.addAttri 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 2simpleForm.html 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+orm.html/? method 3 >e*uestMethod.$%TC pu)lic void simple+ormBModel modelC A model.addAttri uteBnew $serBCC@
D )hen user clic!s on the su mit utton following method of Contoller is executed.
2>e*uestMappingBvalue 3 /9formoutput.html/? method 3 >e*uestMethod.&OSTC pu)lic void simpleB2ModelAttri ute $ser user? Model modelC A model.addAttri 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;9loose.dtd/4 0html4 0head4 0meta http8e*uiv3"Content-Type" content3"text/html; charset=ISO-8859-1"4 0title4Insert title here09title4 09head4 0 ody4 0form.form action3" ormo+tp+t#html" command%ame3"+ser" 4 0ta le align3"center" 4 0tr4 0td4$ser %ame .09td4 0td40form.input path3"+ser,ame" 9409td4 09tr4 0tr4 0td4&mail .09td4 0td40form.input 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;9loose.dtd/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 0td40core.out value3/FAuser.user%ameD/ 9409td4 09tr4 0tr4 0td4,our &mail.09td4 0td40core.out value3/FAuser.emailD/ 9409td4
we .xml get configure for spring framewor! . Step 5" %ow create /dispatcher8servlet.xml/ that use view>esolver configuration . The code dispatcher8
servlet.xml 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/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
&nter $ser %ame /$ser%ame / and &mail / $ser%ame2gmail.com/ 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