問題描述
我正在使用 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 Class
和yyy
是集合元素的屬性我們要索引.
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
usingKeyProperty_xxx=yyy
, wherexxx
is the property of the beanClass
that returns the collection andyyy
is the property of the collection element that we want to index on.
這篇關于如何在 Struts 2 中將值插入 Set 集合的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!