I found an Article on this in Ian Roughley's book. Posting here if someone
is implementing Tabular Data Entry support in their apps.
*Utilize Tabular Data Entry Support*
Struts2 provides support for using lists to transfer tabulated data
easily between the HTML user interface and actions. Let's take a
look at an example. Here is a class for Person; each attribute has
a getter and setter (not shown):
public class Person {
int id;
String name;
int age;
float height;
}
Our action would then use the person class in a list:
public class MyAction {
public List getPeopleList() { … }
public void setPeopleList( List peopleList ) { … }
…
}
Before we can use the Person class as an element in MyAction,
we need to add configuration information. This is achieved with
the "MyAction-conversion.properties" file, which is created in
the same package as MyAction. The name follows the same
convention as for validation, the name of the action followed by
a "*-conversion.properties" suffix. The file contents are:
Element_peopleList=Person
The prefix "Element_" is constant, with the last part of the lefthand
value being the name of the list property in the action class.
The right-hand side value is the full class name (including
package) of the class that is placed into the list.
To finish the example we need to render the list to the user:
<ww:iterator value="peopleList" status="stat">
<s:property value="peopleList[#stat.index].id" />
<s:property value="peopleList[#stat.index].name" />
<s:property value="peopleList[#stat.index].age" />
<s:property value="peopleList[#stat.index].height"/>
</ww:iterator>
Lists are indexed, so we use the index property of the iterators
status object to reference the element being displayed. This is
not the most efficient way of achieving this particular result, as
the value of the <s:property … /> tags could have been simply
"id", "name", "age" and "height" respectively. What it does
show is a clean form of what is needed for an editable form.
For a tabular editable form using the same objects, the JSP is:
<s:form action="update" method="post" >
<s:iterator value="peopleList" status="stat">
<s:hidden
name="peopleList[%{#stat.index}].id"
value="%{peopleList[#stat.index].id}"/>
<s:textfield label="Name"
name="peopleList[%{#stat.index}].name"
value="%{peopleList[#stat.index].name}"/>
<s:textfield label="Age"
name="peopleList[%{#stat.index}].age"
value="%{peopleList[#stat.index].age}" />
<s:textfield label="Height"
name="peopleList[%{#stat.index}].height"
value="%{peopleList[#stat.index].height}"/>
<br/>
</s:iterator>
<s:submit value="Update"/>
</s:form>
Notice that the "name" and "value" attribute of this code is
similar to the "value" attribute above. The difference being in
the "name" we need to provide the actual index by surrounding
"#stat.index" with the correct token to obtain a value, and the
"value" attribute has the entire expression surrounded. Using
this code, Struts2 will create an ArrayList with populated
People objects, and set the list on the action using the
setPeopleList() method.
To allow Struts2 to create new objects in the list (perhaps you
are dynamically creating elements in the user interface), add the
following line to "MyAction-conversion.properties"
configuration file:
CreateIfNull_peopleList = true
On 11/1/07, John Doe <[EMAIL PROTECTED]> wrote:
>
> The simplest way (if I'have understood your problem right) is to create 10
> variables in your action class (with getters and setters of course) and on
> the form. So it'll be
>
> private String firstName1;
> private String firstName2;
> private String firstName3;
> ...
> private String firstName10;
>
> in the action class and :
>
> <s:textfield name="firstName1" size="25" maxlength ="20"/>
> <s:textfield name="firstName2" size="25" maxlength ="20"/>
> <s:textfield name="firstName3" size="25" maxlength ="20"/>
> ...
> <s:textfield name="firstName10" size="25" maxlength ="20"/>
>
> Struts fills action variables values automatically using reflection.
>
> On 11/1/07, Raghuveer Rawat <[EMAIL PROTECTED]> wrote:
> >
> > Hi, still looking for help.
> >
> >
> > On 10/31/07, Raghuveer Rawat <[EMAIL PROTECTED]> wrote:
> > >
> > > Hi, I have form which allows user to invite other users. A user can
> > invite
> > > a max of 10 users at a time. A row contain First Name, Last Name, and
> > Email
> > > Address.
> > > How should I name these textfields (OGNL expression) and how should I
> > > retrieve these values in Action Class?
> > >
> > >
> > > <table>
> > >
> > > <
> > > tr>
> > >
> > > <td>First Name</ td>
> > >
> > > <td>Last Name</ td>
> > >
> > > <td>Email Address</ td>
> > >
> > > </tr>
> > >
> > > <tr>
> > >
> > > <s:textfield name="firstName" size="25" maxlength ="20"/></td>
> > >
> > > <s:textfield name="lastName" size="25" maxlength ="20"/></td>
> > >
> > > <s:textfield name="email" size="25" maxlength= "20"/></td>
> > >
> > > </tr>
> > >
> > > similarly 9 more rows like this.
> > >
> > > </table>
> > >
> > >
> > >
> >
>
>
>
> --
> Best regards,
> Bashmaкov Anton
>