0

I've tried to display on JSP page objects' properties from Map in FormBean. Map is defined as

Map<KeyObject, ValueObject> m

KeyObject has two properties

public class KeyObject implements Comparable<KeyObject> {
    private Integer a;
    private Integer b;

    getters/setters/and rest basic methods
}

On JSP I want to obtain something like code below:

<c:forEach items="${formBean.m}" item="itm">
    ...
    <form:input path="m[itm.key].propertyName" />
    ...
</c:forEach>

I need to:

  • display elements in proper order
  • submit objects to map

So is there any simple solution or I should do some "magic"?

Thanks for your time.

Stefan


Some more information. Each object will have other "view" so I try to use c:import

<c:forEach items="${formBean.m}" item="itm">
    <c:import url=${itm.value.name}Page.jsp" />
</c:forEach>

and on ...Page.jsp I want to use form's inputs.

stefan.m
  • 58
  • 8
  • possible duplicate of [JSTL access a map value by key](http://stackoverflow.com/questions/924451/jstl-access-a-map-value-by-key) – NimChimpsky Nov 29 '12 at 13:58
  • JSTL yes, but not only. I think that I need to use some code to (maybe initBinder) properly convert POST data to objects. I've check link that you provide but it doesn't contain any usefull information. – stefan.m Nov 29 '12 at 14:17
  • I don't understand then, whats you specific question ? You have asked about three or four different things. – NimChimpsky Nov 29 '12 at 14:20
  • 1. How display values from Map where key is object? 2. How submit those data and create Map (key and value)? – stefan.m Nov 29 '12 at 14:30
  • 1 is answered in my answer, 2) Submit data via a form, and 3 that is dependent on deserialization in your controller (spring uses jackson for this) – NimChimpsky Nov 29 '12 at 14:33
  • 1) When I'm saying "display" I mean "display in input". form:input won't work. There will be java.lang.ClassCastException: java.lang.String cannot be cast to KeyObject. 2) How deserialize key objects? – stefan.m Nov 29 '12 at 14:45
  • Why don't you post your actual code, and the controller (using spring-mvc?) – NimChimpsky Nov 29 '12 at 16:57

1 Answers1

1
<c:forEach items="${formBean.m}" varStatus="itm">
    <tr>      
      <td>${itm.key.propertyName}</td>
      <td>${itm.value.propertyName}</td> <!--which is same as below ...  -->
      <td>${formBean.m[itm.key].propertyName}</td>
    </tr>
</c:forEach>

You can iterate through the maps keys and values, like above, and output different fields as required.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311