繁体   English   中英

JSTL foreach var发送到jsp / servlet

[英]JSTL foreach var sending to jsp/servlet

我想知道一种方法,该如何将JSTL foreach的1个指定项目发送到其他JSP或servlet,以打印有关该项目的详细信息。

提供以下代码:

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

我需要替换表格或重新制作表格,才能完成某些工作,但是我没有主意...

啊,顺便说一句。 注意具有上面显示的方法+ showDetails()带有String返回。 我想全部显示它们,或者只执行String all = note.toString(); 在jsp / servlet中

只需传递一个标识符作为请求参数,然后将URL更改为servlet一个即可。 如果您坚持使用提交按钮,请将标识符作为隐藏的输入字段传递:

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

在映射到/showdetails的URL模式的servlet中,只需在doGet()方法中执行以下工作:

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

然后,可以在showdetails.jsp使用${note}来访问选定的Note

确实,当您使用链接代替Bozho建议时,上述servlet也可用,只有一点点不同,在servlet中,您可以预处理请求(找到并准备合适的Note对象以显示)。

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

也可以看看:

您不需要<form> 只需使用一个唯一标识项目的字段添加链接:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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