Hi all I've got some jsp pages and im using struts2 to handle my forms. After submitting a form by user, the url shown in address bar becomes somthing.action, so when the user refreshes the page, the forms gets submitted again. How can I handle this? after submission of a form.
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
POST REDIRECT GET
This pattern needs to be followed to prevent re-submission of form on refresh. This means, after submitting a
POST
request,POST
should send aREDIRECT
response to fetch the destination page usingGET
. With this pattern, if the user refreshes the page, only the GET request happens again, so the same page is fetched without updating anything in server.This is a common design pattern recommended for web. Google would provide a lot of resources about this.
If the goal is to prevent duplicate submission of forms then use
token
interceptor http://struts.apache.org/2.x/docs/token-interceptor.html ortokenSession
interceptor http://struts.apache.org/2.x/docs/token-session-interceptor.html.If you simple want to refresh the page after submit without submitting again then redirect to action where you only show results not form. Use
redirectAction
result for that.+1 to both the other answers.
Post/Redirect/Get is the classic Pattern for every web technology.
Token Interceptor is another way to go, when you are using Struts2;
There is a third way to go, if you don't care about retro-compatibility with old browsers, or browsers with Javascript disabled: HTML5's
window.history.pushState
.Just reset the url to the original one after the page is loaded, and pressing F5 will get the original page, instead of re-submitting the request.