Hello1

From Lift

Jump to: navigation, search

Contents

Hello1 Example

This example demonstrates a basic "Hello World" web page with some dynamic content. This is not the easy lift way to do things (way too much XML configuration), but is provided to show you how lift works with existing Servlet environments. The page will look like this:

Image:screenshot.gif

Minimal Files

The simplest possible Lift application contains 3 files:

  1. web.xml
  2. Boot.scala
  3. index.html

Since we want to show more than static content, we'll need a fourth file called "Hello1.scala" to display dynamic content. Let's review each file one by one.

web.xml

web.xml is a standard J2EE application descriptor for defining a web application. If you are not familiar with web.xml, you can read Sun's tutorial on web applications. This file must be located under WEB-INF/web.xml inside your web application archive (.war).

The Lift filter is defined as follows,

[hello1/src/main/webapp/WEB-INF/web.xml]

<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>

Boot.scala

Every Lift application requires a bootstrap class that initializes the context of the application. This class is named bootstrap.liftweb.Boot. For our simple example, the only thing we need to do is register the package name in which we'll be placing Lift snippets:

[hello1/src/main/scala/bootstrap/Boot.scala]

package bootstrap.liftweb

import net.liftweb.http._

class Boot {
  def boot = {
    LiftServlet.addToPackages("net.liftweb.tutorial.hello1")
  }
}

index.html

The index.html file is the default page presented when accessing a web application. This is a regular HTML file except for the use of special <lift:snippet> tags to embed dynamic content provided by Scala classes.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Lift Tutorial</title>
</head>
<body>
    <h2>Lift Tutorial: Hello1</h2>

    <p><lift:snippet type="Hello1:howdy" /></p>
    <p>The time is <lift:snippet type="Hello1:time" /></p>
</body>
</html>

The first snippet,

    <p><lift:snippet type="Hello1:howdy" /></p>

calls the "howdy" method of the "Hello1" class in the package "net.liftweb.tutorial.hello1.snippet". Notice that "snippet" (XML fragment) has been added to the package name we registered in Boot.scala. Lift automatically searches all registered packages for snippets and displays the first snippet matching the "type" name.

The second snippet will displays the current time,

    <p>The time is <lift:snippet type="Hello1:time" /></p>

by calling Hello1.time.

Hello1.scala

Finally here is our Scala code matching the snippet above,

[hello1/src/main/scala/net/liftweb/tutorial/hello1/snippet/Hello1.scala]

package net.liftweb.tutorial.hello1.snippet

class Hello1 {
  def howdy = <span>Hello from Lift</span>

  def time = <span>{new java.util.Date}</span>
}

Each method returns an XML fragment that is embedded into the page where the corresponding <lift:snippet> tag is located.

That's all there is to our Hello1 application! You can follow with our next tutorial example called Hello2.

Personal tools