SlideShare a Scribd company logo
Enhancing Spring MVC
Web Applications with
Spring JavaScript
Jeremy Grelle - SpringSource
Slide Title




   In this session, you will learn how Spring JavaScript
   makes it simple to integrate rich Ajax toolkits such
   as Dojo into a Spring Web application to create
   compelling user interfaces.




                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Introduction

• Who am I???
  – Jeremy Grelle
     • Senior Software Engineer, SpringSource
     • Member of Spring Web Products team in San
       Francisco, CA
     • Lead of Spring Faces, Spring JavaScript, Spring
       BlazeDS Integration
     • JSF 2.0 EG member.
     • SpringSource's resident Ajax ninja.
     • Rock Star / Geek




                                                                                                 3
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Topics in this session

•   Overview of Ajax
•   Introduction to Spring JS
•   Spring JS Building Blocks
•   Spring JS Advanced Solutions
•   Spring JS + RESTful MVC




                                                                                                 4
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
What is Ajax?

• An ever-evolving definition

• Philosophy of User Experience

• Search for the right balance between client and
  server

• AJAX != Ajax




                                                                                                5
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The Building Blocks of Ajax

• Document Object Model (DOM)
  – tree HTML representation
  – easy to target parts of dynamic web page
  – e.g. 'disable all fields within a div'
• Cascading Stylesheets (CSS)
  – separates content (HTML) from style
  – CSS selectors apply style across DOM elements
• XMLHttpRequest
  – JavaScript API for asynchronous requests
• JavaScript Object Notation (JSON)



                                                                                               6
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Different Ajax Approaches

• JavaScript toolkit
  – Dojo, YUI, jQuery
• JavaScript wrapped
  – in JSF components (ICEfaces, RichFaces)
  – in JSP custom tags (AjaxTags)
• JavaScript generated
  – GWT
• JavaScript RPC-style invocations
  – DWR, JSON-RPC
• RIA - Not necessarily based on JavaScript



                                                                                               7
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Different Kinds of Frameworks

• Lightweight libraries
  – jQuery, Prototype, Scriptaculous

• Full solution frameworks
  – Dojo, YUI, Ext, GWT




                                                                                                8
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Different Application Types

• Applications based on browsing model
    – document-like, bookmarks, back button, refresh
    – example: infoq.com

 • Very rich applications in a browser
    – desktop style, window panes, menus
    – example: Google Maps, Bespin

 • Many applications somewhere in between
    – finding the right balance is important




                                                                                                  9
                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The User Experience

• Ajax can do pretty “cool” things
  – it's easy to be awed at first BUT...
  – harder to predict the overall experience
• Consider the following “disruptive” effects
  – back/forward button
  – bookmarking
  – JavaScript turned off
  – support for accessibility features
• It's necessary to be pragmatic in using Ajax
  techniques
  – find the right blend of document-based vs. rich client



                                                                                                  10
                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Ajax and Server-Side
Frameworks
• Ajax agnostic to server-side framework
    – plain HTTP request

 • The response is the contract
    – HTML (partial DOM update), JSON, XML, ...

 • Full HTML still required even for RIA
    – login page, help pages, ...




                                                                                                  11
                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Spring MVC with Ajax

 • A swiss army knife for Web developers
  – very flexible, pluggable

 • Well-suited to handle Ajax requests
  – supports many rendering technologies
  – easy to expose HTTP endpoints
  – RESTful architecture is an ideal Ajax back-end

 • Versatility truly matters here!




                                                                                                12
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Topics in this session

•   Overview of Ajax
•   Introduction to Spring JS
•   Spring JS Building Blocks
•   Spring JS Advanced Solutions
•   Spring JS + RESTful MVC




                                                                                                 13
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Spring JavaScript Overview

• JavaScript abstraction framework
    – builds on existing toolkits
    – introduced as a SWF 2 module, moving to top-level
      project for 2010

 • Public API with implementations
    – currently Dojo
    – others possible (jQuery next)

 • A pragmatic approach
    – baked-in performance (Yahoo guidelines)
    – progressive enhancement

                                                                                                  14
                 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Progressive Enhancement
(client side)

• Add Ajax behavior unobtrusively
  – avoid proliferation of JavaScript

• Manipulate DOM nodes
  – “enhance” plain HTML elements

