了解“发布/重定向/获取"消息.图案 [英] Understanding the "post/redirect/get" pattern

查看:95
本文介绍了了解“发布/重定向/获取"消息.图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解发布/重定向/获取"的确切过程.

I am having a very hard time understanding the exact process of "post/redirect/get".

我已经在这个网站和网络上梳理了几个小时,除了这里的概念"之外,找不到任何东西.

I have combed through this site and the web for several hours and cannot find anything other than "here's the concept".

如何理解发布/重定向/获取模式?

How to understand the post/redirect/get pattern?

推荐答案

您可能会从

As you may know from your research, POST-redirect-GET looks like this:

  • 客户获得带有表单的页面.
  • 服务器的POST形式.
  • 服务器执行该操作,然后重定向到另一个页面.
  • 客户端遵循重定向.
  • The client gets a page with a form.
  • The form POSTs to the server.
  • The server performs the action, and then redirects to another page.
  • The client follows the redirect.

例如,假设我们具有以下网站结构:

For example, say we have this structure of the website:

  • /posts(显示帖子列表和添加帖子"的链接)
    • /<id>(查看特定帖子)
    • /create(如果通过GET方法请求,则将表单发布返回到其自身;如果是POST请求,则创建该帖子并重定向到/<id>端点)
    • /posts (shows a list of posts and a link to "add post")
      • /<id> (view a particular post)
      • /create (if requested with the GET method, returns a form posting to itself; if it's a POST request, creates the post and redirects to the /<id> endpoint)

      /posts本身与该特定模式无关,所以我将其省略.

      /posts itself isn't really relevant to this particular pattern, so I'll leave it out.

      /posts/<id>可以这样实现:

      • 在数据库中找到具有该ID的帖子.
      • 呈现包含该帖子内容的模板.

      /posts/create可以这样实现:

      • 如果请求是GET请求:
        • 显示一个空表单,目标设置为自身,方法设置为POST.
        • If the request is a GET request:
          • Show an empty form with the target set to itself and the method set to POST.
          • 验证字段.
          • 如果存在无效字段,请再次显示该表单并指出错误.
          • 否则,如果所有字段均有效:
            • 将帖子添加到数据库中.
            • 重定向到/posts/<id>(其中<id>从调用返回到数据库)
            • Validate the fields.
            • If there are invalid fields, show the form again with errors indicated.
            • Otherwise, if all fields are valid:
              • Add the post to the database.
              • Redirect to /posts/<id> (where <id> is returned from the call to the database)

              这篇关于了解“发布/重定向/获取"消息.图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