Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Spring Cookbook
Spring Cookbook
Spring Cookbook
Ebook667 pages2 hours

Spring Cookbook

Rating: 0 out of 5 stars

()

Read preview

About this ebook

About This Book
  • Build full-featured web applications with Spring MVC
  • Use Spring 4 Java configuration style to write less code
  • Learn how to use dependency injection and aspect-oriented programming to write compartmentalized and testable code
Who This Book Is For

This book is for you if you have some experience with Java and web development (not necessarily in Java) and want to become proficient quickly with Spring.

LanguageEnglish
Release dateMay 25, 2015
ISBN9781783985814
Spring Cookbook

Related to Spring Cookbook

Related ebooks

Databases For You

View More

Related articles

Reviews for Spring Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Spring Cookbook - Jérôme Jaglale

    Table of Contents

    Spring Cookbook

    Credits

    About the Author

    About the Reviewers

    www.PacktPub.com

    Support files, eBooks, discount offers, and more

    Why Subscribe?

    Free Access for Packt account holders

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Sections

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Errata

    Piracy

    Questions

    1. Creating a Spring Application

    Introduction

    Installing Java, Maven, Tomcat, and Eclipse on Mac OS

    How to do it…

    Installing Java

    Installing Maven

    Installing Tomcat

    Installing Eclipse

    There's more…

    Installing Java, Maven, Tomcat, and Eclipse on Ubuntu

    How to do it…

    Installing Java

    Installing Maven

    Installing Tomcat

    Installing Eclipse

    There's more…

    Installing Java, Maven, Tomcat, and Eclipse on Windows

    How to do it…

    Installing Java

    Installing Maven

    Installing Tomcat

    Installing Eclipse

    Creating a Spring web application

    How to do it…

    Creating a new Maven project in Eclipse

    Adding Spring to the project using Maven

    Creating the configuration classes for Spring

    Creating a Hello World web page

    How it works…

    Creating a new Maven project in Eclipse

    Adding Spring to the project using Maven

    Creating the configuration classes for Spring

    Creating a Hello World web page

    Running a Spring web application

    How to do it…

    How it works…

    There's more…

    Using Spring in a standard Java application

    How to do it…

    Creating a new Maven project in Eclipse

    Adding Spring to the project using Maven

    Creating a configuration class for Spring

    Creating the User class

    Defining a User singleton in the Spring configuration class

    Using the User singleton in the main() method

    How it works...

    2. Defining Beans and Using Dependency Injection

    Introduction

    Defining a bean explicitly with @Bean

    How to do it…

    How it works…

    There's more…

    Defining a bean implicitly with @Component

    Getting ready

    How to do it…

    How it works…

    There's more…

    Using a bean via dependency injection with @Autowired

    Getting ready

    How to do it…

    How it works…

    There's more…

    Using a bean directly

    Getting ready

    How to do it…

    How it works…

    There's more…

    Listing all beans

    Getting ready

    How to do it…

    How it works…

    There's more…

    Using multiple configuration classes

    Getting ready

    How to do it…

    There's more…

    3. Using Controllers and Views

    Introduction

    Associating a route to a controller method

    How to do it…

    How it works…

    Using a JSP view

    How to do it…

    How it works…

    There's more…

    Passing attributes from a controller to a JSP view

    How to do it…

    How it works…

    Using dynamic route parameters in a controller method

    How to do it…

    How it works…

    Using a common prefix for the routes of a controller

    How to do it…

    How it works…

    Using a page template with Tiles

    How to do it…

    How it works…

    There's more…

    Organizing the JSP with subfolders

    Using multiple page templates

    Defining page titles only once using a text attribute

    Executing some code before and after controllers using interceptors

    How to do it…

    How it works…

    There's more…

    Building multilingual pages

    How to do it…

    How it works…

    There's more…

    Getting the current language

    Using the language in the URL

    4. Querying a Database

    Introduction

    Connecting to a database

    Getting ready

    How to do it…

    How it works…

    Creating a DAO class

    How to do it…

    How it works…

    Calling a DAO method from a controller class

    Getting ready

    How to do it…

    How it works…

    Saving an object

    Getting ready

    How to do it…

    How it works…

    Retrieving an object

    How to do it…

    How it works…

    There's more…

    Retrieving a list of objects

    How to do it…

    How it works…

    Retrieving a list of objects with their dependencies

    Getting ready

    How to do it…

    How it works…

    Updating an object

    How to do it…

    There's more…

    Deleting an object

    How to do it…

    Finding the number of results for an SQL query

    How to do it…

    Saving a list of objects at once

    How to do it…

    How it works…

    Reverting incomplete database modifications using transactions

    How to do it…

    How it works…

    Using Hibernate for powerful object persistence and querying

    Getting ready

    How to do it…

    5. Using Forms

    Introduction

    Displaying and processing a form

    How to do it…

    How it works…

    There's more…

    See also

    Getting a submitted form value using a controller method argument

    How to do it…

    How it works…

    There's more…

    See also

    Setting a form's default values using a model object

    How to do it…

    How it works…

    Saving form data in an object automatically

    How to do it…

    How it works…

    There's more…

    Using text, textarea, password, and hidden fields

    How to do it…

    How it works…

    Using a select field

    How to do it…

    How it works…

    There's more…

    Using a List object

    Using a List object

    Using a checkbox

    How to do it…

    How it works…

    Using a list of checkboxes

    How to do it…

    How it works…

    There's more…

    Using a list of radio buttons

    How to do it…

    How it works…

    There's more…

    Validating a form using annotations

    How to do it…

    How it works…

    There's more…

    Uploading a file

    How to do it…

    How it works…

    There's more…

    6. Managing Security

    Introduction

    Enabling Spring Security

    How to do it…

    How it works…

    Authenticating users using the default login page

    How to do it…

    How it works…

    Authenticating users using a custom login page

    How to do it…

    How it works…

    Authenticating users using a database

    How to do it…

    How it works…

    Adding a logout link

    How to do it…

    How it works…

    Using public folders

    How to do it…

    There's more…

    Authorizing only users with a specific role to view some pages

    How to do it…

    How it works…

    Displaying page elements only to authenticated users in views

    How to do it…

    How it works…

    There's more…

    Using HTTPS with Tomcat

    How to do it…

    How it works…

    There's more…

    7. Unit Testing

    Introduction

    Unit testing with JUnit 4

    Getting ready

    How to do it…

    How it works…

    There's more…

    Unit testing with TestNG 6

    Getting ready

    How to do it…

    How it works…

    There's more…

    Simulating dependencies with mocks using Mockito

    Getting ready

    How to do it…

    How it works…

    There's more…

    Unit testing with JUnit 4 using Spring's application context

    How to do it…

    How it works…

    There's more…

    Unit testing with TestNG 6 using Spring's application context

    How to do it…

    How it works…

    There's more…

    Unit testing with transactions

    How to do it…

    How it works…

    Unit testing controller methods

    Getting ready

    How to do it…

    How it works…

    There's more…

    8. Running Batch Jobs

    Introduction

    Installing and configuring Spring Batch

    How to do it…

    How it works…

    Creating a job

    How to do it…

    How it works…

    There's more…

    Executing a job from the command line

    Getting Ready

    How to do it…

    There's more…

    Executing a job from a controller method

    Getting ready

    How to do it…

    How it works…

    Using job parameters

    Getting ready

    How to do it…

    How it works…

    Executing a system command

    Getting ready

    How to do it…

    How it works…

    There's more…

    Scheduling a job

    Getting ready

    How to do it…

    How it works…

    There's more…

    Creating a read/process/write step

    Getting ready

    How to do it…

    How it works…

    There's more…

    Reading an XML file

    Getting ready

    How to do it…

    How it works…

    Generating a CSV file

    Getting ready

    How to do it…

    How it works…

    Reading from a database

    Getting ready

    How to do it…

    How it works…

    Unit testing batch jobs

    How to do it…

    How it works…

    9. Handling Mobiles and Tablets

    Introduction

    Installing Spring Mobile

    How to do it…

    How it works…

    There's more…

    Detecting mobiles and tablets

    How to do it…

    How it works…

    Switching to the normal view on mobiles

    How to do it…

    How it works…

    There's more…

    Using different JSP views for mobiles automatically

    How to do it…

    How it works…

    There's more…

    Using a .mobi domain name on mobiles

    Getting ready

    How to do it…

    How it works…

    Using an m. subdomain on mobiles

    Getting ready

    How to do it…

    How it works…

    Using a different domain name on mobiles

    Getting ready

    How to do it…

    How it works…

    Using a subfolder path on mobiles

    Getting ready

    How to do it…

    How it works…

    10. Connecting to Facebook and Twitter

    Introduction

    Creating a Facebook app

    Getting ready

    How to do it…

    Creating a test Facebook app and test users

    Getting ready

    How to do it…

    How it works…

    Connecting to Facebook

    Getting ready

    How to do it…

    How it works…

    Retrieving a Facebook user's profile

    Getting ready

    How to do it…

    How it works…

    There's more…

    Retrieving the list of friends of a Facebook user

    Getting ready

    How to do it…

    How it works…

    Posting a Facebook status update

    Getting ready

    How to do it…

    Posting a link to Facebook

    Getting ready

    How to do it…

    Posting a custom object to Facebook

    Getting ready

    How to do it…

    Creating a Twitter application

    Getting ready

    How to do it…

    Connecting to Twitter

    Getting ready

    How to do it…

    How it works…

    Retrieving a user's Twitter profile

    Getting ready

    How to do it…

    Retrieving the tweets of a Twitter user

    Getting ready

    How to do it…

    Posting a tweet to Twitter

    Getting ready

    How to do it…

    Sending a private message to another Twitter user

    Getting Ready

    How to do it…

    11. Using the Java RMI, HTTP Invoker, Hessian, and REST

    Introduction

    Creating a Java RMI service

    Getting ready

    How to do it…

    How it works…

    Querying an existing Java RMI service

    Getting ready

    How to do it…

    How it works…

    Creating an HTTP Invoker service

    Getting ready

    How to do it…

    How it works…

    Querying an existing HTTP Invoker service

    Getting ready

    How to do it…

    How it works…

    Creating a Hessian service

    Getting ready

    How to do it…

    How it works…

    Querying an existing Hessian service

    Getting ready

    How to do it…

    How it works…

    Creating a REST service

    Getting ready

    How to do it…

    How it works…

    There's more…

    Querying an existing REST service

    Getting ready

    How to do it…

    How it works…

    There's more…

    12. Using Aspect-oriented Programming

    Introduction

    Creating a Spring AOP aspect class

    How to do it…

    How it works…

    Measuring the execution time of methods using an around advice

    Getting ready

    How to do it…

    How it works…

    Logging method arguments using a before advice

    Getting ready

    How to do it…

    How it works…

    Logging methods' return values using an after-returning advice

    Getting ready

    How to do it…

    How it works…

    Logging exceptions using an after-throwing advice

    Getting ready

    How to do it…

    How it works…

    Using an after advice to clean up resources

    Getting ready

    How to do it…

    How it works…

    Making a class implement an interface at runtime using an introduction

    Getting ready

    How to do it…

    How it works…

    Setting the execution order of the aspects

    Getting ready

    How to do it…

    How it works…

    There's more…

    Index

    Spring Cookbook


    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

    Credits

    Author

    Jérôme Jaglale

    Reviewers

    Alexandre Arcanjo de Queiroz

    Manuel Jordan

    Stanojko Markovik

    Bert Van den Brande

    Commissioning Editor

    Julian Ursell

    Acquisition Editor

    James Jones

    Content Development Editor

    Ajinkya Paranjape

    Technical Editor

    Siddhesh Ghadi

    Copy Editor

    Neha Vyas

    Project

    Enjoying the preview?
    Page 1 of 1