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> 
  • Use html instead of any template engine to substitute the values ​​transferred from the controller to the form fields. - Sergey Gornostaev

2 answers 2

In your form there is only an option for data entry. If you need to edit as checks for errors before saving in the database, add the verification conditions either on the front or on the back (patterns, etc.). If you need to edit the values ​​already entered and received from the database, you need to substitute them into the fields, for this, use JS

    In the controller, you have already added attributes to the model so that they can be rendered in a twist. Now you can use EL to get attributes.

     <input type="text" name="title" class="form-control" placeholder="Title" value="${title}"> <input type="text" name="username" class="form-control" placeholder="Your name" value="${username}"> <textarea class="form-control" name="story" rows="3" placeholder="Your story..." value="${story}"></textarea> 

    It is assumed that you have a view twist resolver for rendering JSP and / or JSTL. About the view resolvers can be read here .

    • that's what I do not understand. I have a form on the form that triggers on the post method, and how to make it work on the gett too? - Alex
    • This is why? If the form get a gett, then you need to change the mapping. - Roman C