• Page functions with or without JavaScript




                                                                                                15
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Progressive Enhancement
(server side)

• Avoid separate controllers for Ajax
  – use same request handling methods

• Render a page partially
  – composition-based template approach

• Alternate response types
  – HTML, JSON, XML, ...




                                                                                               16
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The Parts of Spring JS

• JavaScript libraries
  – public API + implementations
• Bundled (optimized) toolkits
  – currently Dojo 1.2
• “Starter” CSS framework
  – Mike Stenhouse's CSS framework
• ResourceServlet
  – efficient rendering of static resources in a war or
  a jar
• Spring MVC infrastructure for Ajax
  – AjaxUrlBasedViewResolver, AjaxTilesView, ...

                                                                                                17
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The ResourceServlet

• Generic servlet to serve static resources
  – .js, .css, .jpeg, .gif, etc.

• Locates resources given the path info
  – first under the Web application root
  – the classpath as a fall back

• Writes the resource to the HTTP response
  – gzip compression
  – set response headers: last modified, content
  type, ...


                                                                                                18
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Serving Spring JS Files

 • Spring JS distributed as a jar
      – bundles JavaScript, CSS, and other resources
 • Serve them with the ResourceServlet
 • Possible to override bundled resources
      – include your own in the webapp root
<servlet>
 <servlet-name>Resources Servlet</servlet-name>
 <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>Resources Servlet</servlet-name>
 <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

                                                                                                         19
                        SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Spring JS Resources

• Underlying library
  – /resources/dojo/dojo.js

• Public API
  – /resources/spring/Spring.js

• Toolkit-specific implementation
  – /resources/spring/Spring-Dojo.js

• Additional underlying library resources
  – /resources/dijit/themes/tundra/tundra.css


                                                                                                20
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Including Spring JS On a Page

 • Include required files
<link type="text/css" rel="stylesheet"
   href="<c:url value="/resources/dijit/themes/tundra/tundra.css" />" />

<script type="text/javascript"
          src ="<c:url value="/resources/dojo/dojo.js" />"><script/>
<script type="text/javascript"
          src ="<c:url value="/resources/spring/Spring.js" />"><script/>
<script type="text/javascript"
          src ="<c:url value="/resources/spring/Spring-Dojo.js" />"><script/>

 • Add Dojo “tundra” style to body tag
<body class="tundra" > ...



                                                                                                          21
                         SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Topics in this session

•   Overview of Ajax
•   Introduction to Spring JS
•   Spring JS Building Blocks
•   Spring JS Advanced Solutions
•   Spring JS + RESTful MVC




                                                                                                 22
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Enhancing a DOM Node

• DOM nodes can be manipulated
  – decorate with new look and/or behavior
  – via JS and CSS

• A common technique for “enhancing” plain HTML
  – separates static from dynamic parts
  – easier to understand & maintain

• Supported by underlying toolkit
  – e.g. turn HTML form fields to rich widgets



                                                                                                23
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Spring JS Decorations

• Mechanism for DOM node enhancements
  – abstracts underlying toolkit

• A function for applying decorations
  – Spring.addDecoration(/*Object*/decoration)

• Several available decorations
  – Element decoration
  – Ajax event decoration
  – Validate All decoration



                                                                                                24
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Fragment Rendering

• An Ajax call refreshes parts of a page
  – response must contain a “fragment”
  – not HTML for entire page

• Yet controllers should not be aware of Ajax
  – easier to maintain that way

• Spring JS provides special Spring MVC view
  – renders a fragment of the response only
  – works transparently



                                                                                                25
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The “Fragments” Parameter

• Ajax event decoration submits special parameter
  – a hint for what HTML to be rendered

• Server notices special parameter
  – renders only specified fragment

• Composition-based view technology is required
  – currently JSP/Tiles supported only
  – fragment names match to a Tiles attribute




                                                                                                26
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Built-In Response Handling
Types
• Refresh portions of the page
   – grab top-level DOM nodes
   – replace equivalent nodes on existing page

• Present Ajax popup
   – a modal dialog with the returned HTML
   – enable via AjaxEventDecoration “popup” attribute

• Client side redirect
   – detects server response requesting client-side
     redirect
   – requires AjaxUrlBasedViewResolver


                                                                                                 27
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Demo
Spring JS Building Blocks




       SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Topics in this session

