Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a list of Objects in Struts2

 
Gopal Venkatakrishnan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic