Friday, June 8, 2018

How to fill a map inside a map inside a Spring form?

Leave a Comment

I have the following form

public class myForm { private String code; (getter and setter) private Map<String, Map<String, Object>> map(getter and setter) } 

I can fill the code attribute easily but i don't know how to proceed to fill the map, i don't even know if it's possible ...

This is my Spring form

<form:form commandName="myForm" action="${PostUrl}" method="POST" >   <input type="hidden" path="code" value="78967" />   <input type="submit" value="Submit"/> </form:form> 

I will know the key of the first map and i will know the key of the second map, only the value of the second map will be enter by the user.

To try to be as clear as possible here is in java what i wish to do with my form

Map<String, Map<String, Object>> map1 = new HashMap<String, map<String,  Object>>(); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("DatePickerLabel", DatePickedByTheUser) map1.put("DATEPICKER", map2) 

1 Answers

Answers 1

As there is best option you can do it by the help of inner bean .

public class myForm { private String code; (getter and setter) private Map<String,InnerBeanObject> map(getter and setter) } 

And if you are using inner bean you can do a setter injection to fill your

Map<String, Object> 

If you are using xml bean configuration

    <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">          <bean id="MyFormBean" class="Myform">          <property name="code" >          <property name="map">             <map>             <entry key="Key " value-ref="innerBean"></entry>             </map>         </property>         </bean>         <bean id="innerBean" class="InnerBean">                     <property name="InnerBeanMap">                     <map>                       //get ur bean map key and value                    </map>         </bean>       </beans> 

There is similar way by java code also as you know if you are using earlier version of spring.by annotation @bean

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment