Getting started with Lift Web Framework by creating your First Project


In this blog, I will explain that how can we create a lift web project. There are 2 ways to do this. Here, I will explain both ways step by step .

1. Download and Run Lift :
To install and run Lift, the only prerequisite is to have Java 1.5 or later installed.
Instructions for installing Java can be found here.
Once you have Java, the following instructions will download, build, and start a basic
Lift application.

For Mac and Linux :

    • Visit http://liftweb.net/download and download the most recent Lift 2.6 ZIP file.
    • Unzip the file.
    • Start Terminal or your favourite shell tool.
    • Navigate into the unzipped folder and into the scala_210 subfolder and then into the
    lift_basic folder.
    • Run ./sbt
    • Required libraries will be downloaded automatically.
    • At the SBT prompt (>), type container:start
    • Open your browser and go to http://127.0.0.1:8080/
    • When you’re done, type exit at the SBT prompt to stop your application from
    running.

For Windows :

    • Visit http://liftweb.net/download and locate the link to the most recent ZIP version
    of Lift 2.6 and save this to disk.
    • Extract the contents of the ZIP file.
    • Navigate in Explorer to the extracted folder, and once inside, navigate into sca‐
    la_210 and then lift_basic.
    • Double-click sbt.bat to run the build tool; a Terminal window should open.
    • Required libraries will be downloaded automatically.
    • At the SBT prompt (>), type container:start .
    • You may find Windows Firewall blocking Java from running. If so, opt to “allow
    access.”
    • Start your browser and go to http://127.0.0.1:8080/
    • When you’re done, type exit at the SBT prompt to stop your application from
    running.

2. Create project using SBT :
If you want to create the lift web project without using the ZIP files, then follow the below steps.
First of all, you will need to configure SBT and the Lift project yourself. We need only five small files for creating the project.

a. First, create an SBT plugin file at project/plugins.sbt (all file names are given relative to the project root directory):

addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.9.0")

This file tells SBT that you will be using the xsbt-web-plugin and chooses the correct
version based upon your version of SBT.

b. Next, create an SBT build file, build.sbt:

organization := "org.knoldus.com"

name := "Lift_Using_SBT"

version := "0.1-SNAPSHOT"

scalaVersion := "2.10.0"

seq(com.github.siasia.WebPlugin.webSettings :_*)

libraryDependencies ++= {
val liftVersion = "2.6-M2"
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.7.v20120910" %"container,test",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" %
"container,compile" artifacts Artifact("javax.servlet", "jar", "jar")
)
}

Feel free to change the various versions, though be aware that certain versions of Lift
are only built for certain versions of Scala.
Now that you have the basics of an SBT project, you can launch the sbt console. It should
load all the necessary dependencies, including the proper Scala version, and bring you
to a prompt.

c. Next, create the following file at src/main/webapp/WEB-INF/web.xml:

<!DOCTYPE web-app SYSTEM "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>LiftFilter</filter-name>
<display-name>Lift Filter</display-name>
<description>The Filter that intercepts Lift calls</description>
<filter-class>net.liftweb.http.LiftFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LiftFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

The web.xml file tells web containers, such as Jetty as configured by xsbt-web-plugin ,to pass all requests on to Lift.

d. Next, create a sample index.html file at src/main/webapp/index.html for our Lift app to
load. For example:

<!DOCTYPE html>
<html>
<head>
<title>Lift using SBT</title>
</head>
<body>
<h1>Welcome, you now have a working Lift installation</h1>
</body>
</html>

e. Finally, set up the basic Lift boot settings by creating a Boot.scala file at src/main/scala/bootstrap/liftweb/Boot.scala. The following contents will be sufficient:

package bootstrap.liftweb
import net.liftweb.http.{Html5Properties, LiftRules, Req}
import net.liftweb.sitemap.{Menu, SiteMap}
/**
* A class that's instantiated early and run. It allows the application
* to modify lift's environment
*/
class Boot {
def boot {
// where to search snippet
LiftRules.addToPackages("org.knoldus.com.Lift_Using_SBT")
// Build SiteMap
def sitemap(): SiteMap = SiteMap(
Menu.i("Home") / "index"
)
// Use HTML5 for rendering
LiftRules.htmlProperties.default.set((r: Req) =>
new Html5Properties(r.userAgent))
}
}

Cheers, now you have a working Lift project!

Tu run the application, follow below command :

sbt ~container:start

To browse the application, open http://localhost:8080

To get the sample code, just clone the project from here

Advertisements

About Rishi Khandelwal

Sr. Software Consultant having more than 6 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Spark, Hive, Cassandra, Akka-http, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Scala. Bookmark the permalink.

One Response to Getting started with Lift Web Framework by creating your First Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s