简体   繁体   English

如何在Play Framework 2的视图中呈现当前控制器和操作名称?

[英]How can I render the current controller and action name in the view of Play Framework 2?

I would like to render the name of the current action and controller in the view of a Play 2 Application. 我想在Play 2应用程序的视图中呈现当前操作和控制器的名称。 How do I do this? 我该怎么做呢?

In Scala, we use the following function for this purpose: 在Scala中,我们使用以下函数来实现此目的:

def actionFromRequest(request: RequestHeader): String = {
    request.tags.get(Routes.ROUTE_CONTROLLER).flatMap {
      c => request.tags.get(Routes.ROUTE_ACTION_METHOD).map {
        a => c + "." + a
     }
   }.getOrElse("ActionControllerCannotBeRetrieved")
}

Output here is "ControllerName.ActionName". 这里的输出是“ControllerName.ActionName”。 While you have implicit request in your action, you just simply can include this function. 虽然您在操作中有隐式请求,但您只需包含此功能即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM