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 <ul><li>Map<String, Object> </li></ul><ul><li>Populated by controllers </li></ul><ul><li>Contains all data needed by...
View <ul><li>HTML, XML, RSS, PDF, etc... </li></ul><ul><li>JSP, Freemarker, Velocity, etc </li></ul><ul><li>Direct access ...
Controller <ul><li>Gathers input parameters and coordinates the back-end process </li></ul><ul><li>Front controller vs con...
Controller <ul><li>Gathers input parameters and coordinates the back-end process </li></ul><ul><li>Front controller vs con...
Controller @Controller @RequestMapping(&quot;/users&quot;) public   class  UserController { @Inject   private  UserService...
Controller arguments <ul><li>@RequestParam, @PathVariable </li></ul><ul><li>POJO – arbitrary java object that gets populat...
Controllers – return types <ul><li>String, View, ModelAndView – use a “view“ </li></ul><ul><li>@ResponseBody – any object ...
More... <ul><li>Interceptors </li></ul><ul><li>Exception handling </li></ul><ul><li>Binding and validation customization <...
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.springso...
Questions ?
Upcoming SlideShare
Loading in...5
×

Spring MVC Basics

9,914

Published on

A short presentation about the basics of spring

Spring MVC Basics

  1. 1. Spring-MVC Basics Bozhidar Bozhanov Bulgarian Association of Software Developers www.devbg.org http://techblog.bozho.net
  2. 2. MVC Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
  3. 3. Model <ul><li>Map<String, Object> </li></ul><ul><li>Populated by controllers </li></ul><ul><li>Contains all data needed by the „view“ </li></ul><ul><li>Not containing business logic </li></ul>model.addAttribute(&quot;orders&quot;, orderService.getDailyOrders(user)); model.addAttribute(&quot;period&quot;, Period.DAILY);
  4. 4. View <ul><li>HTML, XML, RSS, PDF, etc... </li></ul><ul><li>JSP, Freemarker, Velocity, etc </li></ul><ul><li>Direct access to the model </li></ul><span> ${latestOrder.date} <span> <input type=&quot;text&quot; value=&quot; ${order.name} &quot; />
  5. 5. Controller <ul><li>Gathers input parameters and coordinates the back-end process </li></ul><ul><li>Front controller vs controller </li></ul><ul><li>@Controller </li></ul><ul><li>RESTful URLs, request-process-forward, @RequestMapping </li></ul><ul><li>Method=Action </li></ul><ul><li>Unit-test friendly </li></ul><ul><li>Pluggable mechanism for argument resolution </li></ul>
  6. 6. Controller <ul><li>Gathers input parameters and coordinates the back-end process </li></ul><ul><li>Front controller vs controller </li></ul><ul><li>@Controller </li></ul><ul><li>RESTful URLs, request-process-forward, @RequestMapping </li></ul><ul><li>Method=Action </li></ul><ul><li>Unit-test friendly </li></ul><ul><li>Pluggable mechanism for argument resolution </li></ul>
  7. 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. 8. Controller arguments <ul><li>@RequestParam, @PathVariable </li></ul><ul><li>POJO – arbitrary java object that gets populated with request values </li></ul><ul><li>@Valid – to enforce validation of the POJO </li></ul><ul><li>BindingResult – to access the results of binding and validation </li></ul><ul><li>Model, ModelMap, Map<String, ?> - acces to the model object </li></ul><ul><li>Raw HttpServletRequest, response, session </li></ul><ul><li>Locale, @RequestHeader, @RequestBody,.... </li></ul>
  9. 9. Controllers – return types <ul><li>String, View, ModelAndView – use a “view“ </li></ul><ul><li>@ResponseBody – any object </li></ul>@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. 10. More... <ul><li>Interceptors </li></ul><ul><li>Exception handling </li></ul><ul><li>Binding and validation customization </li></ul><ul><li>Static resource handling </li></ul><ul><li>Caching configuration </li></ul><ul><li>Locale and theme resolution </li></ul><ul><li>Multipart handling </li></ul><ul><li>Flash scope, conversation scope (web flow) </li></ul>
  11. 11. The lifecycle once again Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html
  12. 12. Demo (spring mvc showcase)
  13. 13. 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
  14. 14. Questions ?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×