user2446815
user2446815

Reputation: 25

Passing a list of values from jsp to Action class in Struts 2

I have the following JSP code to display a list of values

<table>
    <tr>
        <td>
            Name
        </td>
        <td>
            Age
        </td>
        <td>
            Id
        </td>
    </tr>
    <s:iterator value="resultList">
        <tr>
            <td>
                <s:textfield name="Name" />
            </td>
            <td>
                <s:textfield name="Age" />
            </td>
            <td>
                <s:textfield name="Id" />
            </td>
        </tr>
    </s:iterator>
</table>

<table>
    <tr>
        <td>
            <s:submit action="finalSubmit" value=" Submit " />
        </td>
    </tr>
</table>

Its displays a list of result. Now I want to pass all the Id values to the action class. I have defined the action name in struts.xml. Now in the action class only the last value of the Id is coming. But I need all the Ids in the action class.

Upvotes: 0

Views: 4370

Answers (1)

Roman C
Roman C

Reputation: 1

In the action, that should be configured with the name finalSubmit create the property

List<Long> allIds = new ArrayList<Long>();
//create getter and setter 

In the JSP rename the field that has Id value and add the iterator status attribute.

<s:iterator var="row" value="resultList" status="status">
  <s:textfield name="allIds[%{#status.index}]" value="%{#row.Id}"/>

upon submit all ids should come to the list above.

Upvotes: 1

Related Questions