Can jsp: param store a collection?

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 fine, and the packageList parameter is passed as a request attribute by the action handler that invokes this page. In fact, packageList is of type Collection <GenericBean>.

It turned out that this page (the fragment that it stores) is actually very useful and can be used in many places with different collections. So I tried to include this page as follows (on 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 packageList argument. I tried to rewrite the fragment like this (since now it is 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 throws an exception, because it treats packingList as a string, not a collection. Therefore, right now the solution looks like this: set the required collection as an attribute in the code of the action handler:

 // 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 the jsp: param tag get the collection as a value? I read the documentation for JSP tags and it remains unclear - it seems that the only thing you can pass in this way is string parameters (or something that can be converted to string), but not complex objects.

+4
source share
1 answer

you must use the tag file and declare the tag with the correct parameter types.

eg. like 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 put 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 information

+6
source

Source: https://habr.com/ru/post/1338063/


All Articles