The-Definitive-Guide-To-Lift.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN OPEN SOURCE Companion eBook The Definitive Guide to Lift: Available A Scala-Based Web Framework The Definitive Guide to Dear Reader, We would like to welcome you to the exciting world of Lift! Lift is a new web The Definitive Guide to application framework based on the Scala programming language. It takes a refreshing approach to designing web applications by utilizing Scala’s support for XML, functions as objects, and concise syntax. By drawing on Scala, Lift greatly reduces the need for boilerplate code and becomes the basis for a flex- ible XHTML template system based on the view-first approach to writing web pages. This concise book is designed to provide a reference for every aspect of designing a Lift application, from basic pages to database access and advanced URL handling. We leverage Lift’s use of Maven to quickly start a small applica- tion and then examine each part of the application in detail to show how Lift can be customized to fit your requirements. With this book, we hope to show you just how fun and easy you’ll find writ- ing web applications with Lift and Scala. Whether you’re coming from a back- ground in one of the many Java™ web frameworks or just starting out, we think Lift that you’ll appreciate how much Lift handles for you. Derek Chen-Becker, Marius Danciu, and Tyler Weir Lift RELATED TITLES A Scala-Based Web Framework Derek Chen-Becker, Marius Danciu, Learn and apply the Lift web framework in creating Tyler Weir Weir Danciu, Chen-Becker, Scala-based web applications from the actual Companion eBook contributors and professionals who are building Lift. See last page for details on $10 eBook version SOURCE CODE ONLINE Derek Chen-Becker, Marius Danciu, www.apress.com and Tyler Weir Shelve in Java Programming User level: Beginner–Intermediate About firstPress Apress’s firstPress series is your source for understanding cutting-edge technology. Short, highly focused, and written by experts, Apress’s firstPress books save you time and effort. They contain the information you could get based on intensive research yourself or if you were to attend a conference every other week—if only you had the time. They cover the concepts and techniques that will keep you ahead of the technology curve. Apress’s firstPress books are real books, in your choice of electronic or print-on-demand format, with no rough edges even when the technology itself is still rough. You can't afford to be without them. The Definitive Guide to Lift: A Scala-Based Web Framework Dear Reader, We would like to welcome you to the exciting world of Lift! Lift is a new web application framework based on the Scala programming language. It takes a refreshing approach to designing web applications by utilizing Scala’s support for XML, functions as objects, and concise syntax. By drawing on Scala, Lift greatly reduces the need for boilerplate code and becomes the basis for a flexible XHTML template system based on the view-first approach to writing web pages. This concise book is designed to provide a reference for every aspect of designing a Lift application, from basic pages to database access and advanced URL handling. We leverage Lift’s use of Maven to quickly start a small application and then examine each part of the application in detail to show how Lift can be customized to fit your requirements. With this book, we hope to show you just how fun and easy you’ll find writing web applications with Lift and Scala. Whether you’re coming from a background in one of the many Java™ web frameworks or just starting out, we think that you’ll appreciate how much Lift handles for you. Derek Chen-Becker, Marius Danciu, and Tyler Weir Contents Chapter 1: Welcome to Lift ................................................................................ 1 Lifting Off!.............................................................................................................................1 Implementing the MVC Pattern with Lift ........................................................................................ 1 Leveraging the Scala Language........................................................................................................ 2 Supporting Advanced Features Easily.............................................................................................. 3 Getting to Know the Lift Community ....................................................................................3 Creating Your First Lift Application.....................................................................................4 Conclusion..............................................................................................................................9 Chapter 2: PocketChange ............................................................................... 11 Keeping Track of Your PocketChange................................................................................11 Defining the Model ..............................................................................................................12 Creating Your First Template..............................................................................................15 Writing Snippets...................................................................................................................16 Sprinkling a Little AJAX Spice ...........................................................................................19 Conclusion............................................................................................................................23 Chapter 3: Lift Fundamentals........................................................................... 25 Entering Lift.........................................................................................................................25 Making Standard import Assumptions for This Book........................................................26 Bootstrapping in Lift............................................................................................................26 Using LiftRules .............................................................................................................................. 26 Resolving Classes........................................................................................................................... 27 Rendering in Lift..................................................................................................................27 Rendering with Templates.............................................................................................................. 28 Rendering with Views .................................................................................................................... 29 Getting to Know the Lift Tags........................................................................................................ 31 Merging HTML Headings.............................................................................................................. 34 The Definitive Guide to Lift i Providing User Feedback ............................................................................................................... 35 Using Snippets................................................................................................................................ 35 Rewriting URLs ............................................................................................................................. 38 Adding Custom Dispatch Functions............................................................................................... 41 Using HTTP Redirects ................................................................................................................... 43 Using Cookies.......................................................................................................................43 Storing Session and Request States.....................................................................................44 Gathering a Few More Useful Objects................................................................................46 S object........................................................................................................................................... 46 SHtml ............................................................................................................................................. 46 Conclusion............................................................................................................................46 Chapter 4: Forms in Lift ................................................................................... 47 Practicing Form Fundamentals ..........................................................................................47 checkbox......................................................................................................................................... 52 hidden............................................................................................................................................. 53 link.................................................................................................................................................. 53 text and password ........................................................................................................................... 54 textarea ..........................................................................................................................................