[英]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.