34,79 €
Developing secure web applications is one of the most important tasks developers have to deal with. With Lift, it is easy to create solid and formidable web applications as it is the most secure web framework available today. The View-First approach and being able to handle things as purely data transformation, makes working with Lift an exciting task.
"Lift Application Development Cookbook" teaches you how to build web applications using this amazing framework. The book moves gradually, starting with the basics (starting a new project, submitting a form, and so on) before covering more advanced topics such as building a REST API and integrating your application with other technologies and applications.
"Lift Application Development Cookbook" takes you on a journey of creating secure web applications. Step-by-step instructions help you understand how things work and how various elements relate to each other.
You'll learn different ways to process a form, build dynamic HTML pages, and create an API using REST. You'll also learn how to work with relational and NoSQL databases and how to integrate your application with other technologies as well as with third-part applications such as Gmail and Facebook.
By the end of the book, you will be able to understand how Lift works and be able to build web applications using this amazing and exciting framework.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 290
Veröffentlichungsjahr: 2013
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]>)
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
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.
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.
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 <[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.
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.
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.
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:
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.
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.
To be able to follow the recipes, you will need to have Java 7 installed on your computer.
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.
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.
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.
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.
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 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 to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, we will learn about:
In this chapter, we will learn how to install Lift, which is a very secure web framework written in Scala; how to configure the basics of a Lift application such as how to define the SiteMap which is a comprehensive representation of the pages of a Lift application; and how to configure a log engine. We will also learn how to create an application using Simple Build Tool (SBT) or Maven.
SBT is a build tool for Scala and Java projects. The idea of using a build tool such as SBT is to make it easier to manage all of the project dependencies. Also, it helps to ensure that the application generated by the build process is always the same. This means that it doesn't matter whether the application is built on my computer or yours, the end result will be the same.
The easiest way to start with Lift and SBT is by downloading them from the official website. There, you can find a list of tar.gz and .zip files containing everything you need to start using Lift.
Scala, and in turn, the Lift development requires a JVM and because of this, you'll need to install Java 7 on your computer. However, you can skip this step if you already have it installed. If not then go to http://java.comand click on the Free Java Download button. Then, download and install the JDK appropriate for your OS.
Carry out the following steps:
When the download part is completed, you will get the SBT prompt that can be recognized by the > character.
After getting into the SBT prompt, type the following command to start the basic Lift application:The ZIP file contains some examples of Lift applications such as a blank application that you can use to start your application from scratch. It also contains a basic Lift application that contains Blueprint CSS and ready-to-use Mapper models, which you can use as the start point when building your own application.
The lift_basic folder contains a working Lift application. This means that you have SBT and a configured, ready-to-use project in it.
When we ran SBT, it started to download all the required libraries that the application needs (these dependencies are defined in the build.sbt file). Once this step is done, we can start the application.
After downloading the required libraries, we ran the container:start command provided by the sbt-web-plugin that deploys the Lift application into an embedded Jetty server.
You can see that inside the lift_basic application, there is a folder called project which contains a file called project.sbt. In that file, you will see that it defines three plugins for SBT. The first defined plugin is the XSBT plugin. After the XSBT plugin, there is the sbt-idea plugin and the sbteclipse plugin. The former is to enable SBT to be integrated with IntelliJ IDEA, and the latter enables SBT to be integrated with Scala IDE. Another thing to notice in the plugins.sbt file is that it matches the version of SBT to select the correct version of the sbt-web-plugin.
To install and run Lift on Linux or Mac, perform the following steps:
In the previous recipe, we learned how to use SBT to create and run a Lift application. Now I will show you how to set up and run a Lift application using Maven, which is another build tool.
If you don't have Maven installed and configured on your computer, go to http://maven.apache.org/download.cgi, download Maven 3.1.0, and follow the installation instructions.
We will use a Maven archetype that will create a ready-to-use Lift application for us.
After running the previous command, Maven will start to download all the required libraries to create the project.
Once the download is complete, Maven will ask you to confirm some information about the project. Confirm them by typing Y and pressing return.Change the following tags in the pom.xml file:From:
To:
From:
To:
From:
To:
From:
To:
From:
To:
When you create a project using the Lift archetype, you get a fully working application containing everything you need to build your own application. This means that Maven will create an application with its default directory structure, a pom.xml file, with everything needed by the sample application. It will also include the jetty plugin that will allow us to run the application by running the jetty:run command.
The application created by Maven is a sample application that contains Blueprint CSS and a Mapper model. One more thing to notice is that this archetype also includes plugins for IntelliJ IDEA and Scala IDE.
To learn more about Maven, please go to http://maven.apache.org/.
