Passing parameters from jsp to Spring Controller method

前端 未结 2 1961
借酒劲吻你
借酒劲吻你 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:26

    Your controller method should be like this:

    @RequestMapping(value = " /<your mapping>/{id}", method=RequestMethod.GET)
    public String listNotes(@PathVariable("id")int id,Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        int id = 2323;  // Currently passing static values for testing
        model.addAttribute("person", new Person());
        model.addAttribute("listPersons", this.personService.listPersons());
        model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
        return "note";
    }
    

    Use the id in your code, call the controller method from your JSP as:

    /{your mapping}/{your id}
    

    UPDATE:

    Change your jsp code to:

    <c:forEach items="${listNotes}" var="notices" varStatus="status">
        <tr>
            <td>${notices.noticesid}</td>
            <td>${notices.notetext}</td>
            <td>${notices.notetag}</td>
            <td>${notices.notecolor}</td>
            <td>${notices.sectionid}</td>
            <td>${notices.canvasid}</td>
            <td>${notices.canvasnName}</td>
            <td>${notices.personid}</td>
            <td><a href="<c:url value='/editnote/${listNotes[status.index].noticesid}' />" >Edit</a></td>
            <td><a href="<c:url value='/removenote/${listNotes[status.index].noticesid}' />" >Delete</a></td>
        </tr>
    </c:forEach>
    
    0 讨论(0)
  • 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:

    <input type="text" name="id" />
    <input type="submit" />
    

    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
    0 讨论(0)
提交回复
热议问题