Lift Framework for Java Developers

05 November 2013
By Anton Krasikov, Senior Software Architect

Scala language is getting more and more attention nowadays from the Java community and a number of people even consider it to be a kind of improved Java. As many Java developers work with web technologies and have to deal with the diverse world of Java web frameworks, it would be interesting to have a glance at what Scala brings to the table.

One of the most popular Scala based frameworks is Lift, which was created by David Pollack in 2007. It is written mostly using Scala programming language and leverages its several features. Lift was inspired by a set of other frameworks, such as Ruby on Rails, Seaside, Wicket and Django, and tries to implement their best features, such as security, fast prototyping and designer-friendly templates. It is now used in some major web applications, and the most widely known of them is Foursquare.

First of all, Lift framework tries to move away from the widespread MVC pattern and uses a View first model. It was inspired by Wicket framework and means that UI templates are designer friendly, do not have any executable code and comply with XHTML. This could be really useful for web masters to work on the templates without having to coordinate every line of code with developers. There is another big advantage of View First approach – it makes impossible to write database access code inside the views and thus keeping application structure clean and concise.

Lift has powerful templates mechanism which provides content binding and modular structure. XML templates in the Lift application may look similar to the following code:

<lift:surround with="default" at="content">
    <h2>Welcome to your project!</h2>
    <lift:timeSnippet.currentTime>
        <span>Lift application says time is: <b:currentTime/></span>
    </lift:timeSnippet.currentTime>
</lift:surround>

Every UI template has corresponding snippet code, which could be reused throughout the application:

class TimeSnippet {
  def currentTime(in: NodeSeq): NodeSeq =
    Helpers.bind("b", in, "currentTime" -> (new _root_.java.util.Date).
toString)
}

Modern frameworks pay more attention to web application security and Lift keeps up with the trend. The framework has improved security mechanisms and safeguards the application from the most common OWASP vulnerabilities. To name a few things, it has a built-in protection against CSRF and XSS vulnerabilities. The developer doesn’t need to do anything for the application to be secure as all of these features are available thanks to the framework architecture and internal mechanisms.

Lift framework supports both stateful and stateless applications. It’sa solid platform for fully stateless, highly scalable RESTful applications. Lift applications could be stateful as well, for example if a rich interactivewebsite for browser is being developed.

One of the outstanding features of Lift is great support for AJAX and Comet technologies from the early days of the framework. Issuing AJAX calls is really simple and straightforward, requires only a few lines of code,so developer can focus on implementing business logic instead of integrating JavaScript with server side.

Here is a sample server code for AJAX button:

class AjaxSample {
   def button = { 
      def process() = {
         println("action happened on server");
         Alert("client message");
      }
      "button [onclick]" #> SHtml.ajaxInvoke(process)
   }
}

The corresponding UI template looks simple:

<button lift="AjaxSample.button">Do something on server</button>

Probably the biggest challenge in mastering framework Lift is deep understanding of Scala language necessary to use it efficiently. This may stop some developers from leveraging the framework; however, such investment makes a lot of sense. Understanding Scala can help with approaching the typical problems with different mindset and finding elegant solutions. As Lift relies heavily on Scala operators it could be a bit difficult to read constructs like “#>” or “:+=”. These operators takes a little more time to learn providing you have Java-only background, but in the end it becomes clear and understandable.

Lift framework follows convention over configuration style. It has reasonable defaults for a typical web application and provides means of setting user-defined values. The configuration is simply a piece of Scala code without a burden of XML files, except the web.xml file which should have reference to LiftFilter. This brings another useful feature of Lift – it could be run in any Java application container and reuse many existing Java libraries such as Hibernate ORM.

Overall, Lift represents a modern framework which could be used for a large variety of web applications and provides efficient methods of implementing business logic. The framework could be considered mature and ready for production development, although it is still under development. The latest stable version is 2.5 and it’s easy to download and begin playing around with thanks to detailed guides and tutorials available on official website. For those who are interested in a more detailed description of the framework there is a book named Simply Lift available online for free.

Tags: ,


Add Comment

Name Mail Website Comment