struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rubbinio <rsolo...@n-able.com>
Subject How to write JSP to populate back a hashmap of custom objects
Date Mon, 07 Jan 2008 13:37:06 GMT

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<String, GenericElement> list;
 
      public String save() throws Exception {
             //here should be populated
       }

       public String load() throws Exception{
              this.list = new HashMap<String,GenericElement>();
              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<String,GenericElement> getList() {
              return list;
       }

       public void setList(Map<String,GenericElement> list) {
              this.list = list;
       }
}

Finally, the JSP looks like this:

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/includes/body_header.jsp" %>
<html>
       <body>       
              <table border="0" cellpadding="0" cellspacing="0">
                     <tr>
                           <td> /images/customer_l.gif </td>
                           <td class="PageTitle"><s:text
name="label.modify.propagate.defaultSettings"/></td>         
                     </tr>
              </table>
              <s:form action="save_CommunicationSettings" method="POST">          
              <s:iterator value="list">
                     <s:textfield value=""
name="list[%{key}].key"></s:textfield>
                     <s:textfield value=""
name="list[%{key}].value"></s:textfield>
                     <s:property value="value"/>
              </s:iterator>
              <s:submit label="Save"></s:submit>
              </s:form>    
       </body>
</html>



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.


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


Mime
View raw message