•   Overview of Ajax
•   Introduction to Spring JS
•   Spring JS Building Blocks
•   Spring JS Advanced Solutions
•   Spring JS + RESTful MVC




                                                                                                29
               SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Common Questions

• The fundamental pieces of Spring JS seem nice,
  but how do I...

  – perform custom response handling?

  – make my code more terse?

  – keep things DRY?

  – organize and modularize my growing amount of
    JavaScript code?


                                                                                               30
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Demo
Spring JS Advanced Solutions




       SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Topics in this session

•   Overview of Ajax
•   Introduction to Spring JS
•   Spring JS Building Blocks
•   Spring JS Advanced Solutions
•   Spring JS + RESTful MVC




                                                                                                 32
                SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
The Evolving Web Platform

• Capable browsers with advanced JavaScript VM’s
  are becoming more commonplace
  – Safari, Firefox, and Chrome
  – Webkit-based mobile browsers

• As the platform evolves, boundaries can be pushed
  further on the client
  – see Canvas, SVG, Video in HTML 5

• RESTful services provide the perfect compliment
  on the server-side
  – JSON representations easy to consume

                                                                                               33
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
JSON support in Spring 3.0

• MappingJacksonJsonView
  – renders the model as JSON
  – customizable via annotations
  – high-performance

• MappingJacksonHttpMessageConverter
  – easy binding from a JSON payload




                                                                                               34
              SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Demo
Consuming RESTful Services




      SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Summary

• Spring MVC very useful for Ajax
  – a swiss-army knife

• Spring JS provides simple abstraction
  – builds on existing Ajax toolkits
  – provides both client and server side components

• Many common enterprise scenarios made easy
  – rich forms
  – partial page updates
  – Ajax popups
  – JSON

                                                                                                   36
                  SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
Q&A




      SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.

More Related Content

What's hot (19)

PDF
新版阿尔法城背后的前端MVC实践
Dexter Yang
 
DOC
Santosh_Resume_Java
SANTOSH PATTNAIK
 
PDF
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 
KEY
Single Page Applications - Desert Code Camp 2012
Adam Mokan
 
PDF
The Modern Java Web Developer - JavaOne 2013
Matt Raible
 
PPT
Intro to drupal
hernanibf
 
PPTX
Building SPA’s (Single Page App) with Backbone.js
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
PDF
2013 04-02-server-side-backbone
SC5.io
 
PDF
Oxford DrupalCamp 2012 - The things we found in your website
hernanibf
 
PPTX
One Drupal to rule them all - Drupalcamp London
hernanibf
 
PDF
Comparing JVM Web Frameworks - Devoxx 2010
Matt Raible
 
PDF
Next generation Graphics: SVG
David Corbacho Román
 
PPTX
Front End page speed performance improvements for Drupal
Andy Kucharski
 
PDF
JavaONE 2012 Using Java with HTML5 and CSS3
Helder da Rocha
 
PDF
My site is slow
hernanibf
 
PPT
Drupal - Introduction to Building Library Web Site Using Drupal
Vibrant Technologies & Computers
 
PDF
jQuery Comes to XPages
Teamstudio
 
PDF
Devfest09 Cschalk Gwt
Chris Schalk
 
PPTX
Building and Managing Projects with Maven
Khan625
 
新版阿尔法城背后的前端MVC实践
Dexter Yang
 
Santosh_Resume_Java
SANTOSH PATTNAIK
 
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 
Single Page Applications - Desert Code Camp 2012
Adam Mokan
 
The Modern Java Web Developer - JavaOne 2013
Matt Raible
 
Intro to drupal
hernanibf
 
Building SPA’s (Single Page App) with Backbone.js
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
2013 04-02-server-side-backbone
SC5.io
 
Oxford DrupalCamp 2012 - The things we found in your website
hernanibf
 
One Drupal to rule them all - Drupalcamp London
hernanibf
 
Comparing JVM Web Frameworks - Devoxx 2010
Matt Raible
 
Next generation Graphics: SVG
David Corbacho Román
 
Front End page speed performance improvements for Drupal
Andy Kucharski
 
JavaONE 2012 Using Java with HTML5 and CSS3
Helder da Rocha
 
My site is slow
hernanibf
 
Drupal - Introduction to Building Library Web Site Using Drupal
Vibrant Technologies & Computers
 
jQuery Comes to XPages
Teamstudio
 
