用户刷新页面时,将重新提交HTML表单-如何阻止这种情况发生? [英] HTML form is resubmitted when user refreshes page - how do I stop this from happening?

查看:225
本文介绍了用户刷新页面时,将重新提交HTML表单-如何阻止这种情况发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户提交html表单(使用method ="post")之后,它会将他们带回到他们所在的页面(但其中一些数据已更改).如果用户刷新页面,则将再次提交表单.在我的应用程序中,这可能会创建一个非法状态(因为表单数据是在未经验证的情况下提交的.)

After a user submits an html form (with method="post") it brings them back to the page they were on (but with some of the data changed). If the user refreshes the page, the form is submitted again. In my application this can create an illegal state (since the form data is submitted without going through verification).

当用户刷新页面时,如何防止提交表单?

How can I prevent the form from being submitted when the user refreshes the page?

下面是一些示例代码,可让您基本了解发生的情况:

Here is some sample code to give you a basic idea of what is going on:

<script type="javascript">
function verifyForm() {
//do stuff
}
...
<form onsubmit="return verifyForm();" method="post">
<input name="testing" type="text" size="8" />
<input name="save" type="submit value="Submit Form" />
</form>

然后在JSP控制器中:

Then in the JSP controller:

public class myController implements Controller
{ 
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
// do stuff with the form data received in the request
}
}

我可以在几个地方解决此问题:在html页面本身或在控制器中(在jsp中使用MVC模式).到目前为止,我已经发现此问题这似乎几乎适用于我的问题,但似乎并没有问同样的事情(也是我没有使用PHP).

There are several places I might be able to solve this problem: in the html page itself, or in the controller (using MVC pattern in jsp). So far I have found this question that almost seems to apply to my question, but doesn't quite seem to be asking the same thing (also I am not using PHP).

推荐答案

您需要使用PRG模式:Post/Redirect/Get.

You need to use the PRG pattern: Post/Redirect/Get.

在此处阅读:http://en.wikipedia.org/wiki/Post /重定向/获取

这篇关于用户刷新页面时,将重新提交HTML表单-如何阻止这种情况发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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