19
Introducing Lift Web Framework Rishi Khandelwal Software Consultant Knoldus Software LLP Email : [email protected]

Intro lift

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Intro lift

Introducing Lift Web Framework

Rishi Khandelwal Software Consultant Knoldus Software LLP Email : [email protected]

Page 2: Intro lift

Introduction Lift is a free web application framework.

It was originally created by David Pollack

It provides web application developers tools to make writing security, interacting, scalable web applications easier

Lift uses Scala as programming language.

It does not use MVC pattern.

It uses View first approach.

Page 3: Intro lift

Design goals

SECURITY : Whenever you make an AJAX call, use Comet, or even build a simple form, Lift replaces input names and URLs with opaque GUIDs that reference specific functions on the server.

CONCISENESS : As Lift uses Scala as a programming laguage, and scala's code is very concise. So Lift has this property

PERFORMANCE : It is very quick. Using the basic Lift project, you can expect upward of 300 requests per second on a machine with only 1 GB of RAM and a middle-of-the-road processor.

Page 4: Intro lift

View-first design Lift’s view-first approach does the complete opposite of MVC pattern

It first chooses the view and then determines what dynamic content needs to be included on that page.

The three component parts are view, snippet, and model—VSM for short

Execute command Apply / commit

Change Notification Error notices

.

Snippet

View Model

Page 5: Intro lift

Continued...

View : It refers primarily to the HTML content served for a page request. Within any given Lift application, you can have two types of view: 1. Template views that bind dynamic content into a predefined

markup template 2. Generated views in which dynamic content is created, typically with Scala XML literals

Snippet : Snippets are rendering functions that take XML input from within a given page template and then transform that input based upon the logic within the snippet function.

Model : For most applications, it will represent a model of persistence or Data. You ask the model for value x, and it returns it.

Page 6: Intro lift

Lift Sub-projects

Lift is broken down into three top-level subprojects: Lift Core and Lift Web, Lift Persistence, and Lift Modules.

1.. Lift Core and Lift Web :

The Core consists of four projects that build to separate libraries that you can use both with and without Lift’s Web module.

The Web module itself builds upon the Core.

The Web module itself is made up of three projects: the base web systems and two additional projects that provide specialized helpers.

Page 7: Intro lift

Continued... Lift Web

Wizard TestKit

Webkit

Utilities

Actor JSON

Common

Lift Core

Page 8: Intro lift

Continued...LIFT COMMON :

It contains a few base classes that are common to everything else within Lift.

Probably most important of all, Lift Common can be used in projectsthat aren’t even web applications.

LIFT ACTOR :

Actors are a model for concurrent programming whereby asynchronous messaging is used in place of directly working with threads and locks.

Lift has its own for the specific domain of web development.

Lift Actor provides concrete implementations of the base actor traits that are found within Lift Common

Page 9: Intro lift

Continued...LIFT UTILITIES :

Lift Utilities is a collection of classes, traits, and objects that are designed to save you time or provide convenience mechanisms for dealing with common paradigms.

LIFT JSON :

Lift JSON provides an almost standalone package for handling JSON in highly performant way.

The parser included within Lift JSON is approximately 350 times faster than the JSON parser that’s included in the Scala standard library

LIFT WEBKIT : The WebKit module is where Lift holds its entire pipeline, from request processing right down to localization and template rendering.

Page 10: Intro lift

2. Lift Persistence : It is some kind of backend storage.

It provides you with a number of options for saving your data,whether it’s a relational database management system (RDBMS) or one of the new

NoSQL solutions.

There are three foundations for persistence :

LIFT DB AND MAPPER :

It provides communication with an RDBMS of some description,

Mapper provides you with an object-relational mapping (ORM) implementation that handles all the usual relationship tasks

Page 11: Intro lift

Continued...

eg. User.find(By(User.email, "[email protected]")) User.find(By(User.birthday, new Date("Jan 4, 1975")))

LIFT JPA :

It is Java Persistence API.

This module was added to Lift’s persistence options to wrap the JPA API and give it a more idiomatic Scala feel

LIFT RECORD :

Record was designed with the idea that persistence has common idioms no matter what the actual backend implementation was doing to interact with the data.

Page 12: Intro lift

Continued...Record is a layer that gives users create, read, update, and delete (CRUD)

semantics and a set of helpers for displaying form fields, operating validation, and so forth.

Record has 3 backend implementation modules as part of the framework: 1. NoSQL document-orientated storage system CouchDB 2. second for the NoSQL data store MongoDB 3.layer on top of Squeryl ,the highly sophisticated functional persistence library.

3. Lift Modules :

Lift Modules is where the project houses all the extensions to the core framework.

Unlike the other groups of subprojects within Lift, the modules are more organic and have little or no relation to one another.

Each module is generally self-contained regarding the functionality it provides.

Page 13: Intro lift

Project structureprojectsrc main scala bootstrap liftweb Boot.scala code comet lib model snippet view resources webapp WEB-INF/web.xml css images templates-hidden index.html test

Page 14: Intro lift

Boot.scalaclass Boot { def boot { // where to search snippet LiftRules.addToPackages("code")

// Build SiteMap val entries = List( Menu.i("Home") / "index", // the simple way to declare a menu

// more complex because this menu allows anything in the // /static path to be visible Menu(Loc("Static", Link(List("static"), true, "/static/index"),

"Static Content")))

// set the sitemap. Note if you don't want access control for // each page, just comment this line out. LiftRules.setSiteMap(SiteMap(entries:_*))

//Show the spinny image when an Ajax call starts LiftRules.ajaxStart = Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)

Page 15: Intro lift

Continued...

// Make the spinny image go away when it ends LiftRules.ajaxEnd = Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

// Force the request to be UTF-8 LiftRules.early.append(_.setCharacterEncoding("UTF-8"))

// Use HTML5 for rendering LiftRules.htmlProperties.default.set((r: Req) => new Html5Properties(r.userAgent))

//Init the jQuery module, see http://liftweb.net/jquery for more information. LiftRules.jsArtifacts = JQueryArtifacts JQueryModule.InitParam.JQuery=JQueryModule.JQuery172 JQueryModule.init() }}

Page 16: Intro lift

Index.html

<div id="main" class="lift:surround?with=default;at=content"><form class="lift:Process?form=post">

Name: <input id="name"><br> Age: <input id="age"value="0"><br> <input type="submit" value="Submit"id="submit">

</form></div>

default.html<span class="lift:Menu.builder"></span>

<div class="lift:Msgs?showAll=true"></div> <div class="column span-17 last"> <div id="content">The main content will get bound here</div> </div>

Page 17: Intro lift

Process.scalaobject Process {

var name = "" var age = "0" def render = {

// define some variables to put our values into

"#name" #> SHtml.text(name, name = _) & // set the name

// set the age variable if we can convert to an Int "#age" #> SHtml.text(age, age = _) &

// when the form is submitted, process the variable "#submit" #> SHtml.onSubmitUnit(process) }

Page 18: Intro lift

Continued...

// process the form private def process() = asInt(age) match { case Full(a) if a < 13 => S.error("Too young!") case Full(a) => { S.notice("Name: " + name) S.notice("Age: " + a) } case _ => S.error("Age doesn't parse as a number") }

}

Page 19: Intro lift