Spring 3 MVC CodeMash 2009

Post on 17-May-2015

31701 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

Transcript

NFJS Software Symposium Series 2009

Ken SipeTechnology Director, Perficient (PRFT)

Spring 2.5 MVC

NFJS Software Symposium Series 2009

Ken SipeTechnology Director, Perficient (PRFT)

Spring 2.5 MVC3.0

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

kensipe@gmail.comkensipe.blogspot.comhttp://del.icio.us/kensipetwitter: kensipe

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

Spring 3.0 MVC

Spring 3.0 - ADDAnnotated Driven Development

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

Spring 3.0 MVC

5

Introduction to IoC

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

Spring 3.0 MVC

5

Introduction to IoC

service dependency on datasource

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

OperationOperation

AttributeAttribute

AccountService

OperationOperation

AttributeAttribute

AccountDAO

<<uses>>AttributeAttribute

DataSource

<<uses>>

OperationOperation

AttributeAttribute

ApplicationContext

<creates>

Spring 3.0 MVC

6

Spring IoC

Client:

Spring 3.0 MVC

7

XML Configuration

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

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

@Service Intended to be a business service facade

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

Spring 3.0 MVC

Constructor

Setter

Field

12

Types of Injections

Spring 3.0 MVC

configuration method

with any number of arguments

13

New Injection Type

Spring 3.0 MVC

Spring MVC

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

Spring 3.0 MVC

MVC = Model - View - Controllerseparates:

business navigation presentation logic

Improves testability of logic and navigation

16

MVC Overview

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

Dispatcher Servlet

Controller

Model

DB

request request

data accessnavigation

view jsp

access

render

Spring 3.0 MVC

18

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

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

Spring 3.0 MVC

Extensive SupportJSP, Velocity, FreeMarker, JasperReportersPDF, Excel

Views are mapped by ViewResolvers

21

Spring Views

Spring 3.0 MVC

22

Spring View

Spring 3.0 MVC

23

Spring Controller Hierarchy

Spring 3.0 MVC

24

Spring MVC Old School

Spring 3.0 MVC

25

Spring MVC Old School

Spring 3.0 MVC

26

Spring MVC Old School - Working with Requests

Spring 3.0 MVC

DEMO: Spring MVC Old School

Spring 3.0 MVC

From Controller to @ControllerSpring 3 @MVC

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

Spring 3.0 MVC

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

30

New Controller Issues

Spring 3.0 MVC

31

Advantages of Controller Interfaces

Spring 3.0 MVC

31

It looks like your trying to build a controller

Advantages of Controller Interfaces

Spring 3.0 MVC

31

Advantages of Controller Interfaces

Spring 3.0 MVC

Return Type? Parameters?

32

A World Without Rules

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

Spring 3.0 MVC

34

Spring MVC Controller Evolution

mixed model: standard looking old school controller withoutan interface.

Spring 3.0 MVC

35

Spring MVC Controller Evolution

refactored method: inject the model, return the view

Spring 3.0 MVC

36

Spring MVC Controller Evolution

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

Spring 3.0 MVC

37

Spring MVC By Convention

GET /hotel/list

View selectedfrom request path

Added to Model

Conventions:hotel = HotelControllerlist = method

Spring 3.0 MVC

38

/hotel/index

/hotel/show

/hotel/list

Multi-Action Convention

Spring 3.0 MVC

39

/hotel/show?id=42

Parameters

Spring 3.0 MVC

Spring MVC Configurations

Spring 3.0 MVC

41

URL Autonomy

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

Web Application/Servlet/Controller/method

Spring 3.0 MVC

42

DispatcherServlet

Spring 3.0 MVC

42

DispatcherServlet

Spring 3.0 MVC

DefaultAnnotationHandlerMappingannotation based - @Controller

SimpleUrlHandlerMappingconfiguration basedstill works with annotations

ControllerClassNameHandlerMapping

UrlFileNameViewControllerviews without a controller

43

Handler Mappings

Spring 3.0 MVC

44

Mapping by Request Mapping

Spring 3.0 MVC

45

Mapping by Controller

Spring 3.0 MVC

46

Mapping by Convention

Spring 3.0 MVC

Favor @Controller Convention over configuration Group controller logic

47

Spring MVC Tips

Spring 3.0 MVC

48

Controller Class Name Conventions

/petclinic/main/owner/show

org...petclinic.Owner

Spring 3.0 MVC

49

Controller Class Name Conventions - Base Package

/petclinic/main/patient/owner/show

org...petclinic.Owner

Spring 3.0 MVC

50

Controller Class Name Conventions - Base Package

/petclinic/main/patient/owner/show

org...petclinic.Owner

prefix

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

Spring 3.0 MVC

52

Method-Relative Request Mapping

/owner, /owner/*

GET /owner/form

POST /owner/form

GET /owner/find

GET /owner/find?submit=

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=

Spring 3.0 MVCViews without Controllers

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

Spring 3.0 MVC

55

@RequestParam

type conversion

optional request parameter

TIP: for optional parameters use objects

GET /owner/show?submit=2

Spring 3.0 MVC

56

@PathVariable - RESTFUL

GET /owner/show/2

Spring 3.0 MVC

Automatically created on every request Implicit Model

57

Add Model Attributes

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

Spring 3.0 MVC

59

More Crazy Stuff

Annotation Access to:HeadersCookies

Spring 3.0 MVC

60

Working with Session

Session Attribute for “pet”

“pet” will be added to session

“pet” will be removed from session

Spring 3.0 MVC

Spring Forms

Spring 3.0 MVC

62

Model Attributes as Command Objects

Bind command object to form

Bind request to command object

Spring 3.0 MVC

63

Submitting Forms

Spring 3.0 MVC

Hibernate Validator JSR-303 Bean Validation

64

Model Validation

Spring 3.0 MVC

65

Validation Checks in Controller

Spring 3.0 MVC

DEMO: Spring 3.0 MVC

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

Summary

top related