Devfest09 Cschalk Gwt
Chris Schalk
 
Building and Managing Projects with Maven
Khan625
 

Viewers also liked (10)

PDF
Modern Architectures with Spring and JavaScript
martinlippert
 
PDF
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
PPT
Building a Scalable Architecture for web apps
Directi Group
 
PDF
Hr coverage directi 2012
Directi Group
 
PPT
Web Application Development Fundamentals
Mohammed Makhlouf
 
PPTX
Web application architecture
Tejaswini Deshpande
 
PPTX
Introduction to Web Architecture
Chamnap Chhorn
 
PDF
Fundamentals of Web Development For Non-Developers
Lemi Orhan Ergin
 
PDF
Architecture of a Modern Web App
scothis
 
Modern Architectures with Spring and JavaScript
martinlippert
 
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
Building a Scalable Architecture for web apps
Directi Group
 
Hr coverage directi 2012
Directi Group
 
Web Application Development Fundamentals
Mohammed Makhlouf
 
Web application architecture
Tejaswini Deshpande
 
Introduction to Web Architecture
Chamnap Chhorn
 
Fundamentals of Web Development For Non-Developers
Lemi Orhan Ergin
 
Architecture of a Modern Web App
scothis
 
Ad

Similar to Enhancing Spring MVC Web Applications Progressively with Spring JavaScript (20)

PDF
Seven Versions of One Web Application
Yakov Fain
 
PDF
An introduction to Node.js
Kasey McCurdy
 
PDF
JavaScript-Core
tutorialsruby
 
PDF
Structured web apps
Sheng Tian
 
PPTX
Building assets on the fly with Node.js
Acquisio
 
PDF
The Java alternative to Javascript
Manuel Carrasco Moñino
 
PPTX
Going offline with JS (DDD Sydney)
brendankowitz
 
PPS
AJAX Frameworks
shank
 
PDF
Meetup. Technologies Intro for Non-Tech People
IT Arena
 
PPTX
AngularJS - Architecture decisions in a large project 
Elad Hirsch
 
PPTX
Progressive Web Apps and React
Mike Melusky
 
PPTX
An evening with React Native
Mike Melusky
 
PPTX
WebNetConf 2012 - Single Page Apps
Pop Apps
 
PDF
Server Side Javascript
rajivmordani
 
PDF
Kann JavaScript elegant sein?
jbandi
 
PPTX
Going Offline with JS
brendankowitz
 
PPTX
Mobile and IBM Worklight Best Practices
Andrew Ferrier
 
PDF
01/2009 - Portral development with liferay
daveayan
 
PPTX
Javascript & Jquery
Gurpreet singh
 
PDF
GWT-Basics
tutorialsruby
 
Seven Versions of One Web Application
Yakov Fain
 
An introduction to Node.js
Kasey McCurdy
 
JavaScript-Core
tutorialsruby
 
Structured web apps
Sheng Tian
 
Building assets on the fly with Node.js
Acquisio
 
The Java alternative to Javascript
Manuel Carrasco Moñino
 
Going offline with JS (DDD Sydney)
brendankowitz
 
AJAX Frameworks
shank
 
Meetup. Technologies Intro for Non-Tech People
IT Arena
 
AngularJS - Architecture decisions in a large project 
Elad Hirsch
 
Progressive Web Apps and React
Mike Melusky
 
An evening with React Native
Mike Melusky
 
WebNetConf 2012 - Single Page Apps
Pop Apps
 
Server Side Javascript
rajivmordani
 
Kann JavaScript elegant sein?
jbandi
 
Going Offline with JS
brendankowitz
 
Mobile and IBM Worklight Best Practices
Andrew Ferrier
 
01/2009 - Portral development with liferay
daveayan
 
Javascript & Jquery
Gurpreet singh
 
GWT-Basics
tutorialsruby
 
Ad

Recently uploaded (20)

PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 

