Mastering Spring MVC 3

Similar documents
~$> whoami. Ioan Marius Curelariu SDE at Website Analytics Amazon Development Center Romania

CHAPTER 4 ENTITIES AND COMPLEX CONTENT

JVA-117E. Developing RESTful Services with Spring

Index. Combined lifecycle strategy, annotation, 93 ContentNegotiatingViewResolver, 78

Tools for Accessing REST APIs

Introduction to Spring Framework: Hibernate, Spring MVC & REST

REST WEB SERVICES IN JAVA EE 6 AND SPRING 3. Srini Penchikala Austin Java User Group March 30, 2010

RESTful Web Services. 20-Jan Gordon Dickens Chariot Solutions

Spring boot multipart response

JVA-117A. Spring-MVC Web Applications

ADVANCED JAVA TRAINING IN BANGALORE

JVA-563. Developing RESTful Services in Java

Session 25. Spring Controller. Reading & Reference

Spring MVC. PA 165, Lecture 8. Martin Kuba

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

Java J Course Outline

Pro Spring MVC. Marten Deinum Koen Serneels. With Web Flow. Spring Web Flow project founder. Foreword by Erwin Vervaet,

Developing Applications for the Java EE 7 Platform 9-2

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Introduction to Spring 5, Spring MVC and Spring REST

Fast Track to Spring 3 and Spring MVC / Web Flow

Struts: Struts 1.x. Introduction. Enterprise Application

Apache Wink User Guide

Desarrollo de Aplicaciones Web Empresariales con Spring 4

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Module 3 Web Component

5.1 Registration and Configuration

Page 1

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

CORE JAVA 1. INTRODUCATION

CHAPTER 6 ERROR HANDLING AND VALIDATION

Creating RESTful web services with Spring Boot

Introduction to JSP and Servlets Training 5-days

Apache Wink 0.1 Feature Set

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

SSC - Web applications and development Introduction and Java Servlet (I)

Advanced Java Programming

Introduction to Spring Framework: Hibernate, Web MVC & REST

JAVA Training Overview (For Demo Classes Call Us )

Developing Applications with Java EE 6 on WebLogic Server 12c

/ / JAVA TRAINING

Web Applications. and. Struts 2

Table of Contents. Introduction... xxi

Spring Interview Questions

Spring Interview Questions

Building Web Applications With The Struts Framework

Distributed Applications Spring MVC 2.5

Spring MVC. Richard Paul Kiwiplan NZ Ltd 27 Mar 2009

Improve and Expand JavaServer Faces Technology with JBoss Seam

MTAT Enterprise System Integration

Advanced Web Systems 4- PORTLET API specifications (JSR 286) A. Venturini

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

IT6503 WEB PROGRAMMING. Unit-I

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Multi Client Development with Spring

CONFIGURING A SPRING DEVELOPMENT ENVIRONMENT

WA1278 Introduction to Java Using Eclipse

Introduc)on to JAX- RS 3/14/12

Java SE7 Fundamentals

JAVA. 1. Introduction to JAVA

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

Session 12. RESTful Services. Lecture Objectives

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Specialized - Mastering JEE 7 Web Application Development

APACHE SLING & FRIENDS TECH MEETUP BERLIN, SEPTEMBER Dynamic Components using SPA Concepts Andon Sikavica, Bojana Popovska

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Web Presentation Patterns (controller) SWEN-343 From Fowler, Patterns of Enterprise Application Architecture

Implementing a Numerical Data Access Service

Fast Track to Java EE 5 with Servlets, JSP & JDBC

About the Authors. Who Should Read This Book. How This Book Is Organized

Contents at a Glance

Call us: /

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Fast Track to Java EE

Complete Java Contents

Enterprise Java Development using JPA, Hibernate and Spring. Srini Penchikala Detroit JUG Developer Day Conference November 14, 2009

JAVA MICROSERVICES. Java Language Environment. Java Set Up. Java Fundamentals. Packages. Operations

Java EE 6: Develop Web Applications with JSF

