• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating a list of Objects in Struts2

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic