【问题标题】:How to insert a value into Set collection in Struts 2如何在 Struts 2 中将值插入 Set 集合
【发布时间】:2014-08-02 21:06:14
【问题描述】:

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

这是我的操作(我删除了不相关的部分)

public class TeamAction extends BaseAction implements ModelDriven<Team>
{
 Team team=new Team();

}

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

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

这是我的 JSP 部分

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

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

【问题讨论】:

标签: java jsp struts2 type-conversion ognl


【解决方案1】:

Set 是一个Collection,并且任何其他集合都可以通过属性进行索引。

@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>

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

Indexing a collection by a property of that collection.

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

【讨论】:

  • 为什么我们使用(%{#student.id}) 我们可以在这里使用&lt;s:textfield name="students[%{#student.id}].name" /&gt;
  • 我们可以使用相同的 notatin 'In Jsp' 来获取 List 的输入吗
  • @xrcwrn 你自己试过了吗?我认为它应该给你准确的答案。
  • 嗯,新符号,不错。它也适用于检索吗?
  • 他们说[] 语法适用于地图,但它也适用于列表,上述解决方案适用于任何集合。
猜你喜欢
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 2018-07-07
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多