Session 9. Deployment Descriptor Http. Reading and Reference. en.wikipedia.org/wiki/http. en.wikipedia.org/wiki/list_of_http_headers

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Tapestry. Code less, deliver more. Rayland Jeans

JSR 311: JAX-RS: The Java API for RESTful Web Services

Session 8. Reading and Reference. en.wikipedia.org/wiki/list_of_http_headers. en.wikipedia.org/wiki/http_status_codes

SSC - Web development Model-View-Controller for Java Servlet

Spring Framework 2.5: New and Notable. Ben Alex, Principal Software Engineer, SpringSource

One application has servlet context(s).

Watch Core Java and Advanced Java Demo Video Here:

1Z Java EE 6 Web Component Developer Certified Expert Exam Summary Syllabus Questions

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

Restful applications using Spring MVC. Kamal Govindraj TenXperts Technologies

CIS 455 / 555: Internet and Web Systems

Elevate Web Builder Modules Manual

> Dmitry Sklyut > Matt Swartley. Copyright 2005 Chariot Solutions

Chapter 2 Introduction

1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Unit 5 JSP (Java Server Pages)

Servlet Fudamentals. Celsina Bignoli

Enterprise Java Unit 1- Chapter 3 Prof. Sujata Rizal Introduction to Servlets

Transcription:

Mastering Spring MVC 3 And its @Controller programming model Get the code for the demos in this presentation at http://src.springsource.org/svn/spring-samples/mvc-showcase 2010 SpringSource, A division of VMware. All rights reserved

Topics Getting started Introduction to the MVC programming model Mapping HTTP requests Obtaining request input Generating responses Rendering views Type conversion, validation, forms, and file upload Exception handling Testing 2

Getting started Create a new Spring MVC project from a template Most use Roo to do this, either from an IDE like STS or the command-line Typical setup: One DispatcherServlet registered in web.xml FrontController that dispatches web requests to your application logic Generally the default servlet mapped to / Two Spring Containers (or ApplicationContexts) instantiated 1 root context to host shared resources required by Servlets / Filters 1 web context to host local application components delegated to by the DispatcherServlet Your application components are typically discovered via classpath scanning 3

Demo Typical Spring MVC project structure 4

Introduction to the MVC programming model DispatcherServlet requests are mapped to @Controller methods @RequestMapping annotation used to define mapping rules Method parameters used to obtain request input Method return values used to generate responses Simplest possible @Controller @Controller public class HomeController { @RequestMapping( / ) public @ResponseBody String home() { return hello world ; } } 5

Demo Simplest possible @Controller org.springframework.samples.mvc.simple 6

Mapping requests By path @RequestMapping( path ) By HTTP method @RequestMapping( path, method=requestmethod.get) POST, PUT, DELETE, OPTIONS, and TRACE are are also supported By presence of query parameter @RequestMapping( path, method=requestmethod.get, params= foo ) Negation also supported: params={ foo,!bar }) By presence of request header @RequestMapping( path, header= content-type=text/* ) Negation also supported 7

Mapping requests (2) Simplest possible @Controller revisited @Controller public class HomeController { @RequestMapping( /, method=requestmethod.get, headers= Accept=text/plain ) public @ResponseBody String home() { return hello world ; } } 8

Demo Mapping requests org.springframework.samples.mvc.mapping 9

Request mapping at the class level @RequestMapping can be used at the class level Concise way to map all requests within a path to a @Controller @Controller @RequestMapping( /accounts/*) public class AccountsController { @RequestMapping( active ) public @ResponseBody List<Account> active() { + } } @RequestMapping( inactive ) public @ResponseBody List<Account> inactive() { + } 10

Request mapping at the class level (2) The same rules expressed with method-level mapping only: @Controller public class AccountsController { @RequestMapping( /accounts/active ) public @ResponseBody List<Account> active() { + } @RequestMapping( /accounts/inactive ) public @ResponseBody List<Account> inactive() { + } } 11

Demo @RequestMapping at the class level org.springframework.samples.mvc.mapping 12

Obtaining request data Obtain request data by declaring method arguments A query parameter value @RequestParam( name ) A group of query parameter values A custom JavaBean with a getname()/setname() pair for each parameter A path element value @PathVariable( var ) A request header value @RequestHeader( name ) A cookie value @CookieValue( name ) The request body @RequestBody The request body and any request header HttpEntity<T> 13

Demo Obtaining request data org.springframework.samples.mvc.data 14

Injecting standard objects A number of standard arguments can also be injected Simply declare the argument you need HttpServletRequest (or its more portable WebRequest wrapper) Principal Locale InputStream Reader HttpServletResponse OutputStream Writer HttpSession 15

Injecting custom objects Custom object injectors can also be defined Implement the WebArgumentResolver extension point Register with the AnnotationMethodHandlerAdapter public interface WebArgumentResolver { Object resolveargument(methodparameter param, NativeWebRequest request); } 16

Demo Injecting standard and custom objects org.springframework.samples.mvc.data.standard org.springframework.samples.mvc.data.custom 17

Generating responses Return a POJO annotated with @ResponseBody POJO marshaled as the body of the response or Return a new ResponseEntity<T> object More powerful; allows for setting custom response headers and status code 18

Demo Generating responses org.springframework.samples.mvc.response 19

HttpMessageConverters Behind the scenes, a HttpMessageConverter underpins reading the request body and generating the response Multiple converters may be registered for different content types For @RequestBody, the first converter that can read the POSTed Content-Type into the desired method parameter type is used For @ResponseBody, the first converter that can write the method return type into one of the client s Accept ed content types is used Also applies to HttpResponseEntity<T> (can also force the content-type) Default set of HttpMessageConverters registered for you Can write your own 20

Default HttpMessageConverters StringHttpMessageConverter Reads text/* into Strings; writes Strings as text/plain FormHttpMessageConverter Reads application/x-www-form-urlencoded into MultiValueMap<String, String> Writes MultiValueMap<String, String> into application/x-www-form-urlencoded ByteArrayMessageConverter Reads */* into a byte[]; writes Objects as application/octet-stream Jaxb2RootElementHttpMessageConverter Reads text/xml application/xml into Objects annotated by JAXB annotations Writes JAXB-annotated Objects as text/xml or application/xml Only registered by default if JAXB is present on the classpath 21

Default HttpMessageConverters (2) MappingJacksonHttpMessageConverter Reads application/json into Objects; writes Objects as application/json Delegates to the Jackson JSON Processing Library Only registered by default if Jackson API is in your classpath SourceHttpMessageConverter Reads text/xml or application/xml into javax.xml.transform.source Writes javax.xml.transform.source to text/xml or application/xml ResourceHttpMessageConverter Reads/writes org.springframework.core.io.resource objects AtomFeed/RssChannelHttpMessageConverter Reads/writes Rome Feed and RssChannels (application/atom+xml rss+xml) Only registered by default if Rome is present in your classpath 22

Demo Default HttpMessageConverters org.springframework.samples.mvc.messageconverters 23

Other HttpMessageConverters options available to you BufferedImageHttpMessageConverter Reads/writes mime-types supported by Java Image I/O into BufferedImage MarshallingHttpMessageConverter Reads/writes XML but allows for pluggability in Marshalling technology Register your own or customize existing ones by setting the messageconverters property of the AnnotationMethodHandlerAdapter bean Easy to override the defaults using a BeanPostProcessor 24

Rendering views A DispatcherServlet can also render Views Alternative to having a HttpMessageConverter write the response body Designed for generating text/* content from a template Declare a Model parameter to export data to the view Call model.addattribute( name, value) for each item to export Select the view by to render by returning a String Do not use @ResponseBody annotation in this case Configured ViewResolver maps name to a View instance Default ViewResolver forwards to internal servlet resources Many other options: JSP, Tiles, Freemarker, Velocity, itext PDF, JExcel, Jasper Reports, and XSLT are all supported out of the box Can also write your own View integrations 25

Demo Rendering views org.springframework.samples.mvc.views 26

Views vs. @ResponseBody (aka HttpMessageConverters) Two different systems exist for rendering responses ViewResolver + View HttpMessageConverter Triggered in different ways Render a view by returning a String Write a message by returning a @ResponseBody Object or ResponseEntity Which one do I use? Use views to generate documents for display in a web browser HTML, PDF, etc Use @ResponseBody to exchange data with web service clients JSON, XML, etc 27

Type conversion Type conversion happens automatically A common ConversionService underpins the places where type conversion is required Always used with @RequestParam, JavaBean, @PathVariable, and @RequestHeader, and @CookieValue HttpMessageConverter may use when reading and writing objects for @RequestBody, @ResponseBody, HttpEntity, ResponseEntity All major conversion requirements satisfied out-of-the-box Primitives, Strings, Dates, Collections, Maps, custom value objects Can declare annotation-based conversion rules @NumberFormat, @DateTimeFormat, your own custom @Format annotation Elegant SPI for implementing your own converters 28

Demo Type Conversion System org.springframework.samples.mvc.convert 29

Validation Trigger validation by marking a JavaBean parameter as @Valid The JavaBean will be passed to a Validator for validation JSR-303 auto-configured if a provider is present on the classpath Binding and validation errors can be trapped and introspected by declaring a BindingResult parameter Must follow the JavaBean parameter in the method signature Errors automatically exported in the model when rendering views Not supported with other request parameter types (@RequestBody, etc) 30

Demo Validation org.springframework.samples.mvc.validation 31

Forms Get a new form Export a JavaBean to the view as the form bean or form backing object Can pre-populate form using initial bean property values or client query parameters Convenient form tags/macros exist to simplify rendering form fields Post a form Declare JavaBean argument to trigger binding and validation Declare a BindingResult argument to query binding and validation results Re-render form view if validation errors are present Redirect after successful post by returning target resource URL prefixed with special redirect: directive Store any success messages in a flash map cleared after the next request Flash map contents are cached temporarily in the session until the next request completes, then cleared 32

Demo Forms org.springframework.samples.mvc.form 33

Fileupload File Upload Form Set form encoding to multipart/form-data Declare input element of type file Upload Controller Map based on RequestMethod.POST Declare MultipartFile argument to bind file parameter A MultipartResolver bean must be registered in your servlet-context CommonsMultipartResolver most popular implementation Requires commons-fileupload and commons-io libraries Consider Tomcat 7 s (Servlet 3.0) native fileupload capability in the future 34

Demo File Upload org.springframework.samples.mvc.fileupload 35

Exception Handling Two-levels of Exception Handling @Controller level DispatcherServlet level @Controller level Annotate a separate method in your @Controller as a @ExceptionHandler Or simply catch the Exception yourself in your handler method DispatcherServlet level Rely on the DefaultHandlerExceptionResolver Maps common exceptions to appropriate status codes Supplement with your own custom HandlerExceptionResolver as needed 36

Demo Exception Handling org.springframework.samples.mvc.exceptions 37

Testing Unit Testing Controllers are just POJOs - just new them up and test them! Inject mock dependencies using your favorite mocking library (Mockito) HttpServlet Mocks Useful when you have a Servlet API dependency in your @Controller MockHttpServletRequest, MockHttpServletResponse, MockServletContext Integration Testing Selinium-based acceptance tests great way to exercise end-to-end behavior Also useful for automating the testing of @RequestMapping rules 38

Demo Testing 39

Resources Reference Manual http://www.springsource.org/documentation Samples http://src.springsource.org/svn/spring-samples/mvc-showcase http://src.springsource.org/svn/spring-samples Forum http://forum.springframework.org Issue Tracker http://jira.springsource.org/browse/spr Blog http://blog.springsource.com Twitter Follow @kdonald, @poutsma, @springrod, @benalexau 40

Enjoy being an application developer! Questions? 41