INightmare's Blog

Lift Framework

Today I want to write a little about Lift Framework. It is a web application framework targeting Java web application containers written in Scala programming language. Scala is gaining popularity and according to various language popularity ratings (TIOBE and Transparent Language Popularity Index) it is rising in popularity and at the time of writing popularity index is over 0.23% in both metrics. It is a good result considering how much attention programming languages are getting in general. So considering this it seems natural that someone would write a web framework based on Scala. Especially taking into account that Scala is often called “static dynamic language” and even compared to the likes of Ruby. The later got much attention when the famous Ruby on Rails framework was released. Can it be the case that Lift will mean the same for Scala? Might be. Twitter has moved from Ruby to Scala, Foursquare have ported their application to Lift Framework and new Novell Vibe solution was all written using Lift.

What I like about Lift?

View-centric approach. The concept is somewhat similar to JavaServer Faces, where you have a view and reference components and beans from that view. That allows you to compose complex views and keep their logic separate (in case of MVC your controller will be a hub to all the services and data you are passing to the view, so to add any new element to the view you need to update the controller to fetch the data and update the view to make use of it). But in Lift things are a bit different. The view is a simple HTML template, that can access snippets (snippet is a peace of code that can generate content, like a component).

For example lets define a view:

<div class="left:My.text"></div>

Now we need a snippet:

class My {
  def text = "*" #> (<b>Some text</b>)
}

He we tell “replace all content with Some text“. We define a CSS selector “*“ and then fill the matching element with the markup. Since Scala natively supports XML, we can pass the XML content with no special markup. So, you may ask “If the markup is complex I am suppose to write it all in my snippet?”. And my answer is “not at all”. By using CSS selectors you can enter values in specific places. For example we have a view:

<div class="lift:My.complex"><span id="text"></span><b id="boldy"></b></div>

Lets fill span and b with some text.

def complex = "#text *" #> "Normal text" & "#boldy *" #> "Bold text"

And of course you can iterate and repeat the content, build tables and so on. This approach is especially convenient for ajax requests.

Ajax and Comet support. Lift allows you to create ajax actions and update content on the page with 0 JavaScript. It does this with the help of helper methods that generate markup and JavaScript for invoking AJAX calls and of updating the page. You can also pass your own JavaScript that gets executed after an ajax request. And by supporting Comet, Lift allows server to push the changes to the client when required.

Scoped variables. That is a functionality that is similar to using Spring AOP scoped proxy. In Spring you define a bean, set scope to session or request and inject to your controller. In Lift you define a property of type RequestVar or SessionVar to hold the value. For example sessionVariable is session scoped:

class My {
  object sessionVariable extends SessionVar[Box[String]](Empty)
}

Box here is a value holder that can be empty or contain a value, in our case - String. Lift makes use of Java Servlet technology and session scoped variables live in application session. And ofcourse you can have fully stateless application as Lift has no additional state.

Lift also comes with two persistence solutions a Mapper ORM and a broader solution (Record) that also supports document stores, like CouchDB and MongoDB.

You can see some more examples at Lift demo page.

Conclusion

If you’re looking for the next web application framework, Lift is definitely worth looking into. It is fast, backed by functional programming language (which gives nice things like mixins (Scala calls them traits), function objects), actively developed. And fairly documented - has two free books, wiki and some blogs about it. I say fairly, because in this respect there is certainly room for improvement.