web-dev-qa-db-fra.com

Passer des paramètres de jsp à la méthode Spring Controller

Je travaille dans une application Spring MVC qui utilise Hibernate.

Dans la page JSP, j'ai une fonction qui répertorie les valeurs stockées dans la base de données (actuellement toutes les valeurs).

J'ai écrit une méthode, où la liste n'est limitée qu'à un ID passé dans le fichier JSP. J'ai bien fait fonctionner la requête HQL, donc je sais qu'elle récupère des données en fonction de l'ID en tant que paramètre.

Maintenant, je voudrais utiliser cette méthode dans le contrôleur. Pour cela, je dois passer un paramètre d'ID à la liste, donc du côté du contrôleur, la fonction est appelée qui récupérera la liste en fonction de cet ID.

Malheureusement, je ne sais pas comment passer les paramètres d'un fichier JSP.

Fichier JSP:

  <c:url var="addAction" value="/note/add" ></c:url>
<form:form action="${addAction}" commandName="notices">
    <table>
        <c:if test="${!empty notices.notetext}">
            <tr>
                <td>
                    <form:label path="noticesid">
                        <spring:message text="noticesid"/>
                    </form:label>
                </td>
                <td>
                    <form:input path="noticesid" readonly="true" size="8"  disabled="true" />
                    <form:hidden path="noticesid" />
                </td>
            </tr>
        </c:if>
        <tr>
            <td>
                <form:label path="notetext">
                    <spring:message text="notetext"/>
                </form:label>
            </td>
            <td>
                <form:input path="notetext"  />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="notetag" >
                    <spring:message text="notetag"/>
                </form:label>
            </td>
            <td>
                <form:input path="notetag"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="notecolor">
                    <spring:message text="notecolor"/>
                </form:label>
            </td>
            <td>
                <form:input path="notecolor" />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="canvasid">
                    <spring:message text="canvasid"/>
                </form:label>
            </td>
            <td>
                <form:input path="canvasid" />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="sectionid">
                    <spring:message text="sectionid"/>
                </form:label>
            </td>
            <td>
                <form:input path="sectionid"  />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="canvasnName">
                    <spring:message text="canvasnName"/>
                </form:label>
            </td>
            <td>
                <form:input path="canvasnName"  />
            </td>
        </tr>


        <tr>
            <td colspan="2">
                <c:if test="${!empty notices.noticesid}">
                    <input type="submit"
                           value="<spring:message text="Edit note"/>" />
                </c:if>
                <c:if test="${empty notices.notetext}">
                    <input type="submit"
                           value="<spring:message text="Add note"/>" />
                </c:if>
            </td>
        </tr>
    </table>
</form:form>
<br>
<h3>Notes List</h3>

<c:url var="listAction" value="/note/list/2323" ></c:url>
<c:if test="${!empty notices.noticesid}">
    <table class="tg">
        <tr>
            <th width="80">Notes ID</th>
            <th width="120">Notes text</th>
            <th width="120">Note Tag</th>
            <th width="120">Note color</th>
            <th width="120">Note section</th>
            <th width="120">Canvas id</th>
            <th width="120">Canvas name</th>
            <th width="120">Other id</th>
            <th width="60">Edit</th>
            <th width="60">Delete</th>
        </tr>
        <c:forEach items="${listNotes}" var="notices">
            <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/${notices.noticesid}' />" >Edit</a></td>
                <td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td>
            </tr>
        </c:forEach>
    </table>
</c:if>

Fichier contrôleur avec fonction liste:

@RequestMapping(value = "/note/list/{id}", method=RequestMethod.GET)
    public String listNotes(@PathVariable int id,Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        this.setSectionid(id);
        model.addAttribute("person", new Person());
        model.addAttribute("listPersons", this.personService.listPersons());
       model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
        return "note";
    }

@RequestMapping(value= "/note/add")
    public String addNote(@ModelAttribute("notices") Notes p,Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        model.addAttribute("listNotes",this.notesService.listNotes());

        int id = getSectionid();
        System.out.println("Section id is"+id);
         model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
        this.notesService.addNote(p, person);
        return "note";
    }

J'ai essayé de chercher sur le net, mais je ne sais pas comment ça s'appelle que je cherche, donc j'ai du mal. Toute aide serait bonne. Merci.

8
We are Borg

Votre méthode de contrôleur devrait être comme ceci:

@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";
}

Utilisez le id dans votre code, appelez la méthode du contrôleur depuis votre JSP comme:

/{your mapping}/{your id}

MISE À JOUR:

Changez votre code jsp en:

<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>
6
rahul

Utilisez @ RequestParam pour passer un paramètre à la méthode du gestionnaire de contrôleur. Dans le jsp, votre formulaire doit avoir un champ de saisie avec name = "id" comme suit:

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

Ensuite, dans votre contrôleur, votre méthode de gestionnaire devrait être la suivante:

@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";
}

Veuillez également consulter ces réponses et ce didacticiel:

7
user957654