ArrayList的传递从servlet来JSP [英] Passing ArrayList from servlet to JSP

查看:646
本文介绍了ArrayList的传递从servlet来JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ArrayList中包含从服务器对象到JSP。但

Servlet的文件:

 了request.setAttribute(servletName,所属分类); //所属分类是一个ArrayList包含类类对象
getServletConfig().getServletContext().getRequestDispatcher(\"/GetCategory.jsp\").forward(request,response);

JSP文件:

  //类别类
<%分类类别=新类别();
//创建一个类型类别类的ArrayList对象
ArrayList的<类别及GT;列表= ArrayList的<类别及GT;();
//存储从JSP传递的值
清单= request.getAttribute(servletName);的for(int i = 0; I<则为list.size();我++){类别= list.get(ⅰ);通过out.println(category.getId());通过out.println(category.getName());通过out.println(category.getMainCategoryId());
}
%GT;


解决方案

在这个servlet code,与指令了request.setAttribute(servletName,所属分类),保存您的列表中请求对象,并使用名称servletName为闯民宅它。结果
顺便说一句,使用然后命名servletName的名单还是比较混乱的,也许这是最好称之为名单或类似的东西:了request.setAttribute(名单,所属分类)结果
总之,假设你不改变你的Serlvet code和存储使用名为servletName列表中。当你到达你的JSP,有必要从请求检索列表,并为你只需要在 request.getAttribute(...)方法。

 <%
//从请求检索您的清单,铸造
ArrayList的<类别及GT;名单=(ArrayList的<分类和GT;)request.getAttribute(servletName);//打印有关列表的每个类别的信息
对于(分类类别:名单){
    通过out.println(category.getId());
    通过out.println(category.getName());
    通过out.println(category.getMainCategoryId());
}
%GT;

I am trying to pass ArrayList which contains object from servlet to JSP. But

Servlet file:

request.setAttribute("servletName", categoryList); //categorylist is an arraylist      contains object of class category  
getServletConfig().getServletContext().getRequestDispatcher("/GetCategory.jsp").forward(request,response);

JSP file:

//category class    
<% Category category = new Category();
//creating arraylist object of type category class
ArrayList<Category> list = ArrayList<Category>();
//storing passed value from jsp
list = request.getAttribute("servletName");

for(int i = 0; i < list.size(); i++) {

category = list.get(i);

out.println( category.getId());

out.println(category.getName());

out.println(category.getMainCategoryId() );
}
%>

解决方案

In the servlet code, with the instruction request.setAttribute("servletName", categoryList), you save your list in the request object, and use the name "servletName" for refering it.
By the way, using then name "servletName" for a list is quite confusing, maybe it's better call it "list" or something similar: request.setAttribute("list", categoryList)
Anyway, suppose you don't change your serlvet code, and store the list using the name "servletName". When you arrive to your JSP, it's necessary to retrieve the list from the request, and for that you just need the request.getAttribute(...) method.

<%  
// retrieve your list from the request, with casting 
ArrayList<Category> list = (ArrayList<Category>) request.getAttribute("servletName");

// print the information about every category of the list
for(Category category : list) {
    out.println(category.getId());
    out.println(category.getName());
    out.println(category.getMainCategoryId());
}
%>

这篇关于ArrayList的传递从servlet来JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