0

I got this message "The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?" when i try to refresh the page.

This is my controller method

@PostMapping("/savePost")
public String savePost(@RequestParam("content") String content,
        @RequestParam(value = "publicPost", required = false) String[] publicPost, ModelMap model) {
    boolean isPrivate = publicPost == null ? true : false;
    postService.savePost(userService.getLoggedUsername(), content, isPrivate);
    // Get user posts
    String username = userService.getLoggedUsername();
    if (username != null) {
        model.addAttribute("posts", postService.findUserPosts(username));
        return "createPost";
    } else {
        return "login";
    }
}

and This is my HTML form

<form action="/savePost" method="POST">
 .....
 </form>

any method how to solve it ?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • 1
    you are reloading a page where you entered some values, in the form, that you posted. Reloading the page will make the form post the values again. This is the standard behaviour for the browser in such situation – Lelio Faieta May 20 '19 at 10:23
  • @LelioFaieta so how to prevent it ? or how to solve it and prevent inserting the data twice into the DB ? – etharak etharak May 20 '19 at 10:33

2 Answers2

1

If you want an idempotent operation, i.e. one that you can send the same data to as often as you want, use a PUT method and implement the backend accordingly. That also means you cannot easily use the HTML form and will need to use scripting to send the data to the backend (make the PUT call). And on the backend that means you need to make sure that you don't create duplicates when you send the same data twice, which typically means either the path or the message body contain the id of the thing you want to update/create.

If you don't want to go that way, the current behaviour is as designed - and as you typically would want it to work.

Frank Hopkins
  • 639
  • 4
  • 12
0

At the end of this W3 Schools page, a table compares the HTTP GET and POST methods for storing & sending parameters, the first row notes the browser feature you describe, one solution could be to use the GET method in your form tag.

Phil
  • 311
  • 1
  • 6
  • Still the same problem appears – etharak etharak May 20 '19 at 10:39
  • you don't want to use GET for a storage operation. – Frank Hopkins May 20 '19 at 10:39
  • Another post on stack overflow (https://stackoverflow.com/questions/6833914/how-to-prevent-the-confirm-form-resubmission-dialog) says this method should work, has the app set a http parameter on your browser? (Dev tools) if there are any post parameters being sent in the http request, as @Frank said GET doesn't usually do everything we want, im not familiar with the put method he suggested, the other thread suggests using a post-redirect-get pattern, which is relatively simple to setup, so long as the application you are using has a simple session class (php does, not sure about spring-boot) – Phil May 20 '19 at 11:36