简体   繁体   中英

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. How do I do this?

In Scala, we use the following function for this purpose:

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". While you have implicit request in your action, you just simply can include this function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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