Explore 1.5M+ audiobooks & ebooks free for days

From $11.99/month after trial. Cancel anytime.

Lift Application Development Cookbook
Lift Application Development Cookbook
Lift Application Development Cookbook
Ebook645 pages3 hours

Lift Application Development Cookbook

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Lift Application Development Cookbook contains practical recipes on everything you will need to create secure web applications using this amazing framework.The book first teaches you basic topics such as starting a new application and gradually moves on to teach you advanced topics to achieve a certain task. Then, it explains every step in detail so that you can build your knowledge about how things work.This book is for developers who have at least some basic knowledge about Scala and who are looking for a functional, secure, and modern web framework. Prior experience with HTML and JavaScript is assumed.
LanguageEnglish
PublisherPackt Publishing
Release dateSep 25, 2013
ISBN9781849515894
Lift Application Development Cookbook

Related to Lift Application Development Cookbook

Related ebooks

Internet & Web For You

View More

Reviews for Lift Application Development 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

    Lift Application Development Cookbook - Gilberto T. Garcia Jr.

    (missing alt)

    Table of Contents

    Lift Application Development 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

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Errata

    Piracy

    Questions

    1. Getting Started with Lift Basics

    Introduction

    Creating a Lift application using SBT

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Creating a Lift application using Maven

    Getting ready

    How to do it...

    How it works...

    See also

    Defining a SiteMap

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Logging using logback

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Sending e-mails using Gmail's SMTP server

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    2. Working with HTML

    Introduction

    Transforming HTML elements using their IDs

    Getting ready

    How to do it...

    How it works...

    See also

    Creating an HTML list using CSS selectors

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Creating an HTML table with dynamically defined columns

    Getting ready

    How to do it...

    How it works...

    See also

    Nesting snippets

    Getting ready

    How to do it...

    How it works...

    See also

    Dynamic menu rendering

    Getting ready

    How to do it...

    How it works...

    See also

    Localizing templates

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Testing snippets using Specs2

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Creating custom error pages

    Getting ready

    How to do it...

    How it works...

    There's more...

    Sending JavaScript commands from the server

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Invoking server-side functions from the client

    Getting ready

    How to do it...

    How it works...

    See also

    3. Working with Forms

    Introduction

    Creating forms

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Validating forms

    Getting ready

    How to do it...

    How it works...

    There's more...

    Creating an Ajax form

    Getting ready

    How to do it...

    How it works...

    There's more...

    Creating multipage forms

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Defining a relation between form fields

    Getting ready

    How to do it...

    How it works...

    There's more...

    4. Working with REST

    Introduction

    Getting data from the server

    Getting ready

    How to do it...

    How it works...

    There's more...

    Creating data using the REST service

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Modifying data using the REST service

    Getting ready

    How to do it...

    How it works...

    Deleting data using the REST service

    Getting ready

    How to do it...

    How it works...

    Testing a REST service using Specs2

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Uploading a file using the REST service

    Getting ready

    How to do it...

    How it works...

    Creating an RSS feed

    Getting ready

    How to do it...

    How it works...

    See also

    5. Working with Databases

    Introduction

    Configuring a connection to database

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also...

    Mapping a table to a Scala class

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also...

    Creating one-to-many relationships

    Getting ready

    How to do it...

    How it works...

    See also...

    Creating many-to-many relationships

    Getting ready

    How to do it...

    How it works...

    See also...

    Creating CRUD features with CRUDify

    Getting ready

    How to do it...

    How it works...

    There's more...

    Paginating result sets

    Getting ready

    How to do it...

    How it works...

    There's more...

    Using an in-memory database in application tests

    Getting ready

    How to do it...

    How it works...

    There's more...

    6. Working with Record

    Introduction

    Configuring a connection to a database using Squeryl

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Mapping a table to a Scala class

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Migrating the database using Liquibase

    Getting ready...

    How to do it...

    How it works...

    See also

    Creating one-to-many relationships

    Getting ready

    How to do it...

    How it works...

    Creating many-to-many relationships

    Getting ready

    How to do it...

    How it works...

    Paginating result sets using Record

    Getting ready

    How to do it...

    How it works...

    See also

    Testing your application using an in-memory database

    Getting ready

    How to do it...

    How it works...

    See also

    7. Working with MongoDB

    Introduction

    Connecting to MongoDB using record

    Getting ready

    How to do it...

    How it works...

    Mapping a MongoDB collection to a Scala class

    Getting ready

    How to do it...

    How it works...

    See also

    Mapping embedded objects

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also

    Mapping referenced objects

    Getting ready

    How to do it...

    How it works...

    See also...

    Querying with Rogue

    Getting ready

    How to do it...

    How it works...

    See also...

    8. Integrating Lift with Social Media

    Introduction

    Signing up using a Facebook account

    Getting ready

    How to do it...

    How it works...

    There's more...

    See also...

    Fetching a user's Facebook data

    Getting ready

    How to do it...

    How it works...

    See also...

    Signing up using a Gmail account

    Getting ready

    How to do it...

    How it works...

    See also...

    Fetching a user's Gmail data

    Getting ready

    How to do it...

    How it works...

    See also...

    Signing up using a LinkedIn account

    Getting ready

    How to do it...

    How it works...

    See also...

    Fetching a user's LinkedIn data

    Getting ready

    How to do it...

    How it works...

    Signing up using a Twitter account

    Getting ready

    How to do it...

    How it works...

    See also...

    Fetching a user's Twitter data

    Getting ready

    How to do it...

    How it works...

    Index

    Lift Application Development Cookbook


    Lift Application Development Cookbook

    Copyright © 2013 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: September 2013

    Production Reference: 1190913

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-84951-588-7

    www.packtpub.com

    Cover Image by Suresh Mogre (<[email protected]>)

    Credits

    Author

    Gilberto T. Garcia Jr.

    Reviewers

    Diego Medina

    Peter Petersson (karma4u101)

    Peter Robinett

    Paulo Suzart

    Acquisition Editor

    Vinay Argekar

    Lead Technical Editor

    Azharuddin Sheikh

    Technical Editors

    Jalasha D'costa

    Akashdeep Kundu

    Proshonjit Mitra

    Shiny Poojary

    Copy Editors

    Brandt D'Mello

    Mradula Hegde

    Gladson Monteiro

    Project Coordinator

    Angel Jathanna

    Proofreaders

    Simran Bhogal

    Ameesha Green

    Indexer

    Rekha Nair

    Graphics

    Abhinash Sahu

    Production Coordinator

    Nilesh R. Mohite

    Cover Work

    Nilesh R. Mohite

    About the Author

    Gilberto T. Garcia Jr. has a Bachelor's degree in Philosophy from USP, and has been working with Internet-related technologies since 1999. He had worked with different programming languages in several projects with different sizes and complexities.

    As a person who enjoys learning new things, he started to study and work with Scala and Lift in 2010.

    I want to thank my wife, Lavinia, for all the love and encouragement to write this book, and for her support and patience during the writing process. I also want to thank my family and friends.

    Thanks to David Pollak for creating Lift, and thanks to the Lift committers and everyone who makes the Lift community such a nice place to be around.

    About the Reviewers

    Diego Medina lives on the mountains of North Carolina with his wife, daughter, and their three cats. He has been a developer for the past 12 years, and his focus has been on web development and more specifically, web security.

    He is a proud Lift committer and a very active member of the Lift community, answering questions on the mailing list as well as writing articles on his personal blog.

    Peter Petersson lives in the south of Sweden with his wife and one-year-old boy.

    He has been a developer for 18 years. He started his developer career at Ericsson Software Technology mostly using C++, but at the time Java was emerging, he soon jumped onto that bandwagon. He has been a co-owner in a tour operator information system company and is currently a system developer and consultant at Avalon Innovation.

    He started looking at Scala and functional programing at the beginning of 2011, about the same time he found Lift to be an awesome web framework. He is a proud Lift committer and creator of the Lift FoBo, Front End Toolkit Module, as well as the jQuery module.

    Peter Robinett is a web and mobile developer based in Amsterdam. He is passionate about Scala and is a committer to the Lift project. He works under the name Bubble Foundry, and blogs occasionally at www.bubblefoundry.com.

    Paulo Suzart worked in different types of companies in the last 10 years, from e-commerce to insurance, as a Java programmer and lately as an SOA specialist. He currently runs a digital media startup as CTO.

    He truly believes that startups are open fields for new technologies and functional programming languages, such as Scala and Clojure.

    www.PacktPub.com

    Support files, eBooks, discount offers and more

    You might want to visit www.PacktPub.com for support files and downloads related to your book.

    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 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.

    Support files, eBooks, discount offers and more

    http://PacktLib.PacktPub.com

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. 

    Why Subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print and bookmark content

    On demand and accessible via 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 nine entirely free books. Simply use your login credentials for immediate access.

    Preface

    Lift is a web framework built in Scala. Its main goal is to be a secure framework that helps developers to build scalable web applications in a concise and maintainable way.

    There are six things—according to the official website, http://liftweb.net/—that makes Lift different from any other web framework available today:

    Secure: Lift apps are resistant to common vulnerabilities, including many of the OWASP Top 10 projects

    Developer-centric: Lift apps are fast to build, concise, and easy to maintain

    Designer-friendly: Lift apps can be developed in a totally designer-friendly way

    Scalable: Lift has a number of high-performance apps, and they scale in the real world to handle insane traffic levels

    Modular: Lift apps can benefit from easy-to-integrate, pre-built modules

    Interactive like a desktop app: Lift's Comet support is unparalleled, and Lift's Ajax support is super easy and very secure

    The goal of this book is to introduce you to the basics of Lift, and teach you everything that you need to know to build applications using Lift.

    By providing you with hands-on examples, we hope that you find this book useful as your first introduction to Lift, and also as a reference guide when building your own web applications.

    What this book covers

    Chapter 1, Getting Started with Lift Basics, covers the basics of Lift which include how to start a new application using build tools such as SBT and Maven. It also explains how to define the application structure using SiteMap object and how to use the Mailer object to send e-mails.

    Chapter 2, Working with HTML, introduces the reader to CSS selectors and shows how to use them to transform the HTML that will be rendered in the browser. It also covers how to test snippets, how to generate a JavaScript code from the server, and how to invoke server-side functions using JavaScript. It also explains how to localize templates.

    Chapter 3, Working with Forms, covers the different ways the reader can work with forms, such as how to create a single page form and a wizard-like form. It also explains how to submit forms using Ajax and how to wire form fields.

    Chapter 4, Working with REST, introduces Lift's RestHelper object and how to use it to create a REST API. It also explains how to test the REST API, how to upload files, and how to create an RSS feed.

    Chapter 5, Working with Databases, explains how to use Mapper to integrate the application with a database. It also covers the basics of ORM mapping and how to use an in-memory database to test an application that uses Mapper.

    Chapter 6, Working with Record, explains how to integrate a Lift application with a database using Record and Squeryl. It also covers how to test an application that uses Record and Squeryl using an in-memory database.

    Chapter 7, Working with MongoDB, covers the integration of a Lift application with MongoDB, how to use Record to create such an integration, and explains how to query MongoDB using Rogue.

    Chapter 8, Integrating Lift with Social Media, explains how to use social login to authenticate users and how to get their data from their Facebook, Gmail, Twitter, or LinkedIn accounts.

    What you need for this book

    To be able to follow the recipes, you will need to have Java 7 installed on your computer.

    Who this book is for

    This book is for developers who want to learn how to develop web applications using the Lift framework. However, we assume that the reader at least knows the basics of Scala, HTML, and JavaScript.

    Conventions

    In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

    Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: The bigger difference lies in the snippet code. You can see that after getting the user data, we've invoked a method called fetchUserData and used its result to change the contents of a li tag.

    A block of code is set as follows:

    import code.lib.googleSession

    import net.liftweb.util.BindHelpers._

    import net.liftweb.common.Full

     

    object GmailData {

      def render = {

        googleSession.get match {

          case Full(email) => .email #> email

          case _ =>  * #> Not authorized

        }

      }

    }

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

            {longDate(Calendar.getInstance().getTimeInMillis)}         {ClientCache.clients.flatMap {

     

            c =>

             

                Client: {c.id}

                Name: {c.name} - E-mail: {c.email}

    Any command-line input or output is written as follows:

    scala> val transformSpanContent = span * #> Some Text

    New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: If you select a name in the combobox and click on the Delete Selected button, you'll see an alert saying that the name you selected was deleted.

    Note

    Warnings or important notes appear in a box like this.

    Tip

    Tips and tricks appear like this.

    Reader feedback

    Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

    To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.

    If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

    Customer support

    Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

    Downloading the example code

    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.

    Errata

    Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

    Piracy

    Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

    Please contact us at <[email protected]> with a link

    Enjoying the preview?
    Page 1 of 1