In most ATG operations, you update a Map property by updating a specific key value as if it were a servlet bean property:
<dsp:input type="text" bean="MyBean.value.addresses.city"/>
While addresses
is a Map property and city
is a key, this tag represents addresses
and city
as properties. Making a change to a key’s value requires entering text into a text box and submitting the operation.
The RepositoryFormHandler and Maps have several properties that offer more flexibility in updating Map properties:
editMapsAsLists is a RepositoryFormHandler property that, if set to true, lets you make changes to the Map keys and values.
keysAndRepositoryIds is a Map property that you can use to update the Map with a series of key/repository ID pairs.
keysAndValues is a Map property that you can use to update the Map with a series of primitive key/value pairs.
Note: The approach described in this section for updating Map properties applies only to the RepositorFormhandler. For a generic approach to setting Map properties in all forms, see the Map Properties section in the Forms chapter.
editMapsAsLists
You typically interact with Maps by treating them as servlet beans. For example, to expose a value, you specify the key as an input parameter. You cannot add new key/value pairs when you work with Maps in this way.
Alternatively, you can treat a Map as a list if the RepositoryFormHandler’s property editMapsAsLists
is set to true (the default setting). This lets you use dsp:setvalue
and dsp:input
tags to expose and update Maps like a list property.
To use this feature:
Make sure
editMapsAsLists
is set to true.Set the Map’s updateMode property to
append
,prepend
,replace
, orremove
. The default value isreplace
.Note: Because Map elements are unordered, update modes
prepend
andappend
have the same effect. The Map itself determines the actual placement of new key-value pairs.If the Map’s
updateMode
property is set toprepend
orappend
, also set its numNewValues property to a positive integer.Set up the form input fields as required by the
updateMode
setting. For example, ifupdateMode
is set toprepend
orappend
, the form should contain text boxes that accept values for the new Map entries to be added.
Note: All keys in a Map must be unique. If updateMode
is set to append
or prepend
and a key value in the form duplicates an existing Map key, on form submission the form values associated with the key overwrite the corresponding Map element.
For example, a page where users can enter home and work addresses might include this code:
<dsp:input type="hidden" bean="MyRepositoryFormHandler.value.addresses.numNewValues" value="1"/> <dsp:input type="hidden" bean=MyRepositoryFormHandler.value.addresses.updateMode" value="append"/> Enter Home Address: <dsp:input type="hidden" bean="MyRepositoryFormHandler.value.addresses.keys[0]" value="home"/> <br/>Street: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[0].street" value=""/> <br/>City: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[0].city" value=""/> <br/>State: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[0].state" value=""/> <br/>Postal Code: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[0].postalCode" value=""/> Enter Work Address: <dsp:input type="hidden" bean="MyRepositoryFormHandler.value.addresses.keys[1]" value="work"/> <br/>Street: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[1].street" value=""/> <br/>City: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[1].city" value=""/> <br/>State: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[1].state" value=""/> <br/>Postal Code: <dsp:input type="text" bean="MyRepositoryFormHandler.value.addresses[1].postalCode" value=""/>
This page supplies two sets of four text boxes, one set for home address data and identified by the key home
, and the second set for work address and identified by the key work
. On form submission, the RepositoryFormHandler uses the form values to add two new elements to the addresses
Map.
keysAndRepositoryIds
The keysAndRepositoryIds
property lets you simultaneously set a Map key and value, where the Map holds a series of keys that map to repository IDs. Without keysAndRepositoryIds
, performing this operation requires two input tags:
<dsp:input type="text" bean="MyRepositoryFormHandler.value.favorites.keys[0] value=""/> <dsp:input type="text" bean="MyRepositoryFormHandler.value.favorites.repositoryIds[0] value=""/>
Alternatively, a single input tag that specifies the Map’s keysAndRepositoryIds
property can set each key-repository ID pair. This property is typically set with the following format:
key
=
repository-id
Note: The string used to separate the key and repository ID must be the value set by the RepositoryFormHandler property mapKeyValueSeparator—by default, =
(equals).
You can use page parameters to set dynamic key repository ID values and configure a Format
bean to provide the syntax. For example, you might want to track the brands each customer prefers. Each brand is associated with a business, and one business can maintain several brands; so you want to organize the brands by parent business. Therefore, you display the brands as text boxes. When checked, you want the favorites
Map to save a business name and brand ID as the key
and repositoryId
respectively. You might design your code like this:
<dsp:form action="keysandids.jsp" method="post"> <dsp:input type="text" bean="MyRepositoryFormHandler.value.favorites.updateMode" value="prepend"/> <br/> <dsp:droplet name="/atg/dynamo/droplet/RQLQueryForEach"> <dsp:param bean="/atg/userprofiling/ProfileAdapterRepository" name="repository"/> <dsp:param name="itemDescriptor" value="business"/> <dsp:param name="queryRQL" value="all"/> <dsp:oparam name="output"> <dsp:droplet name="/atg/dynamo/droplet/Format"> <dsp:param name="key" param="index"/> <dsp:param name="repositoryId" param="element.repositoryId"/> <dsp:param name="format" value="{key}={repositoryId}"/> <dsp:oparam name="output"> <dsp:input bean="MyRepositoryFormHandler.value.favorites.keysAndRepositoryIds" paramvalue="message" type="checkbox"/> <dsp:valueof param="message">Not set</dsp:valueof> <br/> </dsp:oparam> </dsp:droplet> </dsp:oparam> </dsp:droplet> <dsp:input bean="MyRepositoryFormHandler.update" type="submit" value="Update"/> </dsp:form>
The RQLQueryForEach
and the ProfileAdapterRepository
retrieve the brands saved as business items in the repository. Assume that each brand displays in a text box in the page and when it is modified, the Format
bean associates each business name and brand ID to the key
and repositoryId
properties of a Profile component favorites
Map.
keysAndValues
You can configure keys and values that are primitive data types through a Map’s keysAndValues
property. As with the keysAndRepositoryIds property, you typically set this property with this format:
key
=
value
The string used to separate the key and its value must be the value set by the RepositoryFormHandler property mapKeyValueSeparator—by default, =
(equals).