An Introduction to the Spring Framework - PowerPoint PPT Presentation

1 / 83
About This Presentation
Title:

An Introduction to the Spring Framework

Description:

the origins of the Spring Framework itself arose partly as ... Last Update: 5/15/06. 11. public class CampaignServiceImpl ... id (or name) attribute to ... – PowerPoint PPT presentation

Number of Views:418
Avg rating:3.0/5.0
Slides: 84
Provided by: dougl117
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to the Spring Framework


1
An Introduction to the Spring Framework
  • Introduction to Dependency Injection and
  • Web-based Applications using Spring

Douglas Johnson Sept. 2007
2
Agenda
  • What is Spring?
  • Introduction Spring Dependency Injection / IoC
  • Spring Web Integration Introduction
  • The Model-View-Controller Design Pattern
  • Flow and Configuration of Spring MVC
  • Artifact Basics
  • Spring MVC Run-time Flow
  • Demonstration 1 Configuration Basics
  • Web Application Testing
  • Views from Spring
  • JSTL Introduction
  • Data to html mapping and Spring TagLib
  • Demonstration 2 HTML Mapping and Spring Tag
    Library
  • Sessions, Themes, and Localization Options
  • Advanced Views
  • Demonstration 3 Excel and PDF Samples

3
What is the Spring Framework?
4
Spring Framework History
  • Started 2002/2003 by Rod Johnson and Juergen
    Holler
  • Started as a framework developed around Rod
    Johnsons book Expert One-on-One J2EE Design and
    Development
  • Spring 1.0 Released March 2004, now at 2.0
  • 2004/2005 Spring is emerging as a leading
    full-stack Java/J2EE application framework

the origins of the Spring Framework itself arose
partly as a response to the complexity of
Enterprise JavaBeans
Source Rob-Lambert
5
What is Spring?
What is the Spring Framework?
  • A lightweight non-intrusive framework which
    addresses various tiers in a J2EE application.
  • Helps integrates tiers together using XML
    configuration instead of hard-coding.
  • Substantially reduces code, speeds up
    development, facilitates easy testing and
    improves code quality.
  • A lightweight non-intrusive framework which
    addresses Persistence layer (DAO template support
    for Hibernate, SQLMaps and JDBC)
  • Factory implementations for (to abstract and
    integrate various other facets of enterprise
    applications) E-mails, JMS, WebServices, etc.
  • Presentation layer (various Web technologies
    Struts, JSTL, Tapestry, excel, pdf)
  • Business layer (Lightweight IoC container with
    support for AOP-driven interceptors and
    transaction)

Source Ram A. Rao
6
The Spring Stack
What is the Spring Framework?
7
The Benefits of Spring
What is the Spring Framework?
  • Removes common code issues like leaking
    connections and more
  • Support for declarative transaction management
  • Easy integration with third party tools and
    technologies
  • Not a J2EE container (JEE)
  • Doesnt compete with J2EE app servers. Simply
    provides alternatives.
  • POJO-based
  • Non-invasive allowing a la carte usage of its
    components.
  • Promotes decoupling and reusability
  • Reduces coding effort and enforces design
    discipline
  • Out-of-box implicit patterns singleton, factory,
    service locator ,etc.

Source Ram A. Rao
8
IntroductionSpring Dependency Injection / IoC
9
Dependency Injection and IoC
Introduction Spring Dependency Injection / IoC
  • Beans define their dependencies through
    constructor arguments or properties
  • Prevents hard-coded object creation and
    object/service lookup
  • Loose coupling
  • Supports effective unit tests
  • Instead of objects invoking other objects, the
    dependant objects are added through an external
    entity/container.
  • Also known as the Hollywood principle dont
    call me I will call you
  • Dependency injection dependencies are injected
    spring container at runtime.

