Passing parameters from jsp to Spring Controller method

前端 未结 2 1965
借酒劲吻你
借酒劲吻你 2020-12-16 20:24

I am working in a Spring MVC application which uses Hibernate.

In the JSP page I have a function which lists the values stored in the database(currently all the val

2条回答
  •  忘掉有多难
    2020-12-16 20:50

    Use the @RequestParam to pass a parameter to the controller handler method. In the jsp your form should have an input field with name = "id" like the following:

    
    
    

    Then in your controller, your handler method should be like the following:

    @RequestMapping("listNotes")
    public String listNotes(@RequestParam("id") int id) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        model.addAttribute("person", new Person());
        model.addAttribute("listPersons", this.personService.listPersons());
        model.addAttribute("listNotes", this.notesService.listNotesBySectionId(id, person));
        return "note";
    }
    

    Please also refer to these answers and tutorial:

    • Stackoverflow :: How take the parameter value of a GET HTTP Request
    • Stackoverflow :: Which Signature to use in spring's controller methods
    • Tutorial :: Spring MVC Annotations

提交回复
热议问题