jsp:param可以存储集合吗?

发布于 2024-10-15 18:45:48 字数 1443 浏览 0 评论 0原文

您好,问题来了。我有一个名为 entList.jsp 的页面,它与集合一起使用:

<c:forEach var = "pack" items = "${packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>

一切都很好,参数 packingList 作为请求的属性由操作处理程序传递调用此页面。实际上packingListCollection类型。

事实证明,这个页面(它存储的片段)实际上非常有用,并且可以在许多具有不同集合的地方使用。所以我尝试像这样包含这个页面(在另一个页面中):

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2024-10-22 18:45:48

您应该使用标记文件,并使用正确的参数类型声明标记。

例如 packingList.tag

<%@tag %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@attribute name="packingList" required="true" type="java.util.Collection<Packing>"
        description="the packing list." %>
<c:forEach var = "pack" items = "${packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>

然后,将此文件放在 WEB-INF/tags

,然后添加到您的 jsp 文件中,

<%@ taglib tagdir="/WEB-INF/tags" prefix="pack" %>

<pack:packingList packingList="${packingList}"/>

请参阅 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

<%@tag %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@attribute name="packingList" required="true" type="java.util.Collection<Packing>"
        description="the packing list." %>
<c:forEach var = "pack" items = "${packingList}">
<!--Here goes something with outputting the pack instance in a required format-->
</c:forEach>

Then, place this file in WEB-INF/tags

then, add to your jsp file

<%@ taglib tagdir="/WEB-INF/tags" prefix="pack" %>

<pack:packingList packingList="${packingList}"/>

see http://download.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html for more info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文