Source Ram A. Rao
10
Non IoC / Dependency Injection
Introduction Spring Dependency Injection / IoC
Source Spring Documentation
11
Non IoC Object Service
Introduction Spring Dependency Injection / IoC
  • public class CampaignServiceImpl implements
    CampaignService
  • public Campaign updateCampaign(Campaign
    campaign) throws CampaignException
  • try
  • CampaignDAO campaign new CampaignDAOImpl()
  • ..
  • OpportunityDAO oppDAO new OpportunityDAOImpl(
    )
  • // Alternatively, initialize thru factory
  • // OpportunityDAO oppDAO OppFactory.getOpp()
  • oppDAO.save(campaign)
  • ..
  • catch(Exception e)

Code is tightly coupled or interspersed with
Factory code!
Source Ram A. Rao
12
IoC / Dependency Injection
Introduction Spring Dependency Injection / IoC
Source Spring Documentation
13
IoC Service Object
Introduction Spring Dependency Injection / IoC
public class CampaignServiceImpl implements
CampaignService public Campaign
updateCampaign(Campaign campaign) throws
CampaignException try oppDAO.save(campaign)
catch(Exception e) // Spring sets
the value thru runtime injection! private
setOpportunityDAO(OpportunityDAO oppDao)
this.oppDAO oppDao
Source Ram A. Rao
14
Applied Dependency Injection
Introduction Spring Dependency Injection / IoC
  • BeanFactories are the heart of Spring
  • A BeanFactory is typically configured in an XML
    file with the root element ltbeansgt
  • The XML contains one or more ltbeangt elements
  • id (or name) attribute to identify the bean
  • class attribute to specify the fully qualified
    class

Source Rob-Lambert
15
BeanFactories
Introduction Spring Dependency Injection / IoC
  • By default, beans are treated as singletons, an
    also be prototypes
  • Example

ltbeansgt ltbean idwidgetService
classcom.zabada.base.WidgetServicegt
ltproperty namepoolSizegt lt!-property
value here--gt lt/propertygt lt/beangt lt/beansgt
Source Rob-Lambert
16
Property Values for BeanFactories
  • Strings and Numbers
  • Arrays and Collections

ltproperty namesizegtltvaluegt42lt/valuegtlt/propertygt
ltproperty namenamegtltvaluegtJimlt/valuegtlt/property
gt
ltproperty namehobbiesgt ltlistgt
ltvaluegtBasket Weavinglt/valuegt ltvaluegtBreak
Dancinglt/valuegt lt/listgt lt/propertygt
Source Rob-Lambert
17
Property Values for BeanFactories (continued)
  • The real magic comes in when you can set a
    property on a bean that refers to another bean in
    the configuration

ltbean namewidgetService classcom.zabada.base.
WidgetServiceImplgt ltproperty
namewidgetDAOgt ltref beanmyWidgetDAO/gt
lt/propertygt lt/beangt
calls setWidgetDAO(myWidgetDAO) where myWidgetDAO
is another bean defined in the configuration
Source Rob-Lambert
18
Dependency Injection(Inversion of Control)
  • Complicated sounding terms for a fairly simple
    concept
  • The Hollywood Principle Dont call me, Ill
    call you
  • Dependencies used from within a bean arent asked
    for outwardly, but are injected into the bean by
    the container

Source Rob-Lambert
19
Dependency Injection(Inversion of Control)
  • Eliminates lookup code from within your
    application
  • Allows for pluggablity and hot swapping
  • Promotes good OO design
  • Enables reuse of existing code
  • Makes your application extremely testable

Source Rob-Lambert
20
A Very Special BeanFactorythe ApplicationContext
  • An ApplicationContext is a BeanFactory, but adds
    framework features such as
  • i18n messages
  • Event notifications
  • This is what you will probably most often use in
    your Spring applications

Source Rob-Lambert
21
Spring Web Integration Introduction
22
Overview
Overview of Model-View-Controller (MVC) Design
Pattern
  • What is MVC?
  • Design pattern, philosophical approach
  • Development time tools, techniques for adhering
    to or using the pattern
  • Goals
  • Isolate UI changes, prevent them from requiring
    changes to an Applications Domain Logic

http//www.phpwact.org/pattern/model_view_controll
er
23
Overview
Overview of Model-View-Controller (MVC) Design
Pattern
  • Model
  • The domain-specific representation of the
    information on which the application operates.
  • View
  • Renders the model into a form suitable for
    interaction, typically a user interface element.
  • Controller
  • Processes and responds to events, typically user
    actions, and may invoke changes on the model.

