简体   繁体   中英

JSTL foreach var sending to jsp/servlet

I want to know a method how can I send 1 specified item for JSTL's foreach to other JSP or servlet, to print detailed info about that item.

Giving followin code:

    <c:forEach var="note" items="${notes}">
        <tr>
            <td>${note.getTitle()}</td>
            <td>${note.getRealDate()}</td>
            <td>${note.getUserDate()}</td>
            <td>
                <form action="showdetails.jsp">
                    <input type="submit" value="show details" name="details" />
                </form> 
            </td>
        </tr>
    </c:forEach>

I need to replace my form, or remake it, to get something that does the job, and I'm out of ideas...

ah, btw. Note has method shown above + showDetails() with String return. And I want to display them all, or just do String all = note.toString(); in jsp/servlet

Just pass an identifier as request parameter and change the URL to be a servlet one. If you insist to use a submit button, pass the identifier as a hidden input field:

<form action="showdetails">
    <input type="hidden" name="id" value="${note.id}" />
    <input type="submit" name="details" value="show details" />
</form>

In the servlet which is mapped on an URL pattern of /showdetails just do the following job in doGet() method:

String id = request.getParameter("id");
Note note = noteService.find(id);
request.setAttribute("note", note);
request.getRequestDispatcher("/WEB-INF/showdetails.jsp").forward(request, response);

In the showdetails.jsp you can then use ${note} to access the selected Note .

Exactly the above servlet is also useable when you use a link instead as Bozho suggests, with only one little difference, in the servlet you're able to preprocess the request (to find and prepare the right Note object for display).

<a href="showdetails?id=${note.id}">show details</a>

See also:

You don't need a <form> . Just add a link, using a field that uniquely identifies the item:

<a href="showdetails.jsp?noteId=${note.id}">show details</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM