jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes Populating a list of Objects in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Populating a list of Objects in Struts2" Watch "Populating a list of Objects in Struts2" New topic
Author

Populating a list of Objects in Struts2

Gopal Venkatakrishnan
Greenhorn

Joined: Mar 12, 2008
Posts: 1
Hi

I am trying to populate a list of objects using struts 2.0 s:iterate tag with java.util.List in the Action. I am able to load populate the data from Action to the form but after submitting the form the List in the action is not getting updated with the new data.

My Action class is like this:

public class TestAction extends ActionSupport {

private List<Employee> empList;

public String execute() throws Exception {

// after submitting i put break point here and checked the value of
// empList and the value is null
empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setCountry("1");
emp1.setName("amiya");
empList.add(emp1);
Employee emp2 = new Employee();
emp2.setCountry("2");
emp2.setName("gopal");
empList.add(emp2);
return SUCCESS;
}
public List getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
}


My form looks like this:

<s:form action="test">
<s:iterator value="empList">
<s:textfield name="name"></s:textfield>
</s:iterator>
<s:submit></s:submit>
</s:form>

What I need to change in the jsp or action so that the List in the Action reflects the data submitted.
kal til
Greenhorn

Joined: Apr 03, 2008
Posts: 2
Use the Struts automatic type conversion to facilitate your conversion.
Populate the fields in jsp page as

<s:form action="test">
<s:iterator value="empList" status="stat">
<s:textfield name="empList[%{#stat.index}].name" value="%{name}" label="name" />
</s:iterator>
<s:submit />
</s:form>

----------------------------------------------
Create conversion.properties file with name as ActionName-conversion.proeprties and add the following properties

Element_empList=Employee
CreateIfNull_empList=true
----------------------------------------------

In action class don't forget to create a Employee bean with setter().

Thanks,
Tilak
 
wood burning stoves
 
subject: Populating a list of Objects in Struts2
 
Similar Threads
Display ArrayList data on jsp page
struts2 iterator tag
Struts2 nested iterators with listboxes
couldn't get value back from <s:iterator> after submitting the <s:form>
struts 2 iterator