You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rubbinio <rs...@n-able.com> on 2008/01/07 14:35:49 UTC

How to write JSP to populate back a hashmap of custom objects

Hi,

we have defined the following custom object:

public class GenericElement{
       private String key;
       private String value;

       public GenericElement(){
       }

       public String getKey() {
              return key;
       }

       public void setKey(String key) {
              this.key = key;
       }

       public String getValue() {
              return value;
       }

       public void setValue(String value) {
              this.value = value;
       }     
}  


And the following action class that uses a HashMap of such objects:

public class CommunicationSettingsAction extends ActionSupport {

       private Map list;
 
      public String save() throws Exception {
             //here should be populated
       }

       public String load() throws Exception{
              this.list = new HashMap();
              GenericElement ge = new GenericElement();
              ge.setKey("key one");
              ge.setValue("one value");
              this.list.put("one", ge);

              ge = new GenericElement();
              ge.setKey("two");
              ge.setValue("two value");
              this.list.put("two", ge);

              return SUCCESS;
       }

       public Map getList() {
              return list;
       }

       public void setList(Map list) {
              this.list = list;
       }
}

Finally, the JSP looks like this:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/includes/body_header.jsp" %>

              
              

                     

                           	 /images/customer_l.gif 
                           	         
                     
              

                        
              
                     
                     
                     
              
              
                  
       




Now the action executes fine and we get back a list with one object in it.
So that is OK. However the key is always set to null so when we have
multiple objects on the page (i.e. multiple sets of textboxes ) on submit we
only get back the last object since at every step it is over written because
the key is not set.

What needs to be done to have the key for each object in the hash map be set
back as well.

We have seen cases where this was done with Struts 2 by hard coding the key
but how can one do it dynamically ?

Thanks
-- 
View this message in context: http://www.nabble.com/How-to-write-JSP-to-populate-back-a-hashmap-of-custom-objects-tp14665986p14665986.html
Sent from the Struts - User mailing list archive at Nabble.com.

Re: How to write JSP to populate back a hashmap of custom objects

Posted by Rubbinio <rs...@n-able.com>.
Great,

thank you so much. It works fine. 

We had: <s:textfield value="" name="list[%{key}].key"></s:textfield>
And should have had: <s:textfield value=""
name="list['%{key}'].key"></s:textfield>

What a difference two ' make !


jimkski wrote:
> 
> Hi Rubbino-
> 
> The vita rara blog provided a good example of using the Map method to
> update a list of domain entries.  
> 
> http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities
> 
> I followed that example exactly and was able to get a form submit using
> maps working.  
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-write-JSP-to-populate-back-a-hashmap-of-custom-objects-tp14665986p14696848.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: How to write JSP to populate back a hashmap of custom objects

Posted by jimkski <ji...@hotmail.com>.
Hi Rubbino-

The vita rara blog provided a good example of using the Map method to update
a list of domain entries.  

http://www.vitarara.org/cms/struts_2_cookbook/updating_a_list_of_domain_entities

I followed that example exactly and was able to get a form submit using maps
working.  

-- 
View this message in context: http://www.nabble.com/How-to-write-JSP-to-populate-back-a-hashmap-of-custom-objects-tp14665986p14666537.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org