Starting with Lift

Author: Derek Chen-Becker, Marius Danciu, David Pollak, Tyler Weir. Link to original: http://liftweb.net/docs/getting_started/mod_master.html (English).
Tags: framevork, Java, Lift, scala, web Submitted by achepkunov 15.02.2010. Public material.
Lift is designed to make powerful techniques easily accessible, while keeping the overall framework simple and flexible. It may sound like a cliché, but in our experience Lift makes it fun to develop because it lets you focus on the interesting parts of coding. Our goal for this book is that by the end, you’ll be able to create and extend any web application you can think of.

Translations of this material:

into Russian: Начинаем работать с Лифтом. 99% translated in draft. Almost done, let's finish it!
Submitted for translation by achepkunov 15.02.2010

Text

Contents

1 Welcome to Lift!

1.1 Why Lift?

1.2 For more information

1.3 Your first Lift application

2 Basic Lift

2.1 What we’re going to cover

2.2 Creating a new project

2.3 Adding a model

2.4 Boot and Schemifier

2.5 Snippets: Bridging View and Logic

2.6 Lift’s “View First” design

2.7 Updating the ToDo view

2.8 Seeing if the snippet works

2.9 Creating a to-do item

2.10 Updating the priority and desc fields

2.11 Hooking up the view to the form

2.12 Trying it out again

2.13 Display and Editting to-do items

1 Welcome to Lift!

Lift is designed to make powerful techniques easily accessible, while keeping the overall framework simple and flexible. It may sound like a cliché, but in our experience Lift makes it fun to develop because it lets you focus on the interesting parts of coding. Our goal for this book is that by the end, you’ll be able to create and extend any web application you can think of.

1.1 Why Lift?

For those of you have experience with other web frameworks such as Struts, Tapestry, Rails, et cetera, you must be asking yourself "Why another framework? Does Lift really solve problems any differently or more effectively than the ones I’ve used before?" Based on our experience (and of others in the growing Lift community), the answer is an emphatic "Yes!" Lift has cherry-picked the best ideas from a number of other frameworks, while creating some novel ideas of its own. It’s this combination of solid foundation and new techniques that makes Lift so powerful. At the same time, Lift has been able to avoid the mistakes made in the past by other frameworks. In the spirit of “configuration by convention”, Lift has sensible defaults for everything, while making it easy to customize precisely what you need to; no more and no less. Gone are the days of XML file after XML file providing basic configuration for your application. Instead, a basic Lift app only requires that you add the LiftFilter to your web.xml and add one or more lines telling Lift what package your classes sit in. The methods you code aren’t required to implement a specific interface (called a trait), although there are support traits that make things that much simpler. In short, you don’t need to write anything that isn’t explicitly necessary for the task at hand; Lift is intended to work out of the box, and to make you as efficient and productive as possible.

One of the key strengths of Lift is the clean separation of presentation content and logic, based on the bedrock concept of the Model-View-Controller pattern1 . One of the original Java web application technologies that’s still in use today is JSP, or Java Server Pages2 . JSP allows you to mix HTML and Java code directly within the page. While this may have seemed like a good idea at the start, it has proven to be painful in practice. Putting code in your presentation layer makes it more difficult to debug and understand what is going on within a page, and makes it more difficult for the people writing the HTML portion because the contents aren’t valid HTML. While many modern programming and HTML editors have been modified to accomodate this mess, proper syntax highlighting and validation don’t make up for the fact that you still have to switch back and forth between one or more files to follow the page flow. Lift takes the approach that there should be no code in the presentation layer, but that the presentation layer has to be flexible enough to accomodate any conceivable uses. To that end, Lift uses a powerful templating system, a la Wicket3 , to bind user-generated data into the presentation layer. Lift’s templating is built on the XML processing capabilities of the Scala Language4 , and allows things such as nested templates, simple injection of user-generated content, and advanced data binding capabilities. For those coming from JSP, Lift’s advanced template and XML processing allows you to essentially write custom tag libraries at a fraction of the cost in time and effort.

Lift has another advantage that no other web framework currently shares: the Scala programming language. Scala is a relatively new language developed by Martin Odersky5 and his group at EPFL Switzerland. It compiles to Java bytecode and runs on the JVM, which means that you can leverage the vast ecosystem of Java libraries just as you would with any other java web framework. At the same time, Scala introduces some very powerful features designed to make you, the developer, more productive. Among these features are an extremely rich type system along with powerful type inference, native XML processing, full support for closures and functions as objects, and an extensive high-level library. The power of the type system along with its type inferencing has led people to call it “the statically typed dynamic language”6 . That means you can write code as quickly as you could with dynamically typed languages (Python, Ruby, etc.), but you have the compile-time type safety of a statically typed language like Java. Scala is also a hybrid functional and Object-oriented language, which means you can get the power of the higher-level functional (or FP) languages (such as Haskell, Scheme, etc) while retaining the modularity and reusability of OO components. In particular, the FP concept of immutability is well represented in Scala, and is one of the simplest means to high throughput scalability. The hybrid model also means that if you haven’t touched FP before, you can gradually ease into it. In our experience, Scala allows you to do more in Lift with less lines of code; remember, Lift is all about making you more productive!

