开发者

Can jsp:param store a collection?

Gree开发者_高级运维tings, 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