JSP/Struts2/Hibernate:循环遍历自引用表
假设我们有一个名为 PERSON 的自引用表,具有以下列:ID、PARENT,其中 PARENT 是 PERSON 表中另一个元素的 ID 列的外键。当然,许多人可以有同一个父母。
我使用 Hibernate 3 的惰性获取模式来处理数据库。 Hibernate 从数据库中获取 person 元素,然后通过 Struts2 操作将其放入 ValueStack 中,以便在结果 JSP 页面上使用。
现在的问题是:在JSP中,我怎样才能显示这个person元素的所有子元素(以及子元素的子元素,等等,就像家谱一样)?
当然,对于 n+1 个孩子我可以使用 < s:迭代器>标记 person.person。我还可以嵌套另一个 < s:迭代器>标记 person.person.person 以获取 n+2 个子节点。
但是,如果我想以自动方式执行此操作,直到最后一个 n+p 子元素,并在进程中显示所有 n+1..n+p 元素的所有子元素,该怎么办?
我希望我已经说得足够清楚了。感谢大家抽出宝贵的时间。
——TBW。
Let's say we have a self-referencing table called PERSON, with the following columns: ID, PARENT, where PARENT is a foreign key to the ID column of another element in the PERSON table. Of course, many persons can have the same parent.
I use Hibernate 3 in lazy fetching mode to deal with the database. Hibernate fetches a person element from the database, which is then put in the ValueStack by the Struts2 action, to be used on the result JSP page.
Now the question is : In JSP, how can I do to display all the child (and the child's child, and so on, like a family tree) of this person element?
Of course, for the n+1 children I can use the < s:iterator> tag over the person.person. I can also nest another < s:iterator> tag over person.person.person to get the n+2 children.
But what if I want to do this in an automated manner, up to the last n+p child, displaying in the process all the children of all the n+1..n+p elements?
I hope I have been clear enough. Thank you all for your time.
-- TBW.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 JSTL 和自定义标记文件轻松完成此操作。这个想法是标签是递归的。请注意,WebLogic 11g 的某些早期版本存在一个错误,导致递归标记无法正常工作,但总的来说,Servlet 容器和应用程序服务器都很好地支持这一点。
person.tag
(我只是凭记忆写了这个示例,可能需要一些细微的调整)
因为您是延迟加载的,所以您可能需要使用在视图中打开会话< /em> 模式,如四元数提到的(不需要 Spring)。
You can do this pretty easily with JSTL and a custom tag file. The idea is that the tag is recursive. Note that some early versions of WebLogic 11g had a bug that prevented recursive tags from working properly, but in general this is well supported by servlet containers and app servers.
person.tag
(I just wrote the example from memory, it may need some slight tweaks)
Because you're lazy-loading, you'll probably want to use the Open Session in View pattern, as Quaternion mentioned (Spring not required).