Lift strives to encompass advanced features in a very concise and straightforward manner. Lift’s powerful support for AJAX and COMET allow you to use Web 2.0 features with very little effort. Lift leverages Scala’s Actor library to provide a message-driven framework for COMET updates. In most cases, adding COMET support to a page just involves extending a trait7 to define the rendering method of your page and adding an extra function call to your links to dispatch the update message; Lift handles all of the backend and page-side coding to effect the COMET polling. AJAX support includes special handlers for doing AJAX form submission via JSON, and almost any link function can easily be turned into an AJAX version with a few keystrokes. In order to preform all of this client-side goodness, Lift has a class hierarchy for encapsulating Javascript calls via direct Javascript, jQuery and YUI. The nice part is that you, too, can utilize these support classes so that you can generate the code and don’t have to put Javascript logic into your templates.

1.2 For more information

Lift has a very active community of users and developers. Since its inception in early 2007 the community has grown to hundreds of members from all over the world. The project’s leader, David Pollak8 is constantly attending to the mailing list, answering questions and taking feature requests. There is a core group of developers who work on the project, but submissions are taken from anyone who makes a good case and can turn in good code. While we strive to cover everything you’ll need to know in this book, here are several additional resources available for information on Lift:

1. The first place to look is the Wiki at http://liftweb.net/index.php/Main_Page. The Wiki is maintained not only by David, but by many active members of the Lift community (including the authors). Portions of this book as inspired by and borrow from content on the Wiki. In particular, it has links to all of the generated documentation not only for the stable branch, but for the unstable head if you’re feeling adventurous. There’s also an extensive section of HowTos and articles on advanced topics that covers a wealth of information.

2. The mailing list at http://groups.google.com/group/liftweb is very active and if there are things that this book doesn’t cover you can feel free to ask questions there; there are plenty of very knowledgeable people on the list that should be able to answer your questions. Please post specific questions about the book to the Lift Book Google Group found http://groups.google.com/group/the-lift-book, anything else that is Lift-specific is fair game for the mailing list.

3. Lift has an IRC channel at irc://irc.freenode.net/lift that usually has several people on at any given time. It’s a great place to chat about issues and ideas concerning Lift.

1.3 Your first Lift application

We’ve talked a lot about Lift and its capabilities, so now let’s get hands-on and try out an application. Before we start, though, we need to take care of some prerequisites:

Java 1.5 JDK

Lift runs on Scala, which runs on top of the JVM; the first thing you’ll need to install is a modern version of the Java SE JVM, available at http://java.sun.com/. Please install JDK 1.5 or newer. If you’re using a Mac, the JDK is already installed.

Maven 2

Maven is a project management tool. Maven has extensive capabilities for building, dependency management, testing and reporting. If you haven’t used Maven before you can think of it as an incredibly powerful version of make for now. You can download the latest version of Maven from http://maven.apache.org/. Brief installation instructions (enough to get us started) are on the download page, at http://maven.apache.org/download.html. You must have Maven 2.0.9 or greater installed. Mac OS X has Maven 2.0.6 installed. Please install 2.0.9 and make sure that the version that you installed is the first version accessed in the PATH.

Programming editor

This isn’t a strict requirement for this example, but when we start getting into coding it’s very helpful to have something a little more capable than notepad. If you’d like a full-blown IDE, with support for things like debugging, continuous compile checking, etc, there are plugins available on the Scala website at http://www.scala-lang.org/node/91. The plugins support:

Eclipse

http://www.eclipse.org/ The Scala plugin developer recommends using the Eclipse Classic version of the IDE

NetBeans

http://www.netbeans.org Requires using the NetBeans 6.5

IntelliJ

IDEA http://www.jetbrains.com/idea/index.html Requires version 8 beta

If you’d like something more lightweight, the Scala language distribution comes with plugins for editors like VIM, Emacs, jedit, etc. You can either download the full Scala distribution from http://www.scala-lang.org/ and use the files under misc/scala-tool-support, or you can directly access the latest versions via the SVN (Subversion) interface at https://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src. Getting these plugins to work in your IDE or editor of choice is beyond the scope of this book.

Now that we have the prerequisites out of the way, it’s time to get started. We’re going to leverage Maven’s archetypes to do 99% of the work for us in this example. First, change to whatever directory you’d like to work in:

cd work

Next, we use Maven’s archetype:generate command to create the skeleton of our project:

mvn archetype:generate -U \

-DarchetypeGroupId=net.liftweb \

