Search code examples
javastruts2ognl

setting values for hashset in struts2


I am using Struts2. I have a hashset in my pojo. I am trying to submit values to the hashset. There is no way i can change my collection type to list.

here is 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;
    }
}

Action

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

so this doesnt work. When i submit my jsp with above snippet it gives error, its not able to populate personCollection from Item.

So what should be naming convention in jsp. Like if personCollection would have been a list I could have used item.personCollection[0].someProperty. But how do you set the name for collection of type set.


Solution

  • Well in your submit action use the list and then you can use this list inside jsp with index.

    You cannot use set here as set cannot be accessed using indexes

    In your business logic convert the list into set as you may require set for further orm.