在 JSP 页面之间传递列表/数组
我正在尝试在我拥有的两个 JSP 页面之间传递一个列表。这是我编写的类的对象列表。
如何在 JSP 页面之间传递此列表? request.setAttribute 似乎适用于字符串,但不适用于其他任何东西。而且,如果这不能用列表轻松完成,我可以将列表转换为数组并以这种方式传递它,没问题。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,非常糟糕的设计会导致诸如在不同 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.
Then on the other page you can get
And you should clear the list from the session after you do not require it,
最简单的答案是:这取决于。
如果您有例如
one.jsp
并且您调用重定向到second.jsp
- 您可以使用请求范围如果您有 one.jsp 和几页之后您想要显示您的list,那么您应该使用 session scope:
在 secondary.jsp 上显示您的列表:
yourListObject 您可以替换为
The simplest answer is: it depends.
If you have e.g.
one.jsp
and you call redirect tosecond.jsp
- you can use request scopeIf you have one.jsp and few pages later you want to display your list, then you should use session scope:
to display your list on second.jsp:
yourListObject you can replace by