Struts 2 嵌套迭代器
我简直不敢相信,这么简单的事情在 Struts 2 中似乎很难做到。
这大概就是我想做的事情,因为它会在 Java 中完成。
for (Parent parent : parents){
for (Child child: parent.getChildren()){
System.out.println(child.getName());
}
}
这应该转化为 Stuts 标签中类似的内容:
<s:iterator var="parent" value="parents">
<s:iterator var="child" value="parent.children">
<s:property value="child.name"/>
<s:iterator>
<s:iterator>
我认为 parent.children 应该类似于 ${%(#parent.children)} 但我还没有找到 ${% 的正确组合(要使用的 # 个字符:-)。我还可以使用一个页面链接来解释何时使用其中的一个。
I can't believe how something this simple can seem so hard to do in Struts 2.
This is approximately what I would like to do as it would be done in Java.
for (Parent parent : parents){
for (Child child: parent.getChildren()){
System.out.println(child.getName());
}
}
That should translate to something close to this in Stuts tags:
<s:iterator var="parent" value="parents">
<s:iterator var="child" value="parent.children">
<s:property value="child.name"/>
<s:iterator>
<s:iterator>
I assume parent.children should be something like ${%(#parent.children)} but I have not found a right combination of ${%(# characters to use :-). I could also use a link to a page explaining when to use which one of these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
它对我有用:
It works for me:
JSP 代码如下所示:
以下是 JSP 中使用其 List 的 bean(XBean):
现在您只需在提交操作 (saveaction) 中添加一个带有设置器的字段 lstBean,嘿,您就完成了。
This is how the JSP code will look like:
Following is the bean(XBean) whose List is used in the JSP:
Now you can simply have a field lstBean with setters in your submitting action (saveaction) and hey you are done.
对于 Struts 2.3.x,您可以使用此示例,摘自 http://struts.apache.org/release/2.3.x/docs/iterator-tag-examples.html
在此示例中,“国家/地区”是国家/地区对象的列表,每个对象都有一个名称和城市列表。每个城市都有一个名字。
它们引用堆栈上的特定位置:“[1]”。堆栈顶部位置 0 包含当前城市,由内部迭代器推送;位置 1 包含当前国家/地区,由外部迭代器推送到那里。
For Struts 2.3.x you can use this example, extracted from http://struts.apache.org/release/2.3.x/docs/iterator-tag-examples.html
In this example, 'countries' is a list of country objects, each of which has a name and a list of cities. Each city has a name.
They refer to a specific position on the stack: '[1]'. The top of the stack, position 0, contains the current city, pushed on by the inner iterator; position 1 contains the current country, pushed there by the outer iterator.