Create web application menus using the Lift web framework


Lift is a powerful & secure web framework having elegant features and also stresses the importance of security, maintainability, scalability and performance. Lift applications are written in Scala.

Here we will discuss about creating the menus in Lift. These are the approaches to create a menu:

  • Define the url names and routes directly in Boot.scala.
  • Create the seperate Singleton for defining menu items.

Let us see how this works:

Define the url names and routes in Boot.scala

class Boot {
  def boot {
    // where to search snippet
    LiftRules.addToPackages("demo.helloworld")

    // Build SiteMap
    val entries =
      Menu(Loc("Home", List("index"), "Home"))::
      Menu(Loc("Items", List("items"), "Items")) ::
      Menu(Loc("Search", List("search"), "Search")) :: Nil

    // Sitemap
    LiftRules.setSiteMap(SiteMap(entries:_*))

  }
}

And here is the output:



Create the seperate Singleton for defining menu items

We will define the sitemap in Boot.scala. Sitemap will obtained from the Application Singleton

class Boot {
  def boot {
    LiftRules.setSiteMap(SiteMap(Application.sitemap: _*))
  }
}

Now define your menu items in this seperate singleton say “Application” singleton.

object Application {
  // Sitemap
  val sitemap = List(
    Menu.i("Items") / "items",
    Menu.i("Search") / "search"
  ) ::: User.menus
}

When working with html pages always provide the links to pages corresponds to as defined in your singleton’s sitemap e.g

  <li><a href="/items">Items</a></li>
  <li><a href="/search">Search</a></li>

Lift will search for corresponding HTML pages defined either in Boot.scala or in Application.scala in src/main/webapp folder.

About Neelkanth Sachdeva

Project Manager @ eCIFM Solutions Inc. Previous : Senior Software Engineer @ Evernym Inc. Previous : Software Consultant @ Knoldus Software LLP Software Consultant @ Inphina Technologies
This entry was posted in Scala, Web and tagged . Bookmark the permalink.

One Response to Create web application menus using the Lift web framework

  1. narender says:

    great bro…

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