There is a controller, on a post request, it takes information from the form and saves it to the database (a new link is formed), then I want to make it possible to edit this information, but everything that was in the form is reset. How to make the possibility of further editing?
Example: Initially the page is available at localhost: 8090, I’m typing information into the fields, after saving the link looks like localhost:8090/TTTTTTTT , and all the fields are empty, but it’s necessary that they are filled in as before. Controller code:
@Controller public class MainController { @Autowired public PostService postService; @GetMapping("/") public String index() { return "index"; } @PostMapping("/") public String savedPost(@RequestParam String title, @RequestParam String username, @RequestParam String story) { Post post = new Post(); post.setTitle(title); post.setUsername(username); post.setStory(story); postService.add(post); return "redirect:/" + post.getURI(); } @GetMapping("/{URI}") public String getPost(@PathVariable String URI, Model model){ Post post = postService.loadPostByID(URI); model.addAttribute("title", post.getTitle()); model.addAttribute("username", post.getUsername()); model.addAttribute("story", post.getStory()); return "index"; } } form itself:
<!DOCTYPE HTML> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <title>Ght</title> </head> <body> <form method="post"> <div class="form-group"> <input type="text" name="title" class="form-control" placeholder="Title"> </div> <div class="form-group"> <input type="text" name="username" class="form-control" placeholder="Your name"> </div> <div class="form-group"> <textarea class="form-control" name="story" rows="3" placeholder="Your story..."></textarea> </div> <button class="btn btn-primary" type="submit">PUBLISH</button> </form> </body> </html>