I am doing a project using Struts2 and I have a problem assigning a set collection.
Here's my Action (I eliminated the irrelevant part)
public class TeamAction extends BaseAction implements ModelDriven<Team>
{
Team team=new Team();
}
Here's my model Team
(I eliminated the irrelevant part)
private TeamId id;
private Set students = new HashSet(0);
Here's my JSP part
<input type="text" name=team.student[0].id />
Now the problem is I can't insert the right value into this Set
collection in by ModelDriven
, it will throw a exception. Could you please tell me what to write in JSP file, so I can insert a value to Set
collection in my model?
Set
is a Collection
and as any other collection could be indexed by a property.
@Element(value = Student.class)
@Key(value = Integer.class)
@KeyProperty(value = "id")
@CreateIfNull(value = true)
private Set<Student> students = new HashSet(0);
//getter and setter, also for Student class that should have Integer id.
in JSP
<s:iterator value="students " var="student">
<s:textfield name="students(%{#student.id}).name" />
</s:iterator>
More about this see in Indexing a collection by a property of that collection.
来源:https://stackoverflow.com/questions/24186111/how-to-insert-a-value-into-set-collection-in-struts-2