Struts 2 nesting iterators

家住魔仙堡 提交于 2019-11-27 15:35:44

Try this:

<s:iterator var="parent" value="parents">
    <s:iterator var="child" value="#parent.children">
        <s:property value="#child.name"/>
    <s:iterator>
<s:iterator>

It works for me:

<s:iterator value="parents">
    <s:iterator value="children">
        <s:property value="name" />
    </s:iterator>
</s:iterator>

This is how the JSP code will look like:

    <s:form action="saveaction" >
        <s:iterator value="lstBean" id="lstBean" status="outerStat">
            <s:textfield value="%{name}" name="lstBean[%{#outerStat.index}].name"/>
            <s:textfield value="%{amt}" name="lstBean[%{#outerStat.index}].amt"/>
            <s:textfield value="%{id}" name="lstBean[%{#outerStat.index}].id"/>
            <s:iterator value="%{lstString}" status="myStat">
                <s:textfield name="lstBean[%{#outerStat.index}].lstString[%{#myStat.index}]"/>
            </s:iterator>
        </s:iterator>
        <s:submit value="Click me to submit lstBean"/>
    </s:form>

Following is the bean(XBean) whose List is used in the JSP:

public class XBean
{    
private ArrayList<String> lstString=new ArrayList<String>();
private String name;
private Double amt;
private Integer id;
//Getters and setters of fields
}

Now you can simply have a field lstBean with setters in your submitting action (saveaction) and hey you are done.

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.

<s:iterator value="countries">
    <s:iterator value="cities">
        <s:property value="name"/>, <s:property value="[1].name"/><br>
    </s:iterator>
</s:iterator>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!