Mark Mark - 5 months ago 9
Java Question

POST an array of custom objects to a Struts 2 action

How do I POST an array of custom objects to a Struts 2 action in Java?

For example if I have the following Java object:

public class Person {

private String name;
private String lastName;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}


And the following Action:

public class SavePersons extends ActionSupport {

private List<Person> persons;

@Override
public String execute() throws Exception {
// Do something
return SUCCESS;
}

public void setPersons(List<Person> persons) {
this.persons = persons;
}

}


I'd like to do something like this in an HTML form:

<html>
<body>
<form method="POST" action="http://postHere">
<input type="text" name="persons[0].name" value="Name1"/>
<input type="text" name="persons[0].lastName" value="LastName1"/>
<input type="text" name="persons[1].name" value="Name2"/>
<input type="text" name="persons[1].lastName" value="LastName2"/>
<input type="submit" />
</form>
</body>
</html>


Any tips?

Answer

What you have looks good. It does not make a difference to struts2 if you post or get as far as setting values.

Using the same SavePersons class, except that I added a public List<Person> getPersons() method. This is required to make the solution work.

And using essentially the same form, except I prefer to write my forms using s2 tags where it makes sense (what puts some people off the form tags is the default s2 theme, you can globally set the theme to simple, the label attribute will not work but the UI tags will work just like you'd expect similar html elements to behave):

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Person Form</title>
    </head>
    <body>
        <h1>Person Form</h1>
        <s:form action="person-test" method="post">
            <s:textfield name="persons[0].name" label="fName 1"/>
            <s:textfield name="persons[0].lastName"  label="lName 1"/>
            <s:textfield name="persons[1].name" label="fName 2"/>
            <s:textfield name="persons[1].lastName" label="lName 2"/>
            <s:submit/>
        </s:form>
    </body>
</html>

Note that method="post" is not needed, it is the default.

Here is the page used to display the form data.

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>List of People</h1>
        <s:iterator value="persons">
            <s:property value="name"/> <s:property value="lastName"/><br/>
        </s:iterator>
    </body>
</html>

And it worked just fine.