Kory
Kory

Reputation: 477

How can one set up a Play Framework 404 (OnHandlerNotFound) page in Play 2.2?

So I've checked around and I've looked at various sources but I've failed to find a way to render a specific page when a handler isn't found.

I've gotten to this point in Global.scala:

import play.api._
import play.api.mvc._
import play.api.mvc.Results._
import scala.concurrent.Future
import views.html._

object Global extends GlobalSettings {

  override def onHandlerNotFound(request: RequestHeader) = {
    Future.successful(NotFound(
      ??
    ))
  }

}

I'm just unsure what to add at the "??" in that code to render a specific html document when a Handler isn't found. I've tried a few ways but an error always pops up.

Thanks.

Upvotes: 3

Views: 1076

Answers (2)

lachdrache
lachdrache

Reputation: 151

If you just want to handle prod mode differently:

  override def onHandlerNotFound(request: RequestHeader): Future[Result] =
    Play.maybeApplication match {
      case Some(app) if app.mode == Mode.Prod => Future.successful(Ok(views.html.notFound()))
      case _ => super.onHandlerNotFound(request)
    }

Upvotes: 0

James Roper
James Roper

Reputation: 12850

Create a views/notFound.scala.html template to render the HTML page. Then render it like you would a template in any other action.

override def onHandlerNotFound(request: RequestHeader) = {
  Future.successful(NotFound(
    views.html.notFound()
  ))
}

Upvotes: 4

Related Questions