如何在 Struts 2 中将值插入 Set 集合

2022-01-12 00:00:00 type-conversion java jsp struts2 ognl

我正在使用 Struts2 做一个项目,但在分配集合时遇到问题.

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();

}

这是我的模型Team(我去掉了不相关的部分)

Here's my model Team (I eliminated the irrelevant part)

private TeamId id;
private Set students = new HashSet(0);

这是我的 JSP 部分

Here's my JSP part

<input type="text" name=team.student[0].id />

现在的问题是我无法通过 ModelDriven 将正确的值插入到这个 Set 集合中,它会抛出异常.你能告诉我在 JSP 文件中写什么,以便我可以在我的模型中向 Set 集合插入一个值吗?

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 是一个 Collection 并且任何其他集合都可以通过属性进行索引.

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.

在 JSP 中

<s:iterator value="students " var="student">
  <s:textfield name="students(%{#student.id}).name" />
</s:iterator>


有关此的更多信息,请参阅


More about this see

按集合的属性索引集合.

也有可能获得一个独特的元素通过传递给定属性的值来收集那个元素.默认情况下,属性集合的元素在<Class>->conversion.properties 使用KeyProperty_xxx=yyy,其中 xxx 是属性返回集合的 bean Classyyy 是集合元素的属性我们要索引.

It is also possible to obtain a unique element of a collection by passing the value of a given property of that element. By default, the property of the element of the collection is determined in <Class>->conversion.properties using KeyProperty_xxx=yyy, where xxx is the property of the bean Class that returns the collection and yyy is the property of the collection element that we want to index on.

相关文章