0% found this document useful (0 votes)
96 views6 pages

Spring MVC Notes

1) This document provides steps to create a sample Spring MVC application including configuring the DispatcherServlet and view resolver in web.xml and creating a controller class. 2) It includes code snippets for the HomeController class which returns a ModelAndView object and sample Employee class to add to the model. 3) The application uses annotations like @Controller, @RequestMapping to map URLs to controller methods and returns a view name and model to display data on the home page.

Uploaded by

Uday Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views6 pages

Spring MVC Notes

1) This document provides steps to create a sample Spring MVC application including configuring the DispatcherServlet and view resolver in web.xml and creating a controller class. 2) It includes code snippets for the HomeController class which returns a ModelAndView object and sample Employee class to add to the model. 3) The application uses annotations like @Controller, @RequestMapping to map URLs to controller methods and returns a view name and model to display data on the home page.

Uploaded by

Uday Kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring MVC Notes:

>Change Perspective to JavaEE


> Get a server view.
(Window->Showview->server
>se!ect a server " #rowse $ead%&nsta!!ed 'ocation.
>inish
---------------------------------------------------
i!e>new>)*na+ic We# Pro,ect.
Provide Pro,ect na+e>net>net> Chec we#.+!
generation
>inish
----------------------------
Sa+p!e Prora+:
 J/0S:
Cop* and paste a!! Spring-J/0S in !i# 1o!der. 1o!der.
---------------------------------------
Step 2:
Con3g4re )ispatcherServ!et in we#.+!

step 5: creating spring con3g 3!e:


na+e r4!e: 67serv!et-na+e>8-serv!et.+!
'ocation: %WE9-&N% !ocation.

Step : Con3g4re / view 0eso!ver C!ass. With


pre3 and
s4;.

Step <: Contro!!er


>Create a Java c!ass.
>=se an annotation Contro!!er (Sterio t*pe annot.

>Ena#!e annotations in con3g 3!e
(4se contet sche+a 1or #ase paage and
annotation con3g

>Writre a re?4est +ethod.


(0e?4estMapping(@%4r!@

e: 0e?4estMapping(@%ho+e@ -- GEA t*pe


0e?4estMapping(va!4eB@%a#cd@"+ethodB0e?4est
Method.PSA

> 0et4rn a Mode!/ndView #,ect.


e:
ret4rn new Mode!/ndView(@$o+ePage@D

Step : V&EW : create a 1o!der str4ct4re 1or pre3


(i1 not eist e: %WE9-&N%,sps
--------------------------------------
Note: 0e?4estMethod is en4+" which de3ne na+ed
constans
1or re?4est +ethods !ie GEA"PSA"P=A"A0/CE...etc
We#.+!
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns="http://a!a.sn.com/#m$/ns/a!aee"
xsi:schemaLocation="http://a!a.sn.com/#m$/ns/a!aee
http://a!a.sn.com/#m$/ns/a!aee/we&-app'2'(.#s)"
id="*e&+pp'," version="2.(">

<servlet>
<servlet-name>sample</servlet-name>
<servlet-
class>org.springframework.web.servlet.ispatcher!ervlet</servle
t-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<"rl-pattern>#.abc</"rl-pattern>
</servlet-mapping>
</web-app>
!ample-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springramewor.org/schema/&eans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springramewor.org/schema/p"
xmlns:c="http://www.springramewor.org/schema/c"

xmlns:context="http://www.springramewor.org/schema/conte#t 
"

xsi:schemaLocation="http://www.springramewor.org/schema/&e
ans

http://www.springramewor.org/schema/&eans/spring-&eans.#s)
  http://www.springramewor.org/schema/conte#t 

http://www.springramewor.org/schema/conte#t/spring-
conte#t.#s)
">
<context:annotation-con$g/>
<context:component-scan base-
package="com.app.contro$$er"/>

<bean
class="org.springramewor.we&.ser!$et.!iew.,nterna$esorcei
eweso$!er">
<propert% name="pre#">
<val"e>/&'(-)*+/,sps/</val"e>
</propert%>
<propert% name="s#">
<val"e>.,sp</val"e>
</propert%>

</bean>

</beans>

omeontroller.,ava
pacage com.app.controller

i+port ,ava."til.0rra%List
i+port ,ava."til.List

i+port org.springframework.stereot%pe.ontroller
i+port
org.springframework.web.bind.annotation.1e2"est3apping
i+port
org.springframework.web.bind.annotation.1e2"est3ethod
i+port org.springframework.web.servlet.3odel0nd4iew

5ontroller
p4#!ic c!ass omeontroller 6

51e2"est3apping78/home89
p4#!ic 3odel0nd4iew show4iew796

//3odel0nd4iew mav=new
3odel0nd4iew78omeage89
//ret"rn mav
!tring s;=8hello8
'mplo%ee emp=new 'mplo%ee79
emp.set'mp)d7;;9
emp.set'mp*ame780(89
List<!tring> listata= new 0rra%List<!tring>79
listata.add78hello89
listata.add78b%e89
listata.add78ok89

ret4rn new
3odel0nd4iew78omeage88listata8listata9

51e2"est3apping7val"e=8/home8method=1e2"est3ethod.45ST 
9
p4#!ic 3odel0nd4iew show4iew;796

ret4rn n4!!


'mplo%ee.,ava
pacage com.app.controller

p4#!ic c!ass 'mplo%ee 6
private int emp)d
private !tring emp*ame
p4#!ic int get'mp)d79 6
ret4rn emp)d

p4#!ic void set'mp)d7int emp)d9 6
this.emp)d = emp)d

p4#!ic !tring get'mp*ame79 6
ret4rn emp*ame

p4#!ic void set'mp*ame7!tring emp*ame9 6
this.emp*ame = emp*ame

You might also like