【问题标题】:setting values for hashset in struts2struts2中hashset的设置值
【发布时间】:2012-05-24 04:40:30
【问题描述】:

我正在使用 Struts2。我的 pojo 中有一个哈希集。我正在尝试将值提交到哈希集。我无法将我的收藏类型更改为列表。

这里是波乔

Item{
Set<Person> personCollection;
long itemCode;

    public void setItemCode(long itemCode)
    {
        this.itemCode=itemCode;
    }
    public long getitemCode()
    {
        return itemCode;
    }
    public void setPersonCollection(Set<Person>personCollection)
    {
        this.personCollection=personCollection;
    }
    public Set<Person> getPersonCollection()
    {
        return personCollection;
    }
}

Person{
    String name;
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
}

动作

SubmitItemAction
{
     private Item item;
     public getItem()
     {
          return item;
     }
     public setItem(Item item)
     {
          this.item=item;
     }
     public String submitItem()
     {
           dao.submit(item);
     }
}

jsp

  <s:text name=item.personCollection[0].name/>
  <s:text name=item.personCollection[1].name/>

所以这不起作用。当我使用上面的 sn-p 提交我的 jsp 时,它给出了错误,它无法从 Item 中填充 personCollection。

那么jsp中的命名约定应该是什么?就像如果 personCollection 是我可以使用 item.personCollection[0].someProperty 的列表一样。但是你如何设置类型集的集合的名称。

【问题讨论】:

  • 你必须在 JSP 中这样做吗?为什么不行动?
  • 简而言之-问题是如何在jsp中直接使用HashSet来提交一组值?

标签: java struts2 ognl


【解决方案1】:

在你的提交操作中使用列表,然后你可以在 jsp 中使用这个列表和索引。

这里不能使用 set,因为 set 不能使用索引访问

在您的业务逻辑中,将列表转换为集合,因为您可能需要为进一步的 orm 设置集合。

【讨论】:

  • 暂时这看起来不错。但我正在寻找更通用的解决方案。因为可能有多个使用 POJO 的集合实例。感谢您的回复。
  • 你是对的。可以尝试使用 struts 的迭代器标记。就我个人而言,我没有这样做,并且只在几个地方需要这个。如果您取得任何成功,请更新您的答案。
  • 我在 jsp 中使用迭代器标签。当然,如果找到更好的解决方案,我会更新帖子。
猜你喜欢
  • 2022-01-18
  • 2013-07-22
  • 2013-03-16
  • 2016-06-25
  • 1970-01-01
  • 2013-12-23
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多