如何更改客户端点击链接时返回的 URL
我有一个案例,我有一个列表页面,其中列出了所有项目,并且每个项目都有一个删除链接。用户可以点击此链接删除此项,删除链接格式为: http: //localhost:8080/list.htm?op=del&id=1234
当用户点击删除链接时,将返回到列表页面。但这有一个问题,当用户刷新页面时,它会再次调用删除操作,从而导致错误。那么,当服务器端返回响应时,如何更改 URL?我希望链接仍然存在 http://localhost:8080/list.htm
谢谢 顺便说一句,我正在使用 Spring MVC
Jeff 张
I have a case that I have a list page which list all the items and each item has a delete link. User can click this link to delete this item, this is the delete link format: http://localhost:8080/list.htm?op=del&id=1234
When user click the delete link, it will return to the list page. But this has a problem that when user refresh the page, it will invoke the delete operation again which will cause error. So How can I change the URL when the server side set back the response? I want the link still to be
http://localhost:8080/list.htm
Thanks
BTW I am using Spring MVC
Jeff Zhang
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您在删除操作中不返回任何数据,只需返回一个 HTTP 303 响应,并将 Location 标头设置为“list.htm”。或者,您可以在收到响应后通过客户端的 JavaScript 执行此操作:
但是,我建议您有一个单独的端点来删除。使用名为 list 的端点来删除对象是没有意义的;相反,该链接应该到达一个端点,例如delete.htm?id=1234(然后可以发送303以重定向回list.htm)。
Assuming you are not returning any data on the delete operation, just return an HTTP 303 response with the Location header set to 'list.htm'. Alternatively, you can do this via JavaScript on the client side after the response is received:
I would suggest, however, having a separate endpoint to delete. Using an endpoint called list to delete an object doesn't make sense; instead, the link should hit an endpoint such as delete.htm?id=1234 (which can then send the 303 to redirect back to list.htm).
我能想到的一个简单方法是将删除代码放在单独的页面上,
然后再次将用户重定向到列表页面。
我对 Spring MVC 不太了解,但看起来你正在使用 PHP 来删除它,
通过判断?在网址中。 (或者也许不是 .htm,但无论如何)
PHP 中的重定向是
header( 'Location: list.htm' ) ;
?>
假设 list.htm 位于同一目录中作为新的删除页面。
所以有一个基本的想法,是否使用 PHP 取决于你,我不知道你的其余代码是什么样的,所以我无法提供更多帮助。
Well a simple way that I can think of is to place the delete code on a seperate page,
then to redirect the user to the list page again.
I don't know much about Spring MVC but it looks like you're using PHP to delete it,
judging by the ? in the URL. (or maybe not at it is .htm, but whatever)
A redirect in PHP would be
<?php
header( 'Location: list.htm' ) ;
?>
That's assuming that list.htm is in the same directory as the new delete page.
So there's a basic idea, whether you use PHP or not is up to you, I don't know what the rest of your code looks like etc so I can't help much more.
解决问题的模式称为 Post/Redirect/Get。接收并处理 POST(或 DELETE,如果您愿意)后,您想要向客户端发送 HTTP 重定向,以便他们的浏览器通过 GET 请求加载列表页面。
如果您使用标准的 UrlBasedViewResolver,您可以简单地执行以下操作:
查看 15.5.3 Spring 文档以获取更多信息。
The pattern for solving your problem is called Post/Redirect/Get. After receiving and processing the POST (or DELETE if you wish) you want to send the client an HTTP redirect so that their browser loads the list page with a GET request.
If you are using the standard UrlBasedViewResolver, you can simply do:
Take a look at section 15.5.3 of the Spring docs for some more info.