KEMBAR78
Spring MVC Basics | PPT
Spring-MVC Basics Bozhidar Bozhanov Bulgarian Association of Software Developers www.devbg.org http://techblog.bozho.net
MVC Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
Model Map<String, Object> Populated by controllers Contains all data needed by the „view“ Not containing business logic model.addAttribute(&quot;orders&quot;,  orderService.getDailyOrders(user)); model.addAttribute(&quot;period&quot;, Period.DAILY);
View HTML, XML, RSS, PDF, etc... JSP, Freemarker, Velocity, etc Direct access to the model <span> ${latestOrder.date} <span> <input type=&quot;text&quot;  value=&quot; ${order.name} &quot; />
Controller Gathers input parameters and coordinates the back-end process Front controller vs controller @Controller RESTful URLs, request-process-forward, @RequestMapping Method=Action Unit-test friendly Pluggable mechanism for argument resolution
Controller Gathers input parameters and coordinates the back-end process Front controller vs controller @Controller RESTful URLs, request-process-forward, @RequestMapping Method=Action Unit-test friendly Pluggable mechanism for argument resolution
Controller @Controller @RequestMapping(&quot;/users&quot;) public   class  UserController { @Inject   private  UserService  service ; @RequestMapping(&quot;/login&quot;) public  String login(@RequestParam String username, @RequestParam String pass){ User u = service.login(username, pass); return &quot;welcomePage&quot;; } @RequestMapping(&quot;/view/{username}&quot;) public  String view(@PathVariable String username){ User u = service.find(username); ... return &quot;userPage&quot;; } }
Controller arguments @RequestParam, @PathVariable POJO – arbitrary java object that gets populated with request values @Valid – to enforce validation of the POJO BindingResult – to access the results of binding and validation Model, ModelMap, Map<String, ?> - acces to the model object Raw HttpServletRequest, response, session Locale, @RequestHeader, @RequestBody,....
Controllers – return types String, View, ModelAndView – use a “view“ @ResponseBody – any object @Controller @RequestMapping(&quot;/users&quot;) public   class  UserController { @Inject   private  UserService  service ; @RequestMapping(value=&quot;/ajaxView&quot;  consumes=&quot;application/json&quot;) @ResponseBody public  User view(@RequestParam String username){ User u = service.find(username); return user; } }
More... Interceptors Exception handling Binding and validation customization Static resource handling Caching configuration Locale and theme resolution Multipart handling Flash scope, conversation scope (web flow)
The lifecycle once again Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
Demo (spring mvc showcase)
Resources http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html http://static.springsource.org/docs/petclinic.htmlhttps://github.com/SpringSource/spring-mvc-showcase
Questions ?

Spring MVC Basics

  • 1.
    Spring-MVC Basics BozhidarBozhanov Bulgarian Association of Software Developers www.devbg.org http://techblog.bozho.net
  • 2.
  • 3.
    Model Map<String, Object>Populated by controllers Contains all data needed by the „view“ Not containing business logic model.addAttribute(&quot;orders&quot;, orderService.getDailyOrders(user)); model.addAttribute(&quot;period&quot;, Period.DAILY);
  • 4.
    View HTML, XML,RSS, PDF, etc... JSP, Freemarker, Velocity, etc Direct access to the model <span> ${latestOrder.date} <span> <input type=&quot;text&quot; value=&quot; ${order.name} &quot; />
  • 5.
    Controller Gathers inputparameters and coordinates the back-end process Front controller vs controller @Controller RESTful URLs, request-process-forward, @RequestMapping Method=Action Unit-test friendly Pluggable mechanism for argument resolution
  • 6.
    Controller Gathers inputparameters and coordinates the back-end process Front controller vs controller @Controller RESTful URLs, request-process-forward, @RequestMapping Method=Action Unit-test friendly Pluggable mechanism for argument resolution
  • 7.
    Controller @Controller @RequestMapping(&quot;/users&quot;)public class UserController { @Inject private UserService service ; @RequestMapping(&quot;/login&quot;) public String login(@RequestParam String username, @RequestParam String pass){ User u = service.login(username, pass); return &quot;welcomePage&quot;; } @RequestMapping(&quot;/view/{username}&quot;) public String view(@PathVariable String username){ User u = service.find(username); ... return &quot;userPage&quot;; } }
  • 8.
    Controller arguments @RequestParam,@PathVariable POJO – arbitrary java object that gets populated with request values @Valid – to enforce validation of the POJO BindingResult – to access the results of binding and validation Model, ModelMap, Map<String, ?> - acces to the model object Raw HttpServletRequest, response, session Locale, @RequestHeader, @RequestBody,....
  • 9.
    Controllers – returntypes String, View, ModelAndView – use a “view“ @ResponseBody – any object @Controller @RequestMapping(&quot;/users&quot;) public class UserController { @Inject private UserService service ; @RequestMapping(value=&quot;/ajaxView&quot; consumes=&quot;application/json&quot;) @ResponseBody public User view(@RequestParam String username){ User u = service.find(username); return user; } }
  • 10.
    More... Interceptors Exceptionhandling Binding and validation customization Static resource handling Caching configuration Locale and theme resolution Multipart handling Flash scope, conversation scope (web flow)
  • 11.
    The lifecycle onceagain Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
  • 12.
  • 13.
  • 14.