【问题标题】:How to handle "Action not found" with Dependency Injection Play framework 2.4如何使用 Dependency Injection Play framework 2.4 处理“Action not found”
【发布时间】:2015-10-03 18:40:09
【问题描述】:

我在注入依赖概念上覆盖HttpErrorHandler 类并尝试处理路由页面。我不需要在 URL 的路由页面不正确时显示 Action not found 页面,但它必须通过在 HttpPageErrorHandler 的 view.html.error.notFoundPage() 上指定页面路由来呈现。如下图,当尝试访问 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 页面。

【问题讨论】:

    标签: error-handling playframework dependency-injection


    【解决方案1】:

    怎么样

    play.http.errorHandler = "global.HttpPageErrorHandler"
    

    或者任何你的包裹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      • 2023-03-31
      • 2015-11-10
      • 2015-10-02
      • 1970-01-01
      • 2021-08-02
      • 2022-01-22
      相关资源
      最近更新 更多