Controller
View
Model
Note the solid lines indicate a direct
association, and the dashed line indicate an
indirect association
http//en.wikipedia.org/wiki/Model-view-controller
24
Flow and Configuration of Spring MVC
25
Artifact Basics
Flow and Configuration of Spring MVC
  • Web.xml Entry
  • Standard J2EE web container servlet definition
  • Establishes which and where listeners act on
    requests
  • Bean Definitions
  • servlet-name-servlet.xml
  • Beans unique to the under the web context
    created for the configured Servlet dispatcher
  • ApplicationContext.xml
  • Beans common to all contexts

26
Artifact Basics Web.xml
Flow and Configuration of Spring MVC
  • webapp\WEB-INF\web.xml

web.xml ltweb-appgt ltservletgt
ltservlet-namegtdispatcherlt/servlet-namegt
ltservlet-classgt org.springframework.web.serv
let.DispatcherServlet lt/servlet-classgt
ltload-on-startupgt1lt/load-on-startupgt
lt/servletgt ltservlet-mappinggt
ltservlet-namegtdispatcherlt/servlet-namegt
lturl-patterngt/hello/world/lt/url-patterngt
lt/servlet-mappinggt lt/web-appgt
Standard Servlet
Standard URL filtering
27
Artifact Basics Web.xml
Flow and Configuration of Spring MVC
  • webapp\WEB-INF\web.xml

