struts-user mailing list archives

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


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.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" %>
              <table border="0" cellpadding="0" cellspacing="0">
                           <td> /images/customer_l.gif </td>
                           <td class="PageTitle"><s:text
              <s:form action="save_CommunicationSettings" method="POST">          
              <s:iterator value="list">
                     <s:textfield value=""
                     <s:textfield value=""
                     <s:property value="value"/>
              <s:submit label="Save"></s:submit>

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 ?

View this message in context:
Sent from the Struts - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message