Вы находитесь на странице: 1из 73

NFJS Software Symposium Series 2009

Spring 2.5 MVC

Ken Sipe Technology Director, Perficient (PRFT)

NFJS Software Symposium Series 2009

Spring 2.5 MVC

3.0

Ken Sipe Technology Director, Perficient (PRFT)

Speaker Qualifications

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)

kensipe@gmail.com kensipe.blogspot.com http://del.icio.us/kensipe twitter: kensipe

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

Spring 3.0 MVC

Spring 3.0 - ADD


Annotated Driven Development

Introduction to IoC

Spring 3.0 MVC

AccountService Attribute Attribute Operation Operation

<<uses>>

AccountDAO Attribute Attribute Operation Operation

<<uses>>

DataSource Attribute Attribute

Introduction to IoC

Spring 3.0 MVC

AccountService Attribute Attribute Operation Operation

<<uses>>

AccountDAO Attribute Attribute Operation Operation

<<uses>>

DataSource Attribute Attribute

service dependency on datasource

Introduction to IoC

Spring 3.0 MVC

AccountService Attribute Attribute Operation Operation

<<uses>>

AccountDAO Attribute Attribute Operation Operation

<<uses>>

DataSource Attribute Attribute

<creates> DAOFactory Attribute Attribute Operation Operation

dependency simply moves

Spring IoC

Spring 3.0 MVC

AccountService Attribute Attribute Operation Operation

<<uses>>

AccountDAO Attribute Attribute Operation Operation

<<uses>>

DataSource Attribute Attribute

<creates>

ApplicationContext Attribute Attribute Operation Operation

Client:

Spring 3.0 MVC

XML Configuration

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 object is created, then values are set through each property setter object is created, then the values are (usually) reflectively set on the field (by passing the setter)
8

Setter

Field

Spring 3.0 MVC

Spring Annotations

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 Intended to be a business service facade

@Service

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

Types of Injections

Spring 3.0 MVC

Constructor

Setter

Field

12

New Injection Type

Spring 3.0 MVC

configuration method

with any number of arguments

13

Spring 3.0 MVC

Spring MVC

Spring Web

Spring 3.0 MVC

Spring Web MVC


foundation ajax

for all spring web modules

Spring JavaScript
support for stateful interactions

Spring Web Flow


framework JSF

Spring Faces
support jsr-168 and jsr-286 support
15

Spring Portlet
Portlet

MVC Overview

Spring 3.0 MVC

MVC = Model - View - Controller


separates:

business navigation presentation logic

Improves

testability of logic and navigation

16

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 logic

Controller
controlling

conjuror of model navigation validation

17

Spring 3.0 MVC

request Dispatcher Servlet

request Controller navigation

data access

render
DB

access view jsp Model

18

Spring MVC Taxonomy

Spring 3.0 MVC

DispatcherServlet
Spring

provided front controller Controls request routing

Controllers
User

created POJO Standard Spring beans

TIP: 1. Delegate to service beans for business logic 2. handle only navigation

View
Responsible

for rendering a response

19

Core MVC Components

Spring 3.0 MVC

ModelAndView
stores

model 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

Spring Views

Spring 3.0 MVC

Extensive Support
JSP,

Velocity, FreeMarker, JasperReporters PDF, Excel

Views are mapped by ViewResolvers

21

Spring View

Spring 3.0 MVC

22

Spring Controller Hierarchy

Spring 3.0 MVC

23

Spring MVC Old School

Spring 3.0 MVC

24

Spring MVC Old School

Spring 3.0 MVC

25

Spring MVC Old School - Working with Requests

Spring 3.0 MVC

26

Spring 3.0 MVC

DEMO: Spring MVC Old School

Spring 3.0 MVC

From Controller to @Controller


Spring 3 @MVC

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 Marks session attributes that a handler uses

SessionAttributes

New Controller Issues

Spring 3.0 MVC

Doesnt implement an Interface Multiple request mappings High degree of flexibility

