I'm using the Struts 2 framework and I want to transfer a list of objects from Java to JSP and back to Java. Transferring to JSP works fine with the <s:iterator>
tag. Transferring back to Java does not, my list is never populated.
I already checked these questions:
- Repopulate ArrayList from JSP with Struts 2
- a list of list value populated in struts2 iterator
- Populate Collection from Struts2 Form Submission
- Populate List<String> in struts2 from form data
and followed their suggestions.
Here is my "item" class (it is has more properties than just name, but I'm only showing relevant ones):
Class Item:
private String name;
public Item(){}
public String getName(){ ... };
public void setName(String newName){ ... };
and on my JSP I have:
<s:iterator value="items" status="key">
<s:hidden name="items[%{#key.index}].name" value="%{name}" />
</s:iterator>
Here's the relevant part of the action class:
private List<Item> items = new ArrayList<Item>();
public List<Item> getItems()
{
System.out.println("now getting Items");
if(Items == null)
{
System.out.println("Items is null");
}
else
{
System.out.println("Items is not null. size: " + Items.size());
}
return Items;
}
public void setItems(List<Item> Items)
{
System.out.println("now setting Items");
Items = Items;
}
I'm 100% sure it has all the necessary getters and setters because the JSP is populated correctly, and when I submit my form I can actually see (with log statements) that my getItems()
method is being called once for each item in my list (for each input field in the form). It's just weird that Struts2 is getting the list once for every hidden input field in the form, but then refuses to create an Item object and set it's name to the given value anything in it. The problem cannot be the lack of a no-args constructor.
I also added an conversion file named with ActionName-conversion.properties
(where ActionName
is the name of my action class) and it is in the same folder as my ActionName.java
class.
Element_items = Item
CreateIfNull_items = true
What might be wrong?