开发者

How to change the returned URL when client click a link

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 H开发者_如何学运维ow 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


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:

window.location = '/list.htm'

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).


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.


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:

return "redirect:your-list-view";

Take a look at section 15.5.3 of the Spring docs for some more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