30

Advantages of Controller Interfaces

Spring 3.0 MVC

31

Advantages of Controller Interfaces

Spring 3.0 MVC

It looks like your trying to build a controller

31

Advantages of Controller Interfaces

Spring 3.0 MVC

31

A World Without Rules

Spring 3.0 MVC

Return Type? Parameters?

32

Spring MVC Parameters and Return Types

Spring 3.0 MVC

Parameters can be

Request / response / session WebRequest InputStream OutputStream @RequestParam +++ ModelAndView Object Model Object Map for exposing model View Object String which is a view name Void if method wrote the response content directly

Return types

Spring MVC Controller Evolution

Spring 3.0 MVC

mixed model: standard looking old school controller without an interface.


34

Spring MVC Controller Evolution

Spring 3.0 MVC

refactored method: inject the model, return the view


35

Spring MVC Controller Evolution

Spring 3.0 MVC

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

Spring MVC By Convention

Spring 3.0 MVC

GET /hotel/list View selected from request path

Conventions: hotel = HotelController list = method

Added to Model

37

Multi-Action Convention

Spring 3.0 MVC

/hotel/index

/hotel/show

/hotel/list

38

Parameters

Spring 3.0 MVC

/hotel/show?id=42

39

Spring 3.0 MVC

Spring MVC Configurations

URL Autonomy

Spring 3.0 MVC

http://<host>:<port>/petclinic/main/owner/show
Web Application/Servlet/Controller/method

41

DispatcherServlet

Spring 3.0 MVC

42

DispatcherServlet

Spring 3.0 MVC

42

Handler Mappings

Spring 3.0 MVC

DefaultAnnotationHandlerMapping
annotation

based - @Controller

SimpleUrlHandlerMapping
configuration

based still works with annotations


ControllerClassNameHandlerMapping UrlFileNameViewController
views

without a controller
43

Mapping by Request Mapping

Spring 3.0 MVC

44

Mapping by Controller

Spring 3.0 MVC

45

Mapping by Convention

Spring 3.0 MVC

46

Spring MVC Tips

Spring 3.0 MVC

Favor @Controller Convention over configuration Group controller logic

47

Controller Class Name Conventions

Spring 3.0 MVC

/petclinic/main/owner/show
org...petclinic.Owner

48

Controller Class Name Conventions - Base Package

Spring 3.0 MVC

/petclinic/main/patient/owner/show
org...petclinic.Owner

49

Controller Class Name Conventions - Base Package

Spring 3.0 MVC

/petclinic/main/patient/owner/show
prefix org...petclinic.Owner

50

@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

Method-Relative Request Mapping

Spring 3.0 MVC

/owner, /owner/*

GET /owner/form POST /owner/form GET /owner/find

GET /owner/find?submit=

52

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

Views without Controllers

Spring 3.0 MVC

Some views dont review a controller

/index.htm -> index

@RequestParam

Spring 3.0 MVC

type conversion

optional request parameter

TIP: for optional parameters use objects

GET

/owner/show?submit=2

55

@PathVariable - RESTFUL

Spring 3.0 MVC

GET

/owner/show/2

56

Add Model Attributes

Spring 3.0 MVC

Automatically created on every request Implicit Model

57

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

More Crazy Stuff

Spring 3.0 MVC

Annotation Access to:


Headers Cookies

59

Working with Session

Spring 3.0 MVC

Session Attribute for pet

pet will be added to session

pet will be removed from session

60

Spring 3.0 MVC

Spring Forms

Model Attributes as Command Objects

Spring 3.0 MVC

Bind command object to form

Bind request to command object

62

Submitting Forms

Spring 3.0 MVC

63

Model Validation

Spring 3.0 MVC

Hibernate Validator JSR-303 Bean Validation

64

Validation Checks in Controller

Spring 3.0 MVC

65

Spring 3.0 MVC

DEMO: Spring 3.0 MVC

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

Вам также может понравиться