将数据传递到表单时的Post-Redirect-Get? [英] Post-Redirect-Get when passing data to the form?

查看:191
本文介绍了将数据传递到表单时的Post-Redirect-Get?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几种情况下,servlet需要从数据库中检索到的记录中将数据传递到JSP中的表单.目前,我正在使用RequestDispatcher将此信息存储在请求中,以转发至页面,一切正常.

I have several scenarios where a servlet needs to pass data to a form in a JSP from the retrieved records from the database. Currently, I'm storing this information in the request, using a RequestDispatcher to forward to the page, and all is well.

但是,这不适合PRG模式(AFAIK),并且当然意味着刷新生成的JSP意味着重新运行servlet,这是不希望的.

However, this is not fitting with the PRG pattern (AFAIK), and of course means that refreshing of the resulting JSP means re-running of the servlet, which is undesirable.

我当然可以将这些值存储在会话中,但这意味着要在以后将其清除,即使使用会话,对于从数据库中显示记录似乎也有点可笑.

I could of course store these values in the session, but that would mean clearing them afterwards, and even using the session seems like a bit of a hack for displaying a record from the database.

我只是想知道在这种情况下最好的做法是什么?我应该继续使用请求,使用会话还是其他方法?

I am simply wondering what would be the best practice in this situation? Should I continue using the request, use the session, or some other technique?

谢谢.

修改: 在阅读了几篇文章并获得了堆栈溢出的答案之后,除了将请求的数据从servlet传递到JSP时,没有其他地方可以使用请求和分派器.对我来说似乎不对,但会议也不对.有人能对此有所启发吗?

After reading several articles and stack overflow answers, I can find nowhere that presents any other option than using the request and a dispatcher when passing data from a servlet to a JSP. It doesn't seem right to me, but neither does the session. Can anybody shed some light on this?

推荐答案

我不确定我是否完全理解该问题,但是两种模式是最佳实践:

I'm not sure I fully understand the problem, but two patterns are best practices:

  1. 总是经过一个控制器,该控制器填充模型,将其存储在请求中,然后分派到一个视图,该视图在模型中显示数据.这就是MVC模式
  2. 总是在成功的非幂等请求(即POST,如果遵守HTTP协议)之后重定向.那就是后重定向获取模式.

所以这意味着您应该拥有:

So what it means is that you should have:

  • 请求1进入servlet.
  • servlet获取要在表单中显示的数据并将其存储在请求中,然后转发到JSP
  • JSP显示表单
  • 将表单提交到servlet(请求2)
  • servlet将数据存储在数据库中,该数据库将为创建的数据生成一个ID
  • servlet重定向到/product?id=<generatedId>/product/<generatedId>
  • 之类的URL
  • 浏览器向该URL发送一个请求(请求3).该请求发送到servlet
  • servlet从数据库获取ID标识的数据.它将数据存储到请求中,然后转发到JSP
  • JSP显示数据.
  • request 1 goes to a servlet.
  • servlet gets data to be displayed in the form and stores it in the request, then forwards to the JSP
  • the JSP displays the form
  • the form is submitted to a servlet (request 2)
  • servlet stores the data in the database, which generates an ID for the created data
  • servlet redirects to a URL like /product?id=<generatedId> or /product/<generatedId>
  • browser sends a request to this URL (request 3). This request goes to a servlet
  • servlet gets the data identified by the ID, from the database. It stores the data into the request, and forwards to a JSP
  • the JSP displays the data.

当然,您可以选择重定向到其他页面,例如产品列表.

Of course, you could choose to redirect to some other page, like the list of products for example.

如果麻烦的是在从servlet传输到JSP时使用请求存储数据,那么这应该不会打扰您:这是唯一的干净方法.数据将仅在请求范围内,并在处理请求后被垃圾回收.

If what bothers you is to use the request to store data when forwarding from the servlet to the JSP, then that shouldn't bother you: it's the only clean way to do it. The data will be scoped to the request only, and be garbage-collected when the request has been processed.

这篇关于将数据传递到表单时的Post-Redirect-Get?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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