Servlet that dispatches request to registered
Controller implementations.
web.xml ltweb-appgt ltservletgt
ltservlet-namegtdispatcherlt/servlet-namegt
ltservlet-classgt org.springframework.web.serv
let.DispatcherServlet lt/servlet-classgt
ltload-on-startupgt1lt/load-on-startupgt
lt/servletgt ltservlet-mappinggt
ltservlet-namegtdispatcherlt/servlet-namegt
lturl-patterngt/hello/world/lt/url-patterngt
lt/servlet-mappinggt lt/web-appgt
Has its own application context (default defined
in "servlet-name-servlet.xml)
A web app can contain any number of such
Servlets.
28
Artifact Basics Context Hierarchy
Flow and Configuration of Spring MVC
  • web.xml
  • ApplicationContext.xml
  • servlet-name-servlet.xml

From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
29
Spring MVC Run-time Flow
Flow and Configuration of Spring MVC
http//www.theserverside.com/tt/articles/article.t
ss?lAjaxandSpring
30
Spring MVC Run-time Flow
Flow and Configuration of Spring MVC
Dispatcher
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
31
Spring MVC Flow Pre Dispatcher
Flow and Configuration of Spring MVC
Dispatcher
Web.xml finds a servlet (dispatcher) mapped (and
filtered) to it.
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
32
Spring MVC Flow Pre Dispatcher
Flow and Configuration of Spring MVC
web-app/WEB-INF/web.xml
Dispatcher
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
33
Spring MVC Flow Pre Dispatcher
Flow and Configuration of Spring MVC
161922408 - INFO - HttpServletBean -
Initializing servlet 'dispatcher' 161922408 -
INFO - FrameworkServlet - FrameworkServlet
'dispatcher' initialization started 161922423
- INFO - XmlBeanDefinitionReader - Loading XML
bean definitions from ServletContext resource
/WEB-INF/report-servlet.xml 161922455 - INFO
- AbstractRefreshableApplicationContext - Bean
factory for application context
WebApplicationContext for namespace
'report-servlet' org.springframework.beans.facto
ry.support.DefaultListableBeanFactory defining
beans productStatusController,ProductStatusAsXMLV
iew,urlMapping,viewResolver,messageSource
parent org.springframework.beans.factory.support.
DefaultListableBeanFactory defining beans
productManager,product1,product2,product3 root
of BeanFactory hierarchy 161922455 - INFO -
AbstractApplicationContext - 5 beans defined in
application context WebApplicationContext for
namespace 'report-servlet' 161922455 - INFO -
AbstractApplicationContext - Using MessageSource
org.springframework.context.support.ResourceBundl
eMessageSource basenamesspring2webexamples.bus.
report.web.messages 161922455 - INFO -
AbstractApplicationContext - Unable to locate
ApplicationEventMulticaster with name
'applicationEventMulticaster' using default
org.springframework.context.event.SimpleApplicati
onEventMulticaster_at_45b199 161922455 - INFO -
UiApplicationContextUtils - Unable to locate
ThemeSource with name 'themeSource' using
default org.springframework.ui.context.support.De
legatingThemeSource_at_18ec669 161922470 - INFO
- DefaultListableBeanFactory - Pre-instantiating
singletons in factory productStatusController,P
roductStatusAsXMLView 161922517 - INFO -
FrameworkServlet - Using context class
org.springframework.web.context.support.XmlWebApp
licationContext for servlet 'report' 161922517
- INFO - DispatcherServlet - Unable to locate
MultipartResolver with name 'multipartResolver'
no multipart request handling provided 16192251
7 - INFO - DispatcherServlet - Unable to locate
LocaleResolver with name 'localeResolver' using
default org.springframework.web.servlet.i18n.Acce
ptHeaderLocaleResolver_at_ec459a 161922517 -
INFO - DispatcherServlet - Unable to locate
ThemeResolver with name 'themeResolver' using
default org.springframework.web.servlet.theme.Fix
edThemeResolver_at_1cd9466 161922517 - INFO -
DispatcherServlet - No HandlerAdapters found in
servlet 'dispatcher' using default 161922517
- INFO - DispatcherServlet - Unable to locate
RequestToViewNameTranslator with name
'viewNameTranslator' using default
org.springframework.web.servlet.view.DefaultReque
stToViewNameTranslator_at_6a765 161922517 - INFO
- FrameworkServlet - FrameworkServlet
'dispatcher' initialization completed in 109
ms 161922517 - INFO - HttpServletBean -
Servlet 'dispatcher' configured successfully
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
34
Spring MVC Flow Dispatcher
Flow and Configuration of Spring MVC
Dispatcher
Handler Mapping are reviewed as to which
Controller is to be invoked. Any defined and
implemented HandlerInterceptor.preHandle() calls
are made.
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
35
Spring MVC Flow Dispatcher
Flow and Configuration of Spring MVC
Dispatcher
  • web-app/WEB-INF/servlet-name-servlet.xml

From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
36
Handler Mappings
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
  • The process of Handler Mapping binds incoming web
    requests to appropriate handlers that then
    resolve to a Controller(s) (in most cases)
  • On inbound requests, the DispatcherServlet hands
    it over to the handler mapping to come up with an
    appropriate HandlerExecutionChain
  • Note Handler Mappings apply from the base
    url-pattern specified in the Web.xml
  • By default, if no handler mapping can be found in
    the context, the DispatcherServlet creates a
    BeanNameUrlHandlerMapping for you

http//static.springframework.org/spring/docs/2.0.
x/api/org/springframework/web/servlet/HandlerMappi
ng.html
37
Out of the box HandlerMapping
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
  • Concrete Implementations
  • SimpleUrlHandlerMapping
  • Map Ant-style path matching (see
    org.springframework.util.PathMatcher) to a
    Controller
  • BeanNameUrlHandlerMapping
  • maps incoming HTTP requests to names of beans,
    defined in the web application context
  • CommonsPathMapHandlerMapping
  • ControllerClassNameHandlerMapping
  • Most often, the out-of-the box implementations
    are sufficient

38
Anecdote Under the covers Mapping Execution
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
39
Anecdote Under the covers Mapping Execution
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
How does HandlerExecutionChain actually handle a
mapping to a Controller?
HttpRequestHandlerAdapter SimpleControllerHandlerA
dapter SimpleServletHandlerAdapter ThrowawayContro
llerHandlerAdapter
40
Use of Interceptors
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
  • Interceptors are useful for common services
    security, traffic logging, and perhaps front-end
    common data validation
  • Implement the HandlerInterceptor Interface
  • Simply wire your implemented Interceptors into
    the HandlerMapping
  • Out of the-box implementations exist
  • Locale, Theme, more

41
Use of Interceptors
Flow and Configuration of Spring MVC Spring MVC
Flow Dispatcher
42
Spring MVC Flow Controller
Flow and Configuration of Spring MVC
Dispatcher
Dispatcher
Controller is called. Model objects collected
(or instantiated) and a View is chosen.
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
43
MVC Give me a C
Flow and Configuration of Spring MVC Spring MVC
Flow Controller
  • Controllers
  • Provide access to the application behavior which
    is typically defined by a service interface
  • Controllers interpret input and transform said
    input into a sensible model which will be
    represented for output by the view

http//static.springframework.org/spring/docs/2.0.
x/reference/mvc.htmlmvc-controller
44
MVC Give me a C
Flow and Configuration of Spring MVC Spring MVC
Flow Controller
  • The Controller in Spring is very abstract,
    allowing different kinds of controllers for
    different use cases.
  • Out of the box, Controllers existing to
    facilitate working with User driven Forms,
    command models, execute wizard-style logic, and
    more
  • Role your own Controllers
  • Common data handling, control logic, etc.

http//static.springframework.org/spring/docs/2.0.
x/reference/mvc.htmlmvc-controller
45
The Simple Controller Interface
Flow and Configuration of Spring MVC Spring MVC
Flow Controller
http//static.springframework.org/spring/docs/2.0.
x/api/org/springframework/web/servlet/mvc/Controll
er.html
46
Out of the box Controllers
Flow and Configuration of Spring MVC Spring MVC
Flow Controller
  • Infrastructure
  • AbstractController
  • Command Controllers
  • AbstractCommandController
  • BaseCommandController
  • Form
  • SimpleFormController
  • AbstractFormController
  • AbstractWizardFormController
  • CancellableFormController
  • Specialized
  • MultiActionController
  • ServletWrappingController
  • Resource
  • ParameterizableViewController
  • ServletForwardingController
  • AbstractUrlViewController
  • UrlFilenameViewController

47
Spring MVC Flow View Resolving
Flow and Configuration of Spring MVC
Dispatcher
Dispatcher
Any defined and implemented HandlerInterceptor.pos
tHandle() calls are made. ViewResolver finds the
configured view.
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
48
View Resolving
Flow and Configuration of Spring MVC Spring MVC
Flow
  • If the ModelAndView suppplied to the Dispatcher
    from a Controller requires resolution (the
    isReference() method is true), the a ViewResolver
    has the responsibility for finding a view
    matching the configured names

49
View Resolving Implementation Classes
Flow and Configuration of Spring MVC Spring MVC
Flow View Resolving
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
50
Spring MVC Flow View Routing
Flow and Configuration of Spring MVC
  • web-app/WEB-INF/servlet-name-servlet.xml

Dispatcher
Dispatcher
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
51
Anecdote View Routing Search Order
Flow and Configuration of Spring MVC Spring MVC
Flow View Routing
1
  • DispatchController
  • DispatchController.doDispatch()

2
3
  • DispatchController

52
Anecdote View Routing Search Order
Flow and Configuration of Spring MVC Spring MVC
Flow View Routing
  • From Spring 2 Reference Document 13.5.2
  • Chaining ViewResolvers the contract of a view
    resolver mentions that a view resolver can return
    null to indicate the view could not be found. Not
    all view resolvers do this however!
  • Check the Javadoc for the view resolver to see
    if you're dealing with a view resolver that does
    not report non-existing views.

53
Spring MVC Flow View
Flow and Configuration of Spring MVC
Dispatcher
Dispatcher
Based on your ViewResolver configuration, your
view is called.
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
54
Spring MVC Flow View
Flow and Configuration of Spring MVC
55
Spring MVC Flow View
Flow and Configuration of Spring MVC
Dispatcher
Dispatcher
Any defined and implemented HandlerInterceptor.
afterCompletion() calls are made. Your view data
is response is returned to the caller
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
56
Spring MVC Run-time Flow
Flow and Configuration of Spring MVC
Handler Mapping to Controller (HandlerInterceptor.
preHandle() calls)
Web.xml finds dispatcher
2
Dispatcher
1
3
4
Controller, Model collected,View chosen.
6
(HandlerInterceptor.postHandle()) ViewResolver
calls view
HandlerInterceptor. afterCompletion(), Data
returned
5
View called
From http//www.springframework.org/docs/reference
/mvc.htmlmvc-servlet
57
Live Example
Flow and Configuration of Spring MVC Spring MVC
Flow Controller
  • Demonstration 1 Configuration Basics

58
Views from Spring
59
JSTL Introduction What is it?
Views from Spring
  • What is JSTL?
  • JavaServer Pages Standard Tag Library, JSR 52.
  • JSTL provides an effective way to embed logic
    within a JSP page without using embedded Java
    code directly.
  • Goal
  • Provide the tags to the web page authors, so that
    they can easily access and manipulate the
    application data without using the scriptlets
    (java code).

http//www.roseindia.net/jstl/jstl.shtml
60
JSTL Introduction What is it?
Views from Spring
  • Other facts
  • JSP tags are xml like tags, which can be used by
    non-programmers (page authors or designers)
    without knowledge of Java programming.
  • Because JSTL tags are xml, they fit nicely into
    an XHTML strategy allowing web pages have greater
    tool vender support (for validation and dynamic
    data insertion) among other beneficial attributes
    (such as schema support).
  • Bean driven (for data setting/access)

http//www.roseindia.net/jstl/jstl.shtml
61
JSTL Introduction Concepts
Views from Spring
  • Tag Library
  • Xml compliant structures performing JSP features
    without knowledge of Java programming.
  • Concepts such as flow control (if, for each) and
    more
  • Tags are extendable
  • Expression Language
  • Allows former java Scriptlet logic to be
    simplified into expressions
  • Expression functions are extendable

62
JSTL Introduction Before / After
Views from Spring
  • old
  • new

63
JSTL Introduction EL Expressions
Views from Spring
http//java.sun.com/j2ee/1.4/docs/tutorial/doc/JSP
Intro7.htmlwp77083
64
JSTL Introduction Other Resources
Views from Spring
  • http//www.sitepoint.com/print/java-standard-tag-l
    ibrary
  • http//java.sun.com/j2ee/1.4/docs/tutorial/doc/JST
    L4.html
  • http//en.wikipedia.org/wiki/JSTL
  • http//www.roseindia.net/jstl/jstl.shtml
  • lots more! (one word Google)

65
Spring TagLib Using JSTL
Views from Spring
Note Any Number of view technologies can be
utilized here.
http//www.theserverside.com/tt/articles/article.t
ss?lAjaxandSpring
66
Templating using Spring JSTL
Views from Spring
Controller
1
4
Objects
Model
View
2
3
End User
Model to HTML Binding
HTML Form to Object Binding
67
Model to HTML Binding
Views from Spring Templating using Spring
JSTL
1
Controller
Objects
Model
2
View
68
String Externalization in JSTL
Views from Spring Templating using Spring
JSTL
  • I18N functionality in JSTL is provided by the
    fmt tag
  • Locale and resource bundles
  • Formatting for numbers, dates, and currency

http//www.javaranch.com/newsletter/200309/AnIntro
ductionToJstl.html
69
String Externalization in JSTL
Views from Spring Templating using Spring
JSTL
  • Spring enhances these I18N support tags with its
    own ltSpringmessagegt tag
  • MessageSource integrated with Spring context
  • Define where your messages are stored
  • Works with the locale support that comes with
    Spring
  • Spring gives more flexibility in how the locale
    for messages is resolved fmtmessage tag uses
    request.getLocale() to establish the locale for
    messages

http//forum.springframework.org/archive/index.php
/t-10454.html
70
Live Example
Views from Spring Templating using Spring
JSTL
  • Demonstration 2 HTML Mapping and Spring Tag
    Library

Demonstration 2 - Part 1 Model to HTML
Binding Using External Messages
71
Form to Object Binding
Views from Spring Templating using Spring
JSTL
SomeBean bean (SomeBean) command
4
3
Controller
Class SomeBean public String
getMyBeanFieldName() public void
setMyBeanFieldName(String)
2
Object
Model
View
1
72
Spring TagLib
Views from Spring Templating using Spring
JSTL
  • Starting in Spring 2, a standard JSTL Tag Library
    was added to help simplify form-based web pages
  • form
  • input
  • checkbox
  • radiobox
  • password
  • select
  • option
  • options
  • textarea
  • hidden
  • errors

73
Spring TagLib Binding Forms
Views from Spring Templating using Spring
JSTL
  • Sample JSP

http//forum.springframework.org/showthread.php?p
120100
  • Sample -servlet.xml

74
Form Error Handling
Views from Spring Templating using Spring
JSTL
  • Built-in workflow for form error management
  • Servers side Validator(s)
  • Identifiers errors at a field level
  • Leverages Message keys for Locale support
  • JSTL tags
  • ltspringhasBindErrors namecommandObjName"gt
  • ltformerrors pathmyBeanFieldName" /gt

75
Form Error Handling
Views from Spring Templating using Spring
JSTL
  • messages.properties

error.invalidRegexpsome msg
  • Sample Validator
  • Sample JSP
  • Sample -servlet.xml

76
Live Example
Views from Spring Templating using Spring
JSTL
  • Demonstration 2 HTML Mapping and Spring Tag
    Library

Demonstration 2 - Part 2 Model to HTML
Binding Error Handling
77
Themes and Localization
Views from Spring
  • Themes
  • a collection of static resources affecting the
    visual style of the application, typically style
    sheets and images.
  • Localization
  • DispatcherServlet enables you to automatically
    resolve messages using the client's locale via
    the LocaleResolver

http//static.springframework.org/spring/docs/2.0.
x/reference/mvc.htmlmvc-localeresolver
http//static.springframework.org/spring/docs/2.0.
x/reference/mvc.htmlmvc-themeresolver
78
Theme and Locale Resolver and Intercepter
Views from Spring
ltbean id"themeChangeInterceptor"
class"org...ThemeChangeInterceptor" gt
ltproperty name"paramNamenametheme gt lt/beangt
ltbean id"localeChangeIntercepter"
class"org... LocaleChangeInterceptor" gt
ltproperty name"paramNamenamelocale gt lt/beangt
http//.../test.htm?thememello-yellowlocaleen_U
S
79
Theme Management Configuration
Views from Spring
  • web-app/WEB-INF/servlet-name-servlet.xml

... lt!-- Handler Mappings --gt ltbean
id"urlMapping" class"org.springframework...S
impleUrlHandlerMapping"gt ltproperty
name"mappings"gt ltpropsgt ltprop
key"regexpTester.htm"gtregexpTesterFormlt/propgt
lt/propsgt lt/propertygt ltproperty
name"interceptors"gt ltlistgt ltref
local"themeChangeInterceptor" /gt lt/listgt
lt/propertygt lt/beangt ... lt!-- Theme
definitions --gt ltbean id"themeResolver"
class"org.springframework...theme.SessionThemeRes
olver"gt ltproperty name"defaultThemeName"
value"mello-yellow" /gt lt/beangt ltbean
id"themeChangeInterceptor"
class"org.springframework...theme.ThemeChangeInte
rceptor" /gt lt/beangt ...
80
Live Example
Views from Spring Templating using Spring
JSTL
  • Demonstration 2 HTML Mapping and Spring Tag
    Library

Demonstration 2 - Part 3 Localization Themes
81
Live Example
Views from Spring Templating using Spring
JSTL
  • Demonstration 3 Excel and PDF Samples

Demonstration 3 Excel PDF
82
Closing
  • We looked at
  • Dispatcher Context space
  • Controllers, ModelAndView, and View
    Implementations
  • Model to HTML Binding and MappingForm to Object
    Binding
  • Intercepters theme and localization
  • Brief look at other View technologies Excel and
    PDF

83
Links
  • Inversion of Control
  • http//www.martinfowler.com/articles/injection.htm
    l
  • Spring Framework, Java
  • http//www.springframework.org/
  • Spring Framework, .NET
  • http//www.springframework.net/
Write a Comment
User Comments (0)
About PowerShow.com