jsp:param可以存储集合吗?
您好,问题来了。我有一个名为 entList.jsp 的页面,它与集合一起使用:
<c:forEach var = "pack" items = "${packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>
一切都很好,参数 packingList 作为请求的属性由操作处理程序传递调用此页面。实际上packingList是Collection
事实证明,这个页面(它存储的片段)实际上非常有用,并且可以在许多具有不同集合的地方使用。所以我尝试像这样包含这个页面(在另一个页面中):
<jsp:include page="entList.jsp">
<!-- Pass the required collection as a parameter-->
<jsp:param name = "packingList" value = "${traffic.packingList}"/>
</jsp:include>
但是,现在这个片段看不到参数packingList。我尝试像这样重写片段(因为现在它是一个参数):
<c:forEach var = "pack" items = "${param.packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>
但现在它生成一个异常,因为它将 packingList 视为字符串而不是集合。所以现在的解决方案是这样的 - 将所需的集合设置为操作处理程序代码中的属性:
// This is the original instance set by the action
request.setAttribute("traffic", traffic);
// And this is the additional one, to be used by entList.jsp
request.setAttribute("packingList", traffic.getPackingList());
所以问题是 - jsp:param 标记可以接收集合的值吗?我阅读了 JSP 标签的文档,但仍然不清楚 - 似乎唯一可以以这种方式传递的东西是字符串参数(或可以转换为字符串的东西),但没有复杂的对象。
Greetings, here is the problem. I have a page called entList.jsp, that works with a collection:
<c:forEach var = "pack" items = "${packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>
Everything works great, and the parameter packingList is passed as an attribute of the request by the action handler that calls this page. Actually packingList is of type Collection<GenericBean>.
It turned out that this page (the fragment it stores) is actually pretty useful, and can be used in many places with different collections. So I tried to include this page like this (in another page):
<jsp:include page="entList.jsp">
<!-- Pass the required collection as a parameter-->
<jsp:param name = "packingList" value = "${traffic.packingList}"/>
</jsp:include>
However, now this fragment does not see the argument packingList. I tried to rewrite the fragment like this (since now it's a parameter):
<c:forEach var = "pack" items = "${param.packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>
But now it generates an exception, since it treats packingList as a string and not a collection. So right now the solution is like this - set the required collection as an attribute in the action handler code:
// This is the original instance set by the action
request.setAttribute("traffic", traffic);
// And this is the additional one, to be used by entList.jsp
request.setAttribute("packingList", traffic.getPackingList());
So the question is - can jsp:param tag receive a collection as it's value? I read the documentation for the JSP tags and it remains unclear - it seems like the only thing you can pass in such a way it's string parameters (or something that can be converted to string), but no complicated objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用标记文件,并使用正确的参数类型声明标记。
例如
packingList.tag
然后,将此文件放在
WEB-INF/tags
中,然后添加到您的 jsp 文件中,
请参阅 http://download.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html 了解更多信息
you should be using a tag file, and declare the tag with the correct parameter types.
e.g. as
packingList.tag
Then, place this file in
WEB-INF/tags
then, add to your jsp file
see http://download.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html for more info