-DarchetypeArtifactId=lift-archetype-blank \

-DarchetypeVersion=1.0 \

-DremoteRepositories=http://scala-tools.org/repo-releases \

-DgroupId=demo.helloworld \

-DartifactId=helloworld \

-Dversion=1.0-SNAPSHOT

Maven should output several page’s worth of text. It may stop and ask you to confirm the properties configuration, in which case you can just hit <enter>. At the end you should get a message that says BUILD SUCCESSFUL. You’ve now successfully created your first project! Don’t believe us? Let’s run it to confirm:

cd helloworld

mvn jetty:run

Maven should produce more output, ending with

[INFO] Starting scanner at interval of 5 seconds.

This means that you now have a web server (Jetty9 ) running on port 8080 of your machine. Just go to http://localhost:8080/ and you’ll see your first Lift page, the standard “Hello world!”. With just a few simple commands we’ve built a functional (albeit limited) web app. Let’s go into a little more detail and see exactly how these pieces fit together. First, let’s examine the index page. Whenever Lift serves up a request where the URL ends in a forward slash, Lift automatically looks for a file called index.html10 in that directory. For instance, if you tried to go to http://localhost:8080/test/, Lift would look for index.html under the test/ directory in your project. The HTML sources will be located under src/main/webapp/ in your project directory Here’s the index.html file from our Hello World project:

<lift:surround with="default" at="content">

<h2>Welcome to your project!</h2>

<p><lift:helloWorld.howdy /></p>

</lift:surround>

This may look a little strange at first. For those with some XML experience you may recognize the use of prefixed elements here. For those who don’t know what that is, a prefixed element is an XML element of the form

<prefix:element>

In our case we have two elements in use: <lift:surround> and <lift:helloWorld.howdy/>. Lift assigns special meaning to elements that use the “lift” prefix; they form the basis of lift’s extensive templating support. When Lift processes an XML template, it does so from the outermost element inward. In our case, the outermost element is <lift:surround with=”default” at=”content”>. The <lift:surround> element basically tells Lift to find the template named by the with attribute (default, in our case) and to put the contents of our element inside of that template. The at attribute tells Lift where in the template to place our content. In Lift, this “filling in the blanks” is called binding, and it’s a fundamental concept of Lift’s template system. Just about everything at the HTML/XML level can be thought of as a series of nested binds. Before we move on to the <lift:helloWorld.howdy/> element, let’s recurse and look at the default template. You can find it in the templates-hidden directory of the web app. Much like the WEB-INF and META-INF directories in a Java web application, the contents of templates-hidden cannot be accessed directly by clients; they can, however be accessed when they’re referenced by a <lift:surround> element. Here is the default.html file:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<meta name="description" content="" />

<meta name="keywords" content="" />

<title>demo.helloworld:helloworld:1.0-SNAPSHOT</title>

<script id="jquery" src="/classpath/jquery.js" type="text/javascript">

</script>

</head>

<body>

<lift:bind name="content" />

<lift:Menu.builder />

<lift:msgs/>

</body>

</html>

As you can see in the listing, this is a proper XHTML file, with <html>, <head>, and <body> tags. This is required since Lift doesn’t add these itself; Lift simply processes the XML from each template it encounters. The <head> element and its contents are boilerplate; the interesting things happen inside the <body> element. There are three elements here:

1. The <lift:bind name=”content” /> element determines where the contents of our index.html file are bound (inserted). The name attribute should match the corresponding at attribute from our <lift:surround> element.

2. The <lift:Menu.builder /> element is a special element that builds a menu based on the SiteMap. The SiteMap is a high-level site directory component that not only provides a centralized place to define a site menu, but allows you to control when certain links are displayed (based on, say, whether a user is logged in or what roles they have) and provides a page-level access control mechanism.

3. The <lift:msgs/> element allows Lift (or your code) to display messages on a page as it’s rendered. These could be status messages, error messages, etc. Lift has facilities to set one or more messages from inside your logic code.

Now let’s look back at the <lift:helloWorld.howdy/> element from the index.html file. This element (and the <lift:Menu.builder /> element, actually) is called a snippet, and it’s of the form

<lift:class.method>

Where class is the name of a Scala class defined in our project in the demo.helloworld.snippets package and method is a method defined on that class. Lift does a little translation on the class name to change camel-case back into title-case and then locates the class. In our demo the class is located under src/main/scala/demo/helloworld/snippet/HelloWorld.scala, and is shown here:

package demo.helloworld.snippet

class HelloWorld {

def howdy: NodeSeq =

<span>Welcome to helloworld at {new java.util.Date}</span>

}

Pages: ← previous Ctrl next
1 2 3 4 5 6

© Copyright © 2008, 2009 Derek Chen-Becker, Marius Danciu, David Pollak and Tyler Weir. License: Creative Commons Attribution-No Derivative Works 3.0 Unported License.