Java Spring Training
Spring MVC
Page 1Classification: Restricted
Review
• Spring framework
• Inversion of Control
• Dependency Injection – Two types
• Defining beans using XML
• Inheriting beans
• Auto-wiring
• Annotations based configuration
• Java based configuration
• Spring AOP
Page 2Classification: Restricted
Agenda
• Spring MVC
• Spring MVC – Hibernate Integration
Java & JEE Training
Spring MVC
Page 4Classification: Restricted
Spring MVC Step-by-step
• Refer the official Spring documentation:
https://docs.spring.io/docs/Spring-MVC-step-by-step/
Page 5Classification: Restricted
MVC…
• What is MVC pattern? Review…
• The Model encapsulates the application data and in general they will
consist of POJO.
• The View is responsible for rendering the model data and in general it
generates HTML output that the client's browser can interpret.
• The Controller is responsible for processing user requests and building
appropriate model and passes it to the view for rendering.
Page 6Classification: Restricted
Spring MVC
• Spring MVC tutorial provides an elegant solution to use MVC in Spring
framework by the help of DispatcherServlet.
• In Spring Web MVC, DispatcherServlet class works as the front controller. It
is responsible to manage the flow of the Spring MVC application.
• The @Controller annotation is used to mark the class as the controller in
Spring 3.
• The @RequestMapping annotation is used to map the request url. It is
applied on the method.
Page 7Classification: Restricted
Advantages of Spring 3.0 MVC
• Supports RESTful URLs.
• Annotation based configuration.
• Supports to plug with other MVC frameworks like Struts etc.
• Flexible in supporting different view types like JSP, velocity, XML, PDF etc.,
Page 8Classification: Restricted
MVC – An overview
Controller
View
Model
Request
Response
Page 9Classification: Restricted
Spring MVC- Basic Architecture
Dispatch
erServlet
(Front
controller)
HandlerMapping
(Map of URL and
controllers)
Controller
(Responsible to
handle request)
View (JSP, XML,
Velocity)
Model
(POJO)
Request
1
5
4
3
2
www.JAVA9S.c
Page 10Classification: Restricted
Spring MVC Demo – Hello World
• Basic Demo…
Page 11Classification: Restricted
Spring MVC Annotations
• @Controller
• @RequestMapping
• @RequestParam
• @PathVariable
• @ResponseBody
Page 12Classification: Restricted
@RequestMapping with Class
@Controller
@RequestMapping("/home")
public class HomeController {
}
Page 13Classification: Restricted
@RequestMapping with Method
@RequestMapping(value="/student")
@ResponseBody
public String method0(){
return “some string";
}
Page 14Classification: Restricted
@RequestMapping with Multiple URI
@RequestMapping(value={"/student",“/student/sciencestudent"})
@ResponseBody
public String method1(){
return “somestring";
}
Page 15Classification: Restricted
@RequestMapping with HTTP Method
@RequestMapping(value="/student", method=RequestMethod.POST)
@ResponseBody
public String method2(){
return “some string";
}
@RequestMapping(value="/student2",
method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return “some string";
}
Page 16Classification: Restricted
@RequestMapping with Headers
@RequestMapping(value="/student", headers="name=Pawan")
@ResponseBody
public String method4(){
return “something";
}
@RequestMapping(value="/method5", headers={"name=Pawan", "id=1"})
@ResponseBody
public String method5(){
return “some return value";
}
Page 17Classification: Restricted
@RequestMapping with @PathVariable
@RequestMapping(value="/student/{id}")
@ResponseBody
public String method7(@PathVariable("id") int id){
return “the value with id="+id;
}
Page 18Classification: Restricted
@RequestMapping with @RequestParam for URL parameters
@RequestMapping(value="/student") //e.g.
http://....appname/student?id=111
@ResponseBody
public String method9(@RequestParam("id") int id){
return “student with id= "+id;
}
Page 19Classification: Restricted
@RequestMapping fallback method (404)
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
Page 20Classification: Restricted
Spring – More Demo….
• Demo…
Page 21Classification: Restricted
Topics to be covered in next session
• Frontend development fundamentals HTML, CSS, JS, jQuery, AJAX
Page 22Classification: Restricted
Thank you!

Spring - Part 4 - Spring MVC