Friday, November 27, 2015

Associate a map property to user input field using converter

How to associate a map property to user input field while using formhandler?

HTML forms can set values to formhandler components using bean attribute of dsp:input tags. However if you want to associate user input values to a map attribute it can be done as follows,

<dsp:input bean="FormHandler.map-property" converter="map"  value="key=value" />

Suppose I have a map called primaryAddress in a custom formhandler MyFromHandler and addressLine1, addressLine2, city, state are attributes that needs to be captured from user. In this case we can set 4 input fields and can associated to map attributes directly.

<dsp:input bean="MyFromHandler.primaryAddress" converter="map" value="addressLine1=" />
<dsp:input bean="MyFromHandler.primaryAddress" converter="map" value="addressLine2=" />
<dsp:input bean="MyFromHandler.primaryAddress" converter="map" value="city=" />
<dsp:input bean="MyFromHandler.primaryAddress" converter="map" value="state=" />

Map property association can be done for multiple values loaded for a hidden variable as well.

<dsp:input bean="MyFromHandler.primaryAddress" converter="map" type="hidden" value="addressLine1=${loadedAddress.addressLine1}, addressLine2=${loadedAddress.addressLine2}, city=${loadedAddress.city}, state=${loadedAddress.state}">
</dsp:input> 


1 comment: