spring mvc basics

Post on 08-May-2015

9.218 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

A short presentation about the basics of spring

TRANSCRIPT

Spring-MVC BasicsSpring-MVC Basics

Bozhidar Bozhanov

Bulgarian Association of Software Developers

www.devbg.org

http://techblog.bozho.net

MVCMVC

Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

ModelModel

• Map<String, Object>

• Populated by controllers

• Contains all data needed by the „view“

• Not containing business logic

model.addAttribute("orders", model.addAttribute("orders", orderService.getDailyOrders(user));orderService.getDailyOrders(user));model.addAttribute("period", Period.DAILY); model.addAttribute("period", Period.DAILY);

ViewView

• HTML, XML, RSS, PDF, etc...

• JSP, Freemarker, Velocity, etc

• Direct access to the model

<span><span>${latestOrder.date}${latestOrder.date}<span><span><input type="text" <input type="text" value="value="${order.name}${order.name}" />" />

ControllerController

• 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

ControllerController

• 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

ControllerController

@Controller @RequestMapping("/users")@Controller @RequestMapping("/users")publicpublic classclass UserController { UserController { @Inject@Inject privateprivate UserService UserService serviceservice;; @RequestMapping("/login")@RequestMapping("/login") publicpublic String login(@RequestParam String username, String login(@RequestParam String username, @RequestParam String pass){@RequestParam String pass){ User u = service.login(username, pass);User u = service.login(username, pass); return "welcomePage";return "welcomePage"; }} @RequestMapping("/view/{username}")@RequestMapping("/view/{username}") publicpublic String view(@PathVariable String username){ String view(@PathVariable String username){ User u = service.find(username);User u = service.find(username); ...... return "userPage";return "userPage"; }}}}

Controller argumentsController 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 typesControllers – return types

• String, View, ModelAndView – use a “view“

• @ResponseBody – any object

@Controller @RequestMapping("/users")@Controller @RequestMapping("/users")publicpublic classclass UserController { UserController { @Inject@Inject privateprivate UserService UserService serviceservice;; @RequestMapping(value="/ajaxView" @RequestMapping(value="/ajaxView" consumes="application/json")consumes="application/json") @ResponseBody@ResponseBody publicpublic User view(@RequestParam String username){ User view(@RequestParam String username){ User u = service.find(username);User u = service.find(username); return user;return user; }}}}

More...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 againThe lifecycle once again

Source: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

DemoDemo(spring mvc showcase)

ResourcesResources

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.htmlhttp://static.springsource.org/docs/petclinic.htmlhttps://github.com/SpringSource/spring-mvc-showcase

QuestionsQuestions??

top related