Setting up a custom 404 page

From LiftWiki

Jump to: navigation, search

Nothing screams pro-webdev louder than a custom 404 page. Luckily lift makes it incredibility easy to set one up.

Step 1

Create a new file called 404.html* and add it to the <your-proj-name>\src\main\webapp directory.

Pro Tip

The file name 404.html is completely arbitrary and your custom page can be placed in any <your-proj-name>\src\main\webapp sub-directory. But the rest of this walk-through assumes that your custom 404 page is named 404.html and lives in <your-proj-name>\src\main\webapp.

Step 2

Add the 404 page to your site map

// Build SiteMap
val pages = Menu(Loc("Home", List("index"), "Home")) ::
            Menu(Loc("404", List("404"), "404", Hidden)) ::
            Nil

LiftRules.setSiteMap(SiteMap(pages:_*))

Step 3

Add a new rule to LiftRules:

// Redirect
LiftRules.uriNotFound.prepend{
  case (req, _) => PermRedirectResponse("404", req)
}

Step 4

Test it out. Type mvn jetty:mvn, surf to localhost:8080/does-not-exist, and make sure your custom 404 page shows up.

Trouble shooting

Ultimately your Boot.scala should look something like this:

// where to search snippet
LiftRules.addToPackages("net.lift.myWebsite")

// Redirect
LiftRules.uriNotFound.prepend{
  case (req, _) => PermRedirectResponse("404", req)
}

// Build SiteMap
val pages = Menu(Loc("Home", List("index"), "Home")) ::
            Menu(Loc("404", List("404"), "404", Hidden)) ::
            Nil

LiftRules.setSiteMap(SiteMap(pages:_*))

Infinite Redirect: Make sure that you've added your 404 page to the site map. Also make sure your 404 html file is in <your-proj-name>\src\main\webapp. Otherwise you may end up with an infinite loop.

Personal tools
search|search