spring 3 mvc codemash 2009

73
NFJS Software Symposium Series 2009 Ken Sipe Technology Director, Perficient (PRFT) Spring 2.5 MVC

Upload: kensipe

Post on 17-May-2015

31.703 views

Category:

Technology


5 download

DESCRIPTION

Spring 3 MVC presented at Codemash 2009. If you want the source code send an email to kensipe at gmail

TRANSCRIPT

Page 1: Spring 3 MVC   CodeMash 2009

NFJS Software Symposium Series 2009

Ken SipeTechnology Director, Perficient (PRFT)

Spring 2.5 MVC

Page 2: Spring 3 MVC   CodeMash 2009

NFJS Software Symposium Series 2009

Ken SipeTechnology Director, Perficient (PRFT)

Spring 2.5 MVC3.0

Page 3: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

SOA/Enterprise Architect Developer (Java, C++, C#, Objective-C) Instructor (VisiBroker, RUP, OOAD) Speaker (NFJS, JavaOne, JAX-India) Involved in Java since 1996 Author (Pro Spring 3.0)

Speaker Qualifications

[email protected]://del.icio.us/kensipetwitter: kensipe

Page 4: Spring 3 MVC   CodeMash 2009

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

Agenda

Page 5: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring 3.0 - ADDAnnotated Driven Development

Page 6: Spring 3 MVC   CodeMash 2009

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

Spring 3.0 MVC

5

Introduction to IoC

Page 7: Spring 3 MVC   CodeMash 2009

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

Spring 3.0 MVC

5

Introduction to IoC

service dependency on datasource

Page 8: Spring 3 MVC   CodeMash 2009

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

OperationOperation

AttributeAttribute

DAOFactory

<creates>

Spring 3.0 MVC

5

Introduction to IoC

dependency simply moves

Page 9: Spring 3 MVC   CodeMash 2009

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

OperationOperation

AttributeAttribute

ApplicationContext

<creates>

Spring 3.0 MVC

6

Spring IoC

Client:

Page 10: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

7

XML Configuration

Page 11: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

There are 3 types of dependency injections in the worldConstructor

values are injected through the constructor at object construction

Setter object is created, then values are set through each

property setterField

object is created, then the values are (usually) reflectively set on the field (by passing the setter)

8

Types of Injections

Page 12: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring Annotations

Page 13: Spring 3 MVC   CodeMash 2009

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

Page 14: Spring 3 MVC   CodeMash 2009

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

Page 15: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Constructor

Setter

Field

12

Types of Injections

Page 16: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

configuration method

with any number of arguments

13

New Injection Type

Page 17: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring MVC

Page 18: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring Web MVC foundation for all spring web modules

Spring JavaScriptajax support

Spring Web Flow framework for stateful interactions

Spring FacesJSF support

Spring PortletPortlet jsr-168 and jsr-286 support

15

Spring Web

Page 19: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

MVC = Model - View - Controllerseparates:

business navigation presentation logic

Improves testability of logic and navigation

16

MVC Overview

Page 20: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Modeldata 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

MVC on the Web

Page 21: Spring 3 MVC   CodeMash 2009

Dispatcher Servlet

Controller

Model

DB

request request

data accessnavigation

view jsp

access

render

Spring 3.0 MVC

18

Page 22: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

DispatcherServletSpring provided front controllerControls request routing

ControllersUser created POJOStandard Spring beans

ViewResponsible for rendering a response

19

Spring MVC Taxonomy

TIP: 1. Delegate to service beansfor business logic2. handle only navigation

Page 23: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

ModelAndViewstores model dataassociates a view to the request

can be a view implementation or a logical name

ViewResolverUsed to map logical view names to view

implementations HandlerMapping

Strategy interfaceUsed by DispatcherServlet to map requests to

controllers20

Core MVC Components

Page 24: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Extensive SupportJSP, Velocity, FreeMarker, JasperReportersPDF, Excel

Views are mapped by ViewResolvers

21

Spring Views

Page 25: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

22

Spring View

Page 26: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

23

Spring Controller Hierarchy

Page 27: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

24

Spring MVC Old School

Page 28: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

25

Spring MVC Old School

Page 29: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

26

Spring MVC Old School - Working with Requests

Page 30: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

DEMO: Spring MVC Old School

Page 31: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

From Controller to @ControllerSpring 3 @MVC

Page 32: Spring 3 MVC   CodeMash 2009

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

Spring MVC Annotations

Page 33: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Doesn’t implement an Interface Multiple request mappings High degree of flexibility

30

New Controller Issues

Page 34: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

31

Advantages of Controller Interfaces

Page 35: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

31

It looks like your trying to build a controller

Advantages of Controller Interfaces

Page 36: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

31

Advantages of Controller Interfaces

Page 37: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Return Type? Parameters?

32

A World Without Rules

Page 38: Spring 3 MVC   CodeMash 2009

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

Spring MVC Parameters and Return Types

Page 39: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

34

Spring MVC Controller Evolution

mixed model: standard looking old school controller withoutan interface.

Page 40: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

35

Spring MVC Controller Evolution

refactored method: inject the model, return the view

Page 41: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

36

Spring MVC Controller Evolution

another refactor: just return the model data, view is assumed through convention.

Page 42: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

37

Spring MVC By Convention

GET /hotel/list

View selectedfrom request path

Added to Model

Conventions:hotel = HotelControllerlist = method

Page 43: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

38

/hotel/index

/hotel/show

/hotel/list

Multi-Action Convention

Page 44: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

39

/hotel/show?id=42

Parameters

Page 45: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring MVC Configurations

Page 46: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

41

URL Autonomy

http://<host>:<port>/petclinic/main/owner/show

Web Application/Servlet/Controller/method

Page 47: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

42

DispatcherServlet

Page 48: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

42

DispatcherServlet

Page 49: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

DefaultAnnotationHandlerMappingannotation based - @Controller

SimpleUrlHandlerMappingconfiguration basedstill works with annotations

ControllerClassNameHandlerMapping

UrlFileNameViewControllerviews without a controller

43

Handler Mappings

Page 50: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

44

Mapping by Request Mapping

Page 51: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

45

Mapping by Controller

Page 52: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

46

Mapping by Convention

Page 53: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Favor @Controller Convention over configuration Group controller logic

47

Spring MVC Tips

Page 54: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

48

Controller Class Name Conventions

/petclinic/main/owner/show

org...petclinic.Owner

Page 55: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

49

Controller Class Name Conventions - Base Package

/petclinic/main/patient/owner/show

org...petclinic.Owner

Page 56: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

50

Controller Class Name Conventions - Base Package

/petclinic/main/patient/owner/show

org...petclinic.Owner

prefix

Page 57: Spring 3 MVC   CodeMash 2009

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

@RequestMapping

Page 58: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

52

Method-Relative Request Mapping

/owner, /owner/*

GET /owner/form

POST /owner/form

GET /owner/find

GET /owner/find?submit=

Page 59: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

53

View Name Conventions

Show owner:

Add or edit owner:

Search owner:

GET /owner/show

GET /owner/formGET /owner/form?id=POST /owner/form

GET /owner/findGET /owner/find?submit=

Page 60: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVCViews without Controllers

Some views don’t review a controller /index.htm -> “index”

Page 61: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

55

@RequestParam

type conversion

optional request parameter

TIP: for optional parameters use objects

GET /owner/show?submit=2

Page 62: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

56

@PathVariable - RESTFUL

GET /owner/show/2

Page 63: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Automatically created on every request Implicit Model

57

Add Model Attributes

Page 64: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

58

Adding a Single Model Attribute

Implicit Model

@ModelAttribute - customize the name

TIP: If you need more than one modelattribute, take model as a input argument

Page 65: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

59

More Crazy Stuff

Annotation Access to:HeadersCookies

Page 66: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

60

Working with Session

Session Attribute for “pet”

“pet” will be added to session

“pet” will be removed from session

Page 67: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Spring Forms

Page 68: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

62

Model Attributes as Command Objects

Bind command object to form

Bind request to command object

Page 69: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

63

Submitting Forms

Page 70: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

Hibernate Validator JSR-303 Bean Validation

64

Model Validation

Page 71: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

65

Validation Checks in Controller

Page 72: Spring 3 MVC   CodeMash 2009

Spring 3.0 MVC

DEMO: Spring 3.0 MVC

Page 73: Spring 3 MVC   CodeMash 2009

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 – [email protected]

Summary