4. Agenda Spring 3.0 MVC
Agenda
Spring 3.0 - ADD
Spring MVC
From Controller to @Controller
Controller Mapping
URL Mapping
Request Parameters
Model Attributes
Spring Forms
Summary
11. Types of Injections Spring 3.0 MVC
There are 3 types of dependency
injections in the world
Constructor
values are injected through the constructor at
object construction
Setter
object is created, then values are set through each
property setter
Field
object is created, then the values are (usually)
reflectively set on the field (by passing the setter)
8
13. Spring 3.0 MVC
@Component **
Indicates that a class is a component
Class is a candidate for auto-detection
Custom component extensions
@Controller
Specialized Component
Typically used with RequestMapping annotation
Discussed in section on web mvc
@Repository
Now an extension of @Component
@Service
Intended to be a business service facade
14. Spring 3.0 MVC
@Autowired
Marks a constructor, field, setter or config method for injection.
Fields are injected
After construction
@Autowired(required=false)
@Qualifier
Qualifies a bean for autowiring
May be customized
@Required
Marks a method as being injection required
18. Spring Web Spring 3.0 MVC
Spring Web MVC
foundation for all spring web modules
Spring JavaScript
ajax support
Spring Web Flow
framework for stateful interactions
Spring Faces
JSF support
Spring Portlet
Portlet jsr-168 and jsr-286 support
15
19. MVC Overview Spring 3.0 MVC
MVC = Model - View - Controller
separates:
business
navigation
presentation logic
Improves testability of logic and navigation
16
20. MVC on the Web Spring 3.0 MVC
Model
data container
displayed by the view
manipulated by the controller
commonly a simple map
View
typically jsp or xml
Controller
controlling logic
conjuror of model
navigation
validation 17
21. Spring 3.0 MVC
request request
Dispatcher
Controller
Servlet
navigation
data access
render
DB
access
view jsp
Model
18
22. Spring MVC Taxonomy Spring 3.0 MVC
DispatcherServlet
Spring provided front controller
Controls request routing
Controllers
TIP:
User created POJO 1. Delegate to service beans
for business logic
Standard Spring beans 2. handle only navigation
View
Responsible for rendering a response
19
23. Core MVC Components Spring 3.0 MVC
ModelAndView
storesmodel data
associates a view to the request
can be a view implementation or a logical name
ViewResolver
Used to map logical view names to view
implementations
HandlerMapping
Strategy interface
Used by DispatcherServlet to map requests to
controllers
20
24. Spring Views Spring 3.0 MVC
Extensive Support
JSP,Velocity, FreeMarker, JasperReporters
PDF, Excel
Views are mapped by ViewResolvers
21
32. Spring MVC Annotations Spring 3.0 MVC
@Controller
Stereotype used to “Controller” of MVC
Scanned for RequestMappings
@RequestMapping
Annotates a handler method for a request
Very flexible
@RequestParam
Annotates that a method parameter should be bound to a web request
parameter
SessionAttributes
Marks session attributes that a handler uses
33. New Controller Issues Spring 3.0 MVC
Doesn’t implement an Interface
Multiple request mappings
High degree of flexibility
30
37. A World Without Rules Spring 3.0 MVC
Return Type?
Parameters?
32
38. Spring MVC Parameters and Return Types Spring 3.0 MVC
Parameters can be
Request / response / session
WebRequest
InputStream
OutputStream
@RequestParam
+++
Return types
ModelAndView Object
Model Object
Map for exposing model
View Object
String which is a view name
Void… if method wrote the response content directly
39. Spring MVC Controller Evolution Spring 3.0 MVC
mixed model: standard looking old school controller without
an interface.
34
40. Spring MVC Controller Evolution Spring 3.0 MVC
refactored method: inject the model, return the view
35
41. Spring MVC Controller Evolution Spring 3.0 MVC
another refactor: just return the model data, view is assumed through
convention.
36
42. Spring MVC By Convention Spring 3.0 MVC
Conventions:
hotel = HotelController
GET /hotel/list list = method
View selected
from request
path
Added to
Model
37
49. Handler Mappings Spring 3.0 MVC
DefaultAnnotationHandlerMapping
annotation based - @Controller
SimpleUrlHandlerMapping
configuration based
still works with annotations
ControllerClassNameHandlerMapping
UrlFileNameViewController
views without a controller
43
53. Spring MVC Tips Spring 3.0 MVC
Favor @Controller
Convention over configuration
Group controller logic
47
54. Controller Class Name Conventions Spring 3.0 MVC
/petclinic/main/owner/show
org...petclinic.Owner
48
55. Controller Class Name Conventions - Base Package Spring 3.0 MVC
/petclinic/main/patient/owner/show
org...petclinic.Owner
49
56. Controller Class Name Conventions - Base Package Spring 3.0 MVC
/petclinic/main/patient/owner/show
prefix
org...petclinic.Owner
50
57. @RequestMapping Spring 3.0 MVC
Order of method selection:
First by request method (GET, POST, PUT,
DELETE)
Second by parameter presence/absense, or
parameter value
third by method name
fourth by URL
51
58. Method-Relative Request Mapping Spring 3.0 MVC
/owner, /owner/*
GET /owner/form
POST /owner/form
GET /owner/find
GET /owner/find?submit=
52
59. View Name Conventions Spring 3.0 MVC
Show owner:
GET /owner/show
Add or edit owner:
GET /owner/form
GET /owner/form?id=
POST /owner/form
Search owner:
GET /owner/find
GET /owner/find?submit=
53
60. Views without Controllers Spring 3.0 MVC
Some views don’t review a controller
/index.htm -> “index”
61. @RequestParam Spring 3.0 MVC
type conversion
optional request parameter
TIP: for optional parameters use objects
GET /owner/show?submit=2
55
63. Add Model Attributes Spring 3.0 MVC
Automatically created on every request
Implicit Model
57
64. Adding a Single Model Attribute Spring 3.0 MVC
Implicit Model
@ModelAttribute - customize the name
TIP: If you need more than one model
attribute, take model as a input argument
58
65. More Crazy Stuff Spring 3.0 MVC
Annotation Access to:
Headers
Cookies
59
66. Working with Session Spring 3.0 MVC
Session Attribute for “pet”
“pet” will be added to session
“pet” will be removed from
session
60
73. Summary Spring 3.0 MVC
Closing and Q&A
References
Spring Framework: http://www.springsource.com/download/
community?project=Spring%20Framework
Spring Docs: http://www.springsource.org/documentation
Session Source Code: http://groups.google.com/group/codemash
Please fill out the session evaluation
Ken Sipe – kensipe@gmail.com