You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alex <az...@gmail.com> on 2010/03/01 18:56:08 UTC

Inserting data into nested collections

Hi list,


I am having hard time figuring out how to solve what seems to be a somewhat
trivial problem ...

I am using Struts 2 and OGNL in my JSP to send textfield values in my Action
Class

The struts Action looks something like this :

public class MyAction extends ActionSupport {

       HashMap<String, ArrayList<String>> customAltTerms = new
HashMap<String, ArrayList<String>>();

       /**
        * @return the customAltTerms
        */
       public HashMap<String, ArrayList<String>> getCustomAltTerms() {
               return customAltTerms;
       }

       /**
        * @param customAltTerms the customAltTerms to set
        */
       public void setCustomAltTerms(HashMap<
String, ArrayList<String>> customAltTerms) {
               this.customAltTerms = customAltTerms;
       }


public String execute() throws Exception{

.... some code here to test what's in the hashmap after the form has been
submited

}

 .... other methods of the Action class


}



I am unable to insert the data collected from the textfields of the form
into the HashMap.


I have tried the following in my JSP :


<s:textfield name="customAltTerms['someKey1'].value" />

or


<s:textfield name="customAltTerms['someKey1'].value[0]" />

or

<s:textfield name="customAltTerms['someKey1'][0]" />

or

<s:textfield name="customAltTerms['someKey1'].[0]" />


I almost always end up with java.lang.String cannot be cast to
java.util.ArrayList
java.lang.ClassCastException: java.lang.String cannot be cast to
java.util.ArrayList
etc ...

I also tried replacing the ArrayList with a String[] with the same result
(ClassCastException).

However if I use a HashMap<String, String> the key and the value are
populated correctly.But of course I only get one value per key which is not
what I intend to do.

What syntax must I use in the "name" attribute of the textfield to have the
actual value of the textefield inserted in the ArrayList containend in the
HashMap for the given key?


Thank you for helping me.

Re: Inserting data into nested collections

Posted by Alex <az...@gmail.com>.
Hi Adam !

Thanks for your feedback. I did manage to populate my collection by hacking
a little on the client side and by using javascript and hidden fields but
that's a really dirty way to do it in my opinion....

I'd love to hear from others on this list to see if they managed to populate
nested collecctions in Struts2


Cheers,

Alex.

RE: Inserting data into nested collections

Posted by adam pinder <ap...@hotmail.co.uk>.
 
Alex,
 
just checked my code again and it required me to handle the parameters myself - i added some code into the prepare method and checked request parameters and updated the arraylist myself (it was a while ago i had this problem).
 
In struts1 it would have worked, i couldn't get it to work properly in struts2 even though forum suggestions etc say it should work.
 
Apologies for my initial email.
 
adam


----------------------------------------
> Date: Mon, 1 Mar 2010 23:25:23 +0100
> Subject: Re: Inserting data into nested collections
> From: azlist1@gmail.com
> To: user@struts.apache.org
>
> Thanks Adam for the fast reply.
>
> I'm not sure howver that your solution would solve my problem. I'll try to
> inspire from it however : )
>
> I found this issue which looks pretty much related to my problem :
>
> http://jira.opensymphony.com/browse/XW-408
>
> Is this a known issue in Struts 2 and OGNL ?
>
> Thansk you for your help and suggestions ! 		 	   		  
_________________________________________________________________
Got a cool Hotmail story? Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Inserting data into nested collections

Posted by Alex <az...@gmail.com>.
Thanks Adam for the fast reply.

I'm not sure howver that your solution would solve my problem. I'll try to
inspire from it however : )

I found this issue which looks pretty much related to my problem :

http://jira.opensymphony.com/browse/XW-408

Is this a known issue in Struts 2  and OGNL ?

Thansk you for your help and suggestions !

RE: Inserting data into nested collections

Posted by adam pinder <ap...@hotmail.co.uk>.
 
Alex,
 
i used the following and it worked ok with struts2.
 
In JSP :-
 
<s:textfield name="opportunity.opportunityItem[0].quantity" value="12" size="5" maxlength="5" theme="simple" cssClass="mandatory" onblur="setListPrice(%{#rowstatus.index})"/>

In Action, i have a get/set for opportunity which is an object with the following property (with get/setters)
 
     private Collection opportunityItems = new ArrayList(0);

and the following additional method in the opportunity class
 
      public OpportunityItem getOpportunityItem(int occ)
      {
       Collection coll = this.getOpportunityItems();
       Object[] objs = coll.toArray();
       
       OpportunityItem oppItem = null;
       if (objs[occ] instanceof OpportunityItem)
       {        
        oppItem = (OpportunityItem)objs[occ]; 
       }
       
       return oppItem;
      }

you could cast the collection to ArrayList and use .get(occ) but i wanted to be able to change collection type.
 
struts2 then takes "opportunity.opportunityItem[0].quantity" and does...
 
getOpportunity().getOpportunityItem(0).setQuantity(12)
 
i've found the utility methods are sometimes needed to handle arrays.
 
if it was just a repeating field you could define a String[] and have the fields with the same name and struts would populate it for you.
 
regards
adam 		 	   		  
_________________________________________________________________
Do you have a story that started on Hotmail? Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org