Spring MVC
Spring MVC
Spring MVC
----------------------------------------------------------------------
-----------
-> By Using Spring Web MVC Module we can build 2 types of applications
1) Web applications
2) Distributed Applications
----------------------------------------------------------------------
------------
Spring Web MVC Architecture
----------------------------------------------------------------------
------------
1) DispatcherServlet
2) HandlerMapper
3) Controller
4) ModelAndView
5) ViewResolver
6) View
----------------------------------------------------------------------
------------
-> DispatcherServlet is a predefined servlet class available in Spring
MVC module. It acts as Front Controller and it is responsible for pre-
processing and post-processing of a request.
-> View Component is used to render model data on the view file
----------------------------------------------------------------------
------------
i) spring-boot-starter-web
ii) tomcat-embed-jasper (take this from mvn repo)
i) server port
ii) View Resolver
----------------------------------------------------------------------
------------
Note: When we add spring-boot-starter-web dependency then it will give
Apache Tomcat as default Embedded server.
----------------------------------------------------------------------
-----------
Annotatioons in SpringMVC:
@Controller :
===========
To represent java class as request handler in web mvc applications.
This is used in C 2 B applications.
@RestController:
===============
@RestController Annotations in spring is nothing but combination of
@Controller and @ResponseBody Annotation.It was added in spring 4.0 to
make the development of RestFul Services in spring framework easier.
----------------------------------------------------------------------
------------
Sending Data From Controller to UI
----------------------------------------------------------------------
-----------
-> To send data from Controller to UI we will use Model/ModelAndView
object
-> Model is a Map which represents data in the form of Key-value pair
1) Query Parameters
2) Path Parameters
3) Request Body
Query Parameters
==================
-> Query Parameter will re-present data in URL in the form of Key-
value pair
Ex : localhost:9090/course?name=SBMS
-> Query Parameters are used to send data from UI to server in URL
Path Parameters
===============
-> Path Parameters will represent data in URL directley without key
Ex: localhost:9090/course/{name}/details
-> Path Parameter will start with / symbol and will be seperated by /
symbol only
----------------------------------------------------------------------
------------
-> Query Parameters should present only at the end of the URL
-> To read Query Parameters from the URL we will use @RequestParam
annotation
-> To read Path Parameters from the URL we will use @PathVariable
annotation
-> Query Parameters & Path Parameters are only recommended to send
Small and non-sensitive text data.
----------------------------------------------------------------------
----------
-> If we want to send huge & sensitive data then we should go for
Request body.
-> HTTP POST, PUT and DELETE requests contains request body where as
GET request doesn't have request body.