Enhancing Spring MVC Web Applications Progressively with Spring JavaScript

  • 1. Enhancing Spring MVC Web Applications with Spring JavaScript Jeremy Grelle - SpringSource
  • 2. Slide Title In this session, you will learn how Spring JavaScript makes it simple to integrate rich Ajax toolkits such as Dojo into a Spring Web application to create compelling user interfaces. SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 3. Introduction • Who am I??? – Jeremy Grelle • Senior Software Engineer, SpringSource • Member of Spring Web Products team in San Francisco, CA • Lead of Spring Faces, Spring JavaScript, Spring BlazeDS Integration • JSF 2.0 EG member. • SpringSource's resident Ajax ninja. • Rock Star / Geek 3 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 4. Topics in this session • Overview of Ajax • Introduction to Spring JS • Spring JS Building Blocks • Spring JS Advanced Solutions • Spring JS + RESTful MVC 4 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 5. What is Ajax? • An ever-evolving definition • Philosophy of User Experience • Search for the right balance between client and server • AJAX != Ajax 5 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 6. The Building Blocks of Ajax • Document Object Model (DOM) – tree HTML representation – easy to target parts of dynamic web page – e.g. 'disable all fields within a div' • Cascading Stylesheets (CSS) – separates content (HTML) from style – CSS selectors apply style across DOM elements • XMLHttpRequest – JavaScript API for asynchronous requests • JavaScript Object Notation (JSON) 6 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 7. Different Ajax Approaches • JavaScript toolkit – Dojo, YUI, jQuery • JavaScript wrapped – in JSF components (ICEfaces, RichFaces) – in JSP custom tags (AjaxTags) • JavaScript generated – GWT • JavaScript RPC-style invocations – DWR, JSON-RPC • RIA - Not necessarily based on JavaScript 7 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 8. Different Kinds of Frameworks • Lightweight libraries – jQuery, Prototype, Scriptaculous • Full solution frameworks – Dojo, YUI, Ext, GWT 8 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 9. Different Application Types • Applications based on browsing model – document-like, bookmarks, back button, refresh – example: infoq.com • Very rich applications in a browser – desktop style, window panes, menus – example: Google Maps, Bespin • Many applications somewhere in between – finding the right balance is important 9 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 10. The User Experience • Ajax can do pretty “cool” things – it's easy to be awed at first BUT... – harder to predict the overall experience • Consider the following “disruptive” effects – back/forward button – bookmarking – JavaScript turned off – support for accessibility features • It's necessary to be pragmatic in using Ajax techniques – find the right blend of document-based vs. rich client 10 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 11. Ajax and Server-Side Frameworks • Ajax agnostic to server-side framework – plain HTTP request • The response is the contract – HTML (partial DOM update), JSON, XML, ... • Full HTML still required even for RIA – login page, help pages, ... 11 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 12. Spring MVC with Ajax • A swiss army knife for Web developers – very flexible, pluggable • Well-suited to handle Ajax requests – supports many rendering technologies – easy to expose HTTP endpoints – RESTful architecture is an ideal Ajax back-end • Versatility truly matters here! 12 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 13. Topics in this session • Overview of Ajax • Introduction to Spring JS • Spring JS Building Blocks • Spring JS Advanced Solutions • Spring JS + RESTful MVC 13 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 14. Spring JavaScript Overview • JavaScript abstraction framework – builds on existing toolkits – introduced as a SWF 2 module, moving to top-level project for 2010 • Public API with implementations – currently Dojo – others possible (jQuery next) • A pragmatic approach – baked-in performance (Yahoo guidelines) – progressive enhancement 14 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 15. Progressive Enhancement (client side) • Add Ajax behavior unobtrusively – avoid proliferation of JavaScript • Manipulate DOM nodes – “enhance” plain HTML elements • Page functions with or without JavaScript 15 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 16. Progressive Enhancement (server side) • Avoid separate controllers for Ajax – use same request handling methods • Render a page partially – composition-based template approach • Alternate response types – HTML, JSON, XML, ... 16 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 17. The Parts of Spring JS • JavaScript libraries – public API + implementations • Bundled (optimized) toolkits – currently Dojo 1.2 • “Starter” CSS framework – Mike Stenhouse's CSS framework • ResourceServlet – efficient rendering of static resources in a war or a jar • Spring MVC infrastructure for Ajax – AjaxUrlBasedViewResolver, AjaxTilesView, ... 17 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 18. The ResourceServlet • Generic servlet to serve static resources – .js, .css, .jpeg, .gif, etc. • Locates resources given the path info – first under the Web application root – the classpath as a fall back • Writes the resource to the HTTP response – gzip compression – set response headers: last modified, content type, ... 18 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 19. Serving Spring JS Files • Spring JS distributed as a jar – bundles JavaScript, CSS, and other resources • Serve them with the ResourceServlet • Possible to override bundled resources – include your own in the webapp root <servlet> <servlet-name>Resources Servlet</servlet-name> <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Resources Servlet</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> 19 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 20. Spring JS Resources • Underlying library – /resources/dojo/dojo.js • Public API – /resources/spring/Spring.js • Toolkit-specific implementation – /resources/spring/Spring-Dojo.js • Additional underlying library resources – /resources/dijit/themes/tundra/tundra.css 20 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 21. Including Spring JS On a Page • Include required files <link type="text/css" rel="stylesheet" href="<c:url value="/resources/dijit/themes/tundra/tundra.css" />" /> <script type="text/javascript" src ="<c:url value="/resources/dojo/dojo.js" />"><script/> <script type="text/javascript" src ="<c:url value="/resources/spring/Spring.js" />"><script/> <script type="text/javascript" src ="<c:url value="/resources/spring/Spring-Dojo.js" />"><script/> • Add Dojo “tundra” style to body tag <body class="tundra" > ... 21 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 22. Topics in this session • Overview of Ajax • Introduction to Spring JS • Spring JS Building Blocks • Spring JS Advanced Solutions • Spring JS + RESTful MVC 22 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 23. Enhancing a DOM Node • DOM nodes can be manipulated – decorate with new look and/or behavior – via JS and CSS • A common technique for “enhancing” plain HTML – separates static from dynamic parts – easier to understand & maintain • Supported by underlying toolkit – e.g. turn HTML form fields to rich widgets 23 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 24. Spring JS Decorations • Mechanism for DOM node enhancements – abstracts underlying toolkit • A function for applying decorations – Spring.addDecoration(/*Object*/decoration) • Several available decorations – Element decoration – Ajax event decoration – Validate All decoration 24 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 25. Fragment Rendering • An Ajax call refreshes parts of a page – response must contain a “fragment” – not HTML for entire page • Yet controllers should not be aware of Ajax – easier to maintain that way • Spring JS provides special Spring MVC view – renders a fragment of the response only – works transparently 25 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 26. The “Fragments” Parameter • Ajax event decoration submits special parameter – a hint for what HTML to be rendered • Server notices special parameter – renders only specified fragment • Composition-based view technology is required – currently JSP/Tiles supported only – fragment names match to a Tiles attribute 26 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 27. Built-In Response Handling Types • Refresh portions of the page – grab top-level DOM nodes – replace equivalent nodes on existing page • Present Ajax popup – a modal dialog with the returned HTML – enable via AjaxEventDecoration “popup” attribute • Client side redirect – detects server response requesting client-side redirect – requires AjaxUrlBasedViewResolver 27 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 28. Demo Spring JS Building Blocks SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 29. Topics in this session • Overview of Ajax • Introduction to Spring JS • Spring JS Building Blocks • Spring JS Advanced Solutions • Spring JS + RESTful MVC 29 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 30. Common Questions • The fundamental pieces of Spring JS seem nice, but how do I... – perform custom response handling? – make my code more terse? – keep things DRY? – organize and modularize my growing amount of JavaScript code? 30 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 31. Demo Spring JS Advanced Solutions SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 32. Topics in this session • Overview of Ajax • Introduction to Spring JS • Spring JS Building Blocks • Spring JS Advanced Solutions • Spring JS + RESTful MVC 32 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 33. The Evolving Web Platform • Capable browsers with advanced JavaScript VM’s are becoming more commonplace – Safari, Firefox, and Chrome – Webkit-based mobile browsers • As the platform evolves, boundaries can be pushed further on the client – see Canvas, SVG, Video in HTML 5 • RESTful services provide the perfect compliment on the server-side – JSON representations easy to consume 33 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 34. JSON support in Spring 3.0 • MappingJacksonJsonView – renders the model as JSON – customizable via annotations – high-performance • MappingJacksonHttpMessageConverter – easy binding from a JSON payload 34 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 35. Demo Consuming RESTful Services SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 36. Summary • Spring MVC very useful for Ajax – a swiss-army knife • Spring JS provides simple abstraction – builds on existing Ajax toolkits – provides both client and server side components • Many common enterprise scenarios made easy – rich forms – partial page updates – Ajax popups – JSON 36 SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.
  • 37. Q&A SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.