如何处理"未找到行动"使用依赖注入Play框架2.4

时间:2015-10-03 18:40:09

标签: error-handling playframework dependency-injection

我在Injection Dependency概念上覆盖了类HttpErrorHandler,并尝试处理路由页面。在路由网页不正确时,我不需要显示Action not found页面,但必须通过view.html.error.notFoundPage() HttpPageErrorHandler上的指定网页路由来显示。尝试访问URL时的下图如下所示,参数

http://localhost:9000/adminlanding

指定为

的路由页面

GET /adminlanding/:userId/:ip/:dateFound/ controllers.DashboardAdmin.landing(userId: String, ip : String, dateFound : String)

HttpPageErrorHandler处理Action not found

class HttpPageErrorHandler @Inject() (router: Router) extends HttpErrorHandler {
   private def errorHandler = Play.maybeApplication.fold[HttpErrorHandler](DefaultHttpErrorHandler)(_.errorHandler)

   def onClientError(request: RequestHeader, statusCode: Int, message: String) : Future[Result]= { 
       statusCode match {
         case Status.NOT_FOUND => 
                Future.successful(NotFound(views.html.errors.notFoundPage(request)))
         case clientError if statusCode >= 400 && statusCode < 500 =>
                Future.successful(Forbidden(views.html.errors.notFoundPage(request)))
       }
   } 

   def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
     errorHandler.onServerError(request, exception)
   }

} 

Application.conf声明配置以调用注入类

play.http.HttpErrorHandler = RequestHandler

运行命令行后sbt run个静止图片显示Action not found页。

1 个答案:

答案 0 :(得分:1)

怎么样

play.http.errorHandler =“global.HttpPageErrorHandler”

或者你的包裹。