简体   繁体   English

如何在游戏框架中的动作中获得被叫动作的响应

[英]how to get the response of called Action within an Action in play framework

i have two Actions in different controllers ActionA and ActionB i am calling ActionB in ActionA and i want to get its(ActionB) response in ActionA is it possible ? 我在不同的控制器ActionA和ActionB中有两个Action,我在ActionB中调用ActionB,我想在ActionA中获得它的(ActionB)响应吗? how can i achive this please help here is my code 我怎么能做到这一点,请帮助我的代码

class ControllerA extends Controller{

def ActionA = Action { implicit request =>
    var jsonRequest = request.body.asJson.get
    val uuid = (jsonRequest \ "uuid").as[String]
    log.info("in ActionA" + uuid)
    val controllerB= new ControllerB
    val actionB=controllerB.ActionB.apply(request)
    //here i want to get the response of ActionB and return this response as the response of ActionA whether its OK or InternelServerError
    Ok("i want to show the response of ActionB")
    }
}

class ControllerB extends Controller{
def ActionB = Action { implicit request =>
    var jsonRequest = request.body.asJson.get
    val uuid = (jsonRequest \ "uuid").as[String]
    log.info("in ActionB " + uuid)
    try {
      Ok("i am ActionB with id {}"+uuid)
    } catch {
      case e: Exception =>
        log.error("Exception ", e)
        val status = Http.Status.INTERNAL_SERVER_ERROR
        InternalServerError(Json.obj("status" -> status, "msg" -> ServerResponseMessages.INTERNAL_SERVER_ERROR))
    }
  }
}

please help 请帮忙

In play 2.2 and 2.3 controllers are typically an object instead of a class so I changed your controllers to be objects. 在播放2.2和2.3中,控制器通常是object而不是class因此我将您的控制器更改为对象。 In newer versions of play controllers are classes that are injected using the Guice framework. 在较新版本的播放控制器中,是使用Guice框架注入的类。

Since invocations of actions is asynchronous, you need to change ActionA to be Action.async . 由于动作的调用是异步的,因此您需要将ActionA更改为Action.async Below are the changes that I made: 以下是我所做的更改:

object ControllerA extends Controller{

  def ActionA = Action.async { implicit request =>
    var jsonRequest = request.body.asJson.get
    val uuid = (jsonRequest \ "uuid").as[String]
    log.info("in ActionA" + uuid)
    ControllerB.ActionB(request)
  }
}

object ControllerB extends Controller{
  def ActionB = Action { implicit request =>
    var jsonRequest = request.body.asJson.get
    val uuid = (jsonRequest \ "uuid").as[String]
    log.info("in ActionB " + uuid)
    try {
      Ok("i am ActionB with id {}"+uuid)
    } catch {
      case e: Exception =>
        log.error("Exception ", e)
        val status = Http.Status.INTERNAL_SERVER_ERROR
        InternalServerError(Json.obj("status" -> status, "msg" -> ServerResponseMessages.INTERNAL_SERVER_ERROR))
    }
  }
}

As the previous answer alluded to, it's far more advantageous to have shared controller code in a service layer that sits below your controllers as opposed to sharing controller code directly. 正如前面的答案所暗示的,与直接共享控制器代码相比,在位于控制器下方的服务层中共享控制器代码更为有利。 Given your simplistic example though it seems OK to do what you're doing. 考虑到您的简单化示例,可以执行您正在做的事情。

If you deploy your controllers in a single JVM, I think you can extract a function from ActionB and share the code between two controllers. 如果您将控制器部署在单个JVM中,我认为您可以从ActionB中提取一个函数并在两个控制器之间共享代码。 If you deploy your controllers in two different JVMs, in this case you need to use a web service client library to query the endpoint. 如果将控制器部署在两个不同的JVM中,则在这种情况下,您需要使用Web服务客户端库来查询端点。 Just my two cents. 只是我的两分钱。

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

相关问题 Play Framework 2.7:如何在Java中的动作组合中更新会话 - Play Framework 2.7: How to update session within a Action composition in Java 播放框架2.2:如何在Java中获取当前的控制器和动作 - Play framework 2.2: How to get current controller and action in java 尝试在Play Framework中使用简单身份验证,但我的操作被两次调用 - Trying to use simple authentication in Play Framework, but my action is called twice 找不到Play Framework / Eclipse / Action - Play Framework/ Eclipse/ Action not found 播放2.5:在自定义http操作中获取响应正文 - Play 2.5: get response body in custom http action 播放2.6:在自定义http操作中获取响应正文 - Play 2.6: get response body in custom http action 调用动作后,在动作内的动作网址中添加查询参数 - Add Query Parameter in action url within action after action is called 如何在Play 2.3中自定义操作组合以记录请求和响应? - How to do custom action composition to log request and response in Play 2.3? 如何在播放框架中使用json数据重定向到另一个动作 - how to Redirect to another Action with json data in play-framework 如何在Play Framework 2.3中从我的表单反向路由到操作 - How to reverse route to an action from my form in Play Framework 2.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM