You are previewing Spring Cookbook.
O'Reilly logo
Spring Cookbook

Book Description

Over 100 hands-on recipes to build Spring web applications easily and efficiently

In Detail

Spring is the most popular Java web framework. It makes it easy to efficiently build modular and testable web applications by using the Model-View-Controller paradigm and dependency injection.

Using this practical guide, you'll learn how to build a full-featured web application step-by-step, but with high efficiency using the latest improvements in Spring 4. Starting with the installation of Java, Maven, and Tomcat, you'll go on to create a basic MVC web application. You'll then add a database, forms, and user authentication, and learn how to handle mobile devices and integrate with Facebook and Twitter. Next, you will be introduced to unit testing, web services, and aspect-oriented programming.

What You Will Learn

  • Install Maven and Tomcat and create a Spring MVC web application

  • Display, validate, and process forms

  • Query a database with minimal code

  • Authenticate users and manage their roles

  • Build REST, Java RMI, and Hessian web services

  • Create and schedule batch jobs

  • Offer customized page layouts to mobiles and tablets

  • Exchange data with Facebook and Twitter

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Spring Cookbook
      1. Table of Contents
      2. Spring Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Sections
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Creating a Spring Application
        1. Introduction
        2. Installing Java, Maven, Tomcat, and Eclipse on Mac OS
          1. How to do it…
            1. Installing Java
            2. Installing Maven
            3. Installing Tomcat
            4. Installing Eclipse
          2. There's more…
        3. Installing Java, Maven, Tomcat, and Eclipse on Ubuntu
          1. How to do it…
            1. Installing Java
            2. Installing Maven
            3. Installing Tomcat
            4. Installing Eclipse
          2. There's more…
        4. Installing Java, Maven, Tomcat, and Eclipse on Windows
          1. How to do it…
            1. Installing Java
            2. Installing Maven
            3. Installing Tomcat
            4. Installing Eclipse
        5. Creating a Spring web application
          1. How to do it…
            1. Creating a new Maven project in Eclipse
            2. Adding Spring to the project using Maven
            3. Creating the configuration classes for Spring
            4. Creating a "Hello World" web page
          2. How it works…
            1. Creating a new Maven project in Eclipse
            2. Adding Spring to the project using Maven
            3. Creating the configuration classes for Spring
            4. Creating a "Hello World" web page
        6. Running a Spring web application
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Using Spring in a standard Java application
          1. How to do it…
            1. Creating a new Maven project in Eclipse
            2. Adding Spring to the project using Maven
            3. Creating a configuration class for Spring
            4. Creating the User class
            5. Defining a User singleton in the Spring configuration class
            6. Using the User singleton in the main() method
          2. How it works...
      9. 2. Defining Beans and Using Dependency Injection
        1. Introduction
        2. Defining a bean explicitly with @Bean
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Defining a bean implicitly with @Component
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Using a bean via dependency injection with @Autowired
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Using a bean directly
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Listing all beans
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Using multiple configuration classes
          1. Getting ready
          2. How to do it…
          3. There's more…
      10. 3. Using Controllers and Views
        1. Introduction
        2. Associating a route to a controller method
          1. How to do it…
          2. How it works…
        3. Using a JSP view
          1. How to do it…
          2. How it works…
          3. There's more…
        4. Passing attributes from a controller to a JSP view
          1. How to do it…
          2. How it works…
        5. Using dynamic route parameters in a controller method
          1. How to do it…
          2. How it works…
        6. Using a common prefix for the routes of a controller
          1. How to do it…
          2. How it works…
        7. Using a page template with Tiles
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Organizing the JSP with subfolders
            2. Using multiple page templates
            3. Defining page titles only once using a text attribute
        8. Executing some code before and after controllers using interceptors
          1. How to do it…
          2. How it works…
          3. There's more…
        9. Building multilingual pages
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Getting the current language
            2. Using the language in the URL
      11. 4. Querying a Database
        1. Introduction
        2. Connecting to a database
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Creating a DAO class
          1. How to do it…
          2. How it works…
        4. Calling a DAO method from a controller class
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Saving an object
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Retrieving an object
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Retrieving a list of objects
          1. How to do it…
          2. How it works…
        8. Retrieving a list of objects with their dependencies
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Updating an object
          1. How to do it…
          2. There's more…
        10. Deleting an object
          1. How to do it…
        11. Finding the number of results for an SQL query
          1. How to do it…
        12. Saving a list of objects at once
          1. How to do it…
          2. How it works…
        13. Reverting incomplete database modifications using transactions
          1. How to do it…
          2. How it works…
        14. Using Hibernate for powerful object persistence and querying
          1. Getting ready
          2. How to do it…
      12. 5. Using Forms
        1. Introduction
        2. Displaying and processing a form
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        3. Getting a submitted form value using a controller method argument
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Setting a form's default values using a model object
          1. How to do it…
          2. How it works…
        5. Saving form data in an object automatically
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Using text, textarea, password, and hidden fields
          1. How to do it…
          2. How it works…
        7. Using a select field
          1. How to do it…
          2. How it works…
          3. There's more…
            1. Using a List<String> object
            2. Using a List<Object> object
        8. Using a checkbox
          1. How to do it…
          2. How it works…
        9. Using a list of checkboxes
          1. How to do it…
          2. How it works…
          3. There's more…
        10. Using a list of radio buttons
          1. How to do it…
          2. How it works…
          3. There's more…
        11. Validating a form using annotations
          1. How to do it…
          2. How it works…
          3. There's more…
        12. Uploading a file
          1. How to do it…
          2. How it works…
          3. There's more…
      13. 6. Managing Security
        1. Introduction
        2. Enabling Spring Security
          1. How to do it…
          2. How it works…
        3. Authenticating users using the default login page
          1. How to do it…
          2. How it works…
        4. Authenticating users using a custom login page
          1. How to do it…
          2. How it works…
        5. Authenticating users using a database
          1. How to do it…
          2. How it works…
        6. Adding a logout link
          1. How to do it…
          2. How it works…
        7. Using public folders
          1. How to do it…
          2. There's more…
        8. Authorizing only users with a specific role to view some pages
          1. How to do it…
          2. How it works…
        9. Displaying page elements only to authenticated users in views
          1. How to do it…
          2. How it works…
          3. There's more…
        10. Using HTTPS with Tomcat
          1. How to do it…
          2. How it works…
          3. There's more…
      14. 7. Unit Testing
        1. Introduction
        2. Unit testing with JUnit 4
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Unit testing with TestNG 6
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Simulating dependencies with mocks using Mockito
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Unit testing with JUnit 4 using Spring's application context
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Unit testing with TestNG 6 using Spring's application context
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Unit testing with transactions
          1. How to do it…
          2. How it works…
        8. Unit testing controller methods
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      15. 8. Running Batch Jobs
        1. Introduction
        2. Installing and configuring Spring Batch
          1. How to do it…
          2. How it works…
        3. Creating a job
          1. How to do it…
          2. How it works…
          3. There's more…
        4. Executing a job from the command line
          1. Getting Ready
          2. How to do it…
          3. There's more…
        5. Executing a job from a controller method
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Using job parameters
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Executing a system command
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Scheduling a job
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Creating a read/process/write step
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        10. Reading an XML file
          1. Getting ready
          2. How to do it…
          3. How it works…
        11. Generating a CSV file
          1. Getting ready
          2. How to do it…
          3. How it works…
        12. Reading from a database
          1. Getting ready
          2. How to do it…
          3. How it works…
        13. Unit testing batch jobs
          1. How to do it…
          2. How it works…
      16. 9. Handling Mobiles and Tablets
        1. Introduction
        2. Installing Spring Mobile
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Detecting mobiles and tablets
          1. How to do it…
          2. How it works…
        4. Switching to the normal view on mobiles
          1. How to do it…
          2. How it works…
          3. There's more…
        5. Using different JSP views for mobiles automatically
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Using a .mobi domain name on mobiles
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Using an m. subdomain on mobiles
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Using a different domain name on mobiles
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Using a subfolder path on mobiles
          1. Getting ready
          2. How to do it…
          3. How it works…
      17. 10. Connecting to Facebook and Twitter
        1. Introduction
        2. Creating a Facebook app
          1. Getting ready
          2. How to do it…
        3. Creating a test Facebook app and test users
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Connecting to Facebook
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Retrieving a Facebook user's profile
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Retrieving the list of friends of a Facebook user
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Posting a Facebook status update
          1. Getting ready
          2. How to do it…
        8. Posting a link to Facebook
          1. Getting ready
          2. How to do it…
        9. Posting a custom object to Facebook
          1. Getting ready
          2. How to do it…
        10. Creating a Twitter application
          1. Getting ready
          2. How to do it…
        11. Connecting to Twitter
          1. Getting ready
          2. How to do it…
          3. How it works…
        12. Retrieving a user's Twitter profile
          1. Getting ready
          2. How to do it…
        13. Retrieving the tweets of a Twitter user
          1. Getting ready
          2. How to do it…
        14. Posting a tweet to Twitter
          1. Getting ready
          2. How to do it…
        15. Sending a private message to another Twitter user
          1. Getting Ready
          2. How to do it…
      18. 11. Using the Java RMI, HTTP Invoker, Hessian, and REST
        1. Introduction
        2. Creating a Java RMI service
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Querying an existing Java RMI service
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Creating an HTTP Invoker service
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Querying an existing HTTP Invoker service
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Creating a Hessian service
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Querying an existing Hessian service
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Creating a REST service
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Querying an existing REST service
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      19. 12. Using Aspect-oriented Programming
        1. Introduction
        2. Creating a Spring AOP aspect class
          1. How to do it…
          2. How it works…
        3. Measuring the execution time of methods using an around advice
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Logging method arguments using a before advice
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Logging methods' return values using an after-returning advice
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Logging exceptions using an after-throwing advice
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Using an after advice to clean up resources
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Making a class implement an interface at runtime using an introduction
          1. Getting ready
          2. How to do it…
          3. How it works…
        9. Setting the execution order of the aspects
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      20. Index