如何在 Struts 2 中的 Set 集合中插入一个值 [英] How to insert a value into Set collection in Struts 2

查看:22
本文介绍了如何在 Struts 2 中的 Set 集合中插入一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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

通过集合的属性索引集合.

也可以得到一个唯一的元素通过传递给定属性的值来收集那个元素.默认情况下,属性集合的元素在->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.

这篇关于如何在 Struts 2 中的 Set 集合中插入一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