Spring Cookbook Over 100 hands-on recipes to build Spring web applications easily and efficiently Jérôme Jaglale BIRMINGHAM - MUMBAI Spring Cookbook Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: May 2015 Production reference: 1190515 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-580-7 www.packtpub.com FM-2 Credits Author Project Coordinator Jérôme Jaglale Harshal Ved Reviewers Proofreaders Alexandre Arcanjo de Queiroz Stephen Copestake Manuel Jordan Safis Editing Stanojko Markovik Bert Van den Brande Indexer Mariammal Chettiyar Commissioning Editor Julian Ursell Graphics Disha Haria Abhinash Sahu Acquisition Editor James Jones Production Coordinator Alwin Roy Content Development Editor Ajinkya Paranjape Cover Work Alwin Roy Technical Editor Siddhesh Ghadi Copy Editor Neha Vyas FM-3 About the Author Jérôme Jaglale is a web developer based in Vancouver, Canada. He has spent the last 10 years working with several web frameworks, including Struts, CodeIgniter, and Laravel, and doing frontend work with CSS and JavaScript that is focused on user experience. He's currently working at Simon Fraser University on a prototype of iReceptor, a scientific gateway federating immune genetics databases in order to help researchers answer complex questions about immune response. I would like to thank Laurent Conin and Michel Mac Wing at Orika for introducing me to Spring in 2005. I would like to express my thanks to Daniel Sheinin at 7th Floor Media for encouraging me to learn more about Spring in 2007. It resulted in Spring MVC Fast Tutorial, the most popular tutorial on http://jeromejaglale.com. I would also like to thank Brian Corrie at IRMACS for giving me some time to work on this book. I am grateful to Ajinkya Paranjape and the Packt Publishing staff for their hard work. I would like to express my gratitude to everyone else indirectly related to the making of this book: my parents, family, co-workers, and friends. FM-4 About the Reviewers Alexandre Arcanjo de Queiroz is a Brazilian software developer who graduated from the Faculty of Technology of Sao Paulo, a renowned institution in his country. He has experience in developing backend and frontend applications using the Java EE platform in the Unix environment. Currently, he is working at Geofusion. It is the leader in geomarketing in Brazil and offers an online platform called OnMaps, which is indispensable for companies seeking expansion and assistance in more accurate decision making. I would like to thank my family who support me in every moment of my life and my friends who believe in my potential. Stanojko Markovik is an accomplished software engineer, team leader, and solutions architect. Although he is versatile in many technologies across the board, he specializes in Spring for enterprise apps and Android for mobile. He's keen on building high-performance applications that are optimized in all layers and are used by millions of users across all continents. He's currently working at Dashlane, building the best password manager in the world in order to rid users of their need to enter another password ever again. The app he is building has been featured on the Google Play Store multiple times and is used by millions of users. Stanojko was also the technical reviewer for Instant Spring for Android Starter, a book that explains how to use the Spring framework on the Android platform. FM-5 Bert Van den Brande is a passionate developer with almost 15 years of experience in the software consultancy world. He has been working in the telecoms industry for the last 4 years. He started his career as a Java and Smalltalk developer. For the last half a decade, his focus has been on Java, Groovy, and team coaching. FM-6 www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print, and bookmark content f On demand and accessible via a web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. FM-7 Table of Contents Preface v Chapter 1: Creating a Spring Application 1 Introduction 1 Installing Java, Maven, Tomcat, and Eclipse on Mac OS 2 Installing Java, Maven, Tomcat, and Eclipse on Ubuntu 5 Installing Java, Maven, Tomcat, and Eclipse on Windows 8 Creating a Spring web application 13 Running a Spring web application 16 Using Spring in a standard Java application 18 Chapter 2: Defining Beans and Using Dependency Injection 23 Introduction 23 Defining a bean explicitly with @Bean 24 Defining a bean implicitly with @Component 25 Using a bean via dependency injection with @Autowired 27 Using a bean directly 28 Listing all beans 29 Using multiple configuration classes 30 Chapter 3: Using Controllers and Views 31 Introduction 32 Associating a route to a controller method 33 Using a JSP view 33 Passing attributes from a controller to a JSP view 36 Using dynamic route parameters in a controller method 36 Using a common prefix for the routes of a controller 37 Using a page template with Tiles 38 Executing some code before and after controllers using interceptors 43 Building multilingual pages 45 i