首页 文章

Action不会重定向到play-framework中的另一个Action

提问于
浏览
0
class InstitutionController extends Controller {
    def updateInstitution = Action { implicit request =>
        {
          request.body.asJson.get.validate[GallreyJsonValidationForUpdate].fold(
            valid = {
              updateInstitution =>
                {
                        Redirect(routes.GalleryController.updateGallreyObject()).flashing("uuid"-    >updateInstitution.uuid,"institutionName"->updateInstitution.institutionName,"details"->updateInstitution.details)
                }
            },
            invalid = {
              errors =>
                {
                  val json=commonUtils.getResponse(Http.Status.BAD_REQUEST, ServerResponseMessages.VALIDATION_FAILED,JsError.toJson(errors))
                  log.error("sending error in json{}", json)
                  BadRequest(json)
                }
            })
        }
      }

这是我重定向到的动作

class GalleryController extends Controller {
def updateGallreyObject = Action { implicit request =>
  {
       val uuid=request.flash.get("uuid")
       val institutionName=request.flash.get("institutionName")
       val details=request.flash.get("details")
       Ok("some details")
 }}
}

这是我正在使用的卷曲文件

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

data='{  "uuid" : "123" , "institutionName" : "abc" , "details" : "some details" 

 }';
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/institution/update

我得到的回应是

HTTP/1.1 303 See Other
Location: /gallery/update
Set-Cookie: PLAY_FLASH=uuid=123 &institutionName=abc&details=some+details; Path=/; HTTPOnly
Date: Sat, 04 Mar 2017 14:40:29 GMT
Content-Length: 0

这是路线

POST   /institution/update                                  controllers.InstitutionController.updateInstitution

为什么它没有重定向到 updateGallreyObject 动作?我做错了什么请帮助,我希望它重定向到 updateGallreyObject 行动与数据请帮助,我期待这个回应"some details"

更新我已经有这条路线

POST   /gallery/update                                  controllers.GalleryController.updateGallreyObject

2 回答

  • 0

    因为您需要在路线中使用 updateGallreyObject 的映射 .

    GET /galery                    controllers.GalleryController.updateGallreyObject
    

    附:在将代码发布到此处之前重新格式化代码 . 您可能想要使用外部格式化程序,如Scalariform或Scalafmt .

  • 0

    尝试使用 matchfold ,就像在文档中一样:https://www.playframework.com/documentation/2.3.x/ScalaJsonCombinators#Putting-it-all-together

    request.body.asJson.get.validate[GallreyJsonValidationForUpdate] match {
      case s: JsSuccess[GallreyJsonValidationForUpdate] => {
        val updateInstitution: GallreyJsonValidationForUpdate = s.get
    
        Redirect(routes.GalleryController.updateGallreyObject()).flashing("uuid"-    >updateInstitution.uuid,"institutionName"->updateInstitution.institutionName,"details"->updateInstitution.details)
      }
      case e: JsError => {
        val json=commonUtils.getResponse(Http.Status.BAD_REQUEST, ServerResponseMessages.VALIDATION_FAILED,JsError.toJson(errors))
        log.error("sending error in json{}", json)
        BadRequest(json)
      }
    }
    

相关问题