在JSP页面之间传递列表/数组 [英] Passing list/array between JSP pages

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

问题描述

我正在尝试在我拥有的两个JSP页面之间传递列表.这是我编写的类的对象列表.

I'm trying to pass a List between two JSP pages that I have. This is a list of objects that is of a class that I wrote.

如何在JSP页面之间传递此列表? request.setAttribute似乎适用于字符串,但不适用于其他任何东西.而且,如果使用列表无法轻松完成此操作,则可以将列表转换为数组并以这种方式传递它,这没问题.

How do I pass this list between JSP pages? request.setAttribute seems to work for strings, but not anything else. And, if this cannot be easily done with a list, I can convert the list to an array and pass it that way, no problem.

推荐答案

第一件事是,一个非常糟糕的设计将导致诸如在不同JSP页面之间传递列表之类的问题. 扼杀罪恶之源"将是创建一个单独的java类,其中包含列表并对其进行初始化,然后您可以根据需要在任意多个jsp页面上访问列表.

The first thing is that a very bad design will lead to such questions as passing lists between different JSP pages. The "nip the evil at the bud" will be to create a separate java class which contains the list and initializes it, then you can access the list at as many jsp pages as you want.

但是,如果您确实想这样做,可以将列表放入会话中.

But incase you really want to do, you can put the list in the session.

request.getSession().setAttribute("list",myListObject);

然后在另一页上您将获得

Then on the other page you can get

List<MyType>myListObject=(List<MyType>) request.getSession().getAttribute("list");

然后,您在不需要会话后应从会话中清除该列表,

And you should clear the list from the session after you do not require it,

request.getSession().removeAttribute("list");

这篇关于在JSP页面之间传递列表/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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