如何将 ArrayList 对象(从会话)传递到 JSP 页中声明的变量中



我在JavaAction类List products = miscDao.getInsSubProdsByLoc("Y", locCntryId);中创建了一个ArrayList,并使用session.setAttribute("products", products );将其设置为会话

现在,我想在我的 JSP 文件中使用此数组列表。所以我做到了

<%
ArrayList<String> populated = (ArrayList)session.getAttribute("products");
%> 

当我检查上面的列表时,我们正在将对象放入该数组中。我希望与该对象对应的值以数组的形式保存在 javascript 函数中,如下所示。

var Countries = ['ARGENTINA', 
'AUSTRALIA', 
'BRAZIL', 
'BELARUS', 
'BHUTAN',
'CHILE', 
'CAMBODIA', 
'CANADA', 
'CHILE', 
'DENMARK', 
'DOMINICA'];

谁能帮我解决这个问题。实际上我正在创建自动完成功能,为此我需要将值从该数组列表传递到文本框。

ArrayList<String> populated = (ArrayList)session.getAttribute("products");

完成此操作后,使用 JSTL 打印它们中的每一个:

<c:forEach items="${populated}" var="item">
${item}<br>
</c:forEach>

在 servlet 代码中,首先将列表保存在请求对象中,并使用名称"countryList"或任何您想要的名称:

request.setAttribute("countryList",listObj);

当你到达你的JSP时,有必要从请求中检索列表,为此你只需要request.getAttribute(...(方法。 喜欢:

request.getAttribute("requiredList");
<%  
// retrieve your list from the request, with casting 
ArrayList<String> list = (ArrayList<String>) request.getAttribute("countryList");
// print the information about every category of the list
for(String country: countryList) {
out.println(country);
}
%>

最新更新