【问题标题】:how to Redirect to another Action with json data in play-framework如何在播放框架中使用 json 数据重定向到另一个动作
【发布时间】:2017-03-03 11:12:16
【问题描述】:

我有以下代码:

case class ResetPasswordJsonValidation (id : String ,email : String)

object ResetPasswordJsonValidation {

  implicit val resetPasswordRead : Reads[ResetPasswordJsonValidation]= (
    (JsPath \ "email").read(email) and
    (JsPath \ "id").read(id)
  )(ResetPasswordJsonValidation.apply _)

}

以下Action

def resetPassword = Action { request =>
    request.body.asJson.get.validate[ResetPasswordJsonValidation].fold(
        resetPassword => {
            log.info("id is {}", resetPassword.id)
            log.info("email id is {}", resetPassword.email)
        }
    )
}

和路由文件:

POST    /direct-user/reset-password    controllers.DirectUserController.resetPassword

这是curl 文件,我用它来向上面的路由发出请求:

contentType="Content-type: application/json";

data='{  "id" : "54d3732d-d728-40d3-ae63-b18ab6be8e70" ,
       "email":"bob@example.com"}';
echo "    "
echo "------------------   Sending Data   ------------------"
echo "    "
echo "Content-Type : " $contentType
echo "Data : " $data


echo "    "
echo "------------------     Response     ------------------" 
echo "    "
echo "    "

还有curl 命令:

curl --include --request POST --header "Content-type: application/json"  --data "$data" http://localhost:9000//direct-user/reset-password

我想要做的是另一个重定向到resetPassword 的操作,但保留正文。该怎么做?

【问题讨论】:

  • 你想在这里解决什么问题?结果密码只是记录参数。你只是想记录信息吗?

标签: java json scala playframework playframework-2.3


【解决方案1】:

使用闪光范围

def A = Action {
    var email:String="bob@example.com"
    var id:String="54d3732d-d728-40d3-ae63-b18ab6be8e70"
    Redirect(routes.DirectUserController.resetPassword()).flashing(
        "email" -> email,
        "id" -> id
    )  
  }

文档:https://www.playframework.com/documentation/2.5.x/ScalaSessionFlash

您需要编写特殊代码来从闪存范围获取变量,例如

request.flash.get("email")

【讨论】:

  • 还有其他方法吗?
  • 可以使用get参数,也可以使用TemporaryRedirect(307)。在临时重定向的情况下,您需要从客户端发送该数据,您将无法将它们添加到操作中。
猜你喜欢
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多