2011-02-02 185 views
4

問候,這是問題所在。我有一個網頁叫做entList.jsp,與收集工作:可以jsp:param存儲一個集合嗎?

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

一切的偉大工程,並PACKINGLIST,作爲調用這個頁面的操作處理程序請求的屬性被傳遞的參數。其實packingList集合<GenericBean>

事實證明,這個頁面(它存儲的片段)實際上非常有用,並且可以在許多具有不同集合的地方使用。於是,我就包括這個頁面是這樣的(在另一頁):

<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標籤的文檔,目前還不清楚 - 看起來你可以通過這種方式傳遞字符串參數(或者可以轉換爲字符串的東西),但沒有複雜的對象。

回答

6

您應該使用標記文件,並使用正確的參數類型聲明標記。

例如作爲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更多信息

相關問題