【问题标题】:how to redirect to a Action which has a post method in play framework如何重定向到在播放框架中有 post 方法的动作
【发布时间】:2016-07-21 07:54:18
【问题描述】:
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 _)
}

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 文件

#!/bin/bash

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 --include --request POST --header "Content-type: application/json"  --data "$data" http://localhost:9000//direct-user/reset-password

现在我想用代码执行此操作意味着我有一个操作 A 我需要重定向到 resetPasswordAction 我不知道该怎么做 请帮帮我

def A = Action {
    var email:String="bob@example.com"
    var id:String="54d3732d-d728-40d3-ae63-b18ab6be8e70"
    Redirect(routes.DirectUserController.resetPassword())//how can i send email and id parameter to resetPassword as its getting from curl file  
  }

请帮忙

【问题讨论】:

  • 您能否说明您为什么要这样做而不是让用户直接发布到该网址?
  • 因为在操作 A 中,我在 5 次尝试不成功后阻止了用户,然后他/她将重定向到重置密码操作为了简单起见,我省略了代码
  • A 动作本身是 POST 吗?
  • 您可能想尝试使用 307 代码。这是一篇有趣的文章programmers.stackexchange.com/a/99966

标签: java scala playframework playframework-2.3 scala-2.11


【解决方案1】:

您可以直接调用该方法而不是通过路由对象而不是重定向,

返回directController.resetPassword()

因为上下文对象(请求、响应、会话是该线程的本地对象,因此您可以从请求对象中获取这些参数)

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多