To create an alternate shipping address, a registered member clicks on the add new address link to go to new_shipping_address.jsp
, where a form collects the address fields and the nickname for the new address. The form’s input tags are associated with the editValue
property of the B2CProfileFormHandler
; the Add Address button invokes the handleNewAddress()
handler as demonstrated in the following example.
Note: The “. . .” markers in the following code sample indicate a place where code has been removed to clarify the sample.
<dsp:form action="address_book.jsp" method="POST"> . . . Give this address a nickname for your address book:<br> <dsp:input bean="B2CProfileFormHandler.editValue.nickname" maxlength="40" size="20" type="text"/> <p> <!-- Store the ProfileID in ownerId field of the new address. This tells us this address "belongs to" (and can be edited) by the user. --> <dsp:input bean="B2CProfileFormHandler.editValue.ownerId" beanvalue="Profile.id" type="hidden"/> Name<br> <dsp:input bean="B2CProfileFormHandler.editValue.firstName" maxlength="100" size="15" type="text" required="<%=true%>"/> <dsp:input bean="B2CProfileFormHandler.editValue.middleName" maxlength="100" size="10" type="text"/> <dsp:input bean="B2CProfileFormHandler.editValue.lastName" maxlength="100" size="15" type="text" required="<%=true%>"/><br> Street address <br> <dsp:input bean="B2CProfileFormHandler.editValue.address1" maxlength="30" size="40" type="text" required="<%=true%>"/><br> <dsp:input bean="B2CProfileFormHandler.editValue.address2" maxlength="30" size="40" type="text"/><br> <table cellpadding=0 cellspacing=0> <tr> <td>City<br> <dsp:input bean="B2CProfileFormHandler.editValue.city" maxlength="30" size="20" type="text" required="<%=true%>"/></td> <td>State<br> <dsp:select bean="B2CProfileFormHandler.editValue.state"> <%@ include file="StatePicker.jspf" %> </dsp:select> </td> <td>Postal Code<br> <dsp:input bean="B2CProfileFormHandler.editValue.postalCode" maxlength="10" size="10" type="text" required="<%=true%>"/></td> </tr> </table> Country<br> <dsp:select bean="B2CProfileFormHandler.editValue.country" required="<%=true%>"> <%@ include file="CountryPicker.jspf" %> </dsp:select><br> <p> </td> </tr> <tr valign=top> <!-- 6. Submit: --> <td></td> <td></td> <td> <table width=100% cellpadding=0 cellspacing=0 border=0> <tr><td class=box-top-profile> Done?</td></tr> </table> <p> <br> <!-- Submit form to handleUpdate() handler --> <dsp:input bean="B2CProfileFormHandler.newAddress" type="submit" value=" Add Address --> "/> . . .
The handleNewAddress()
handler creates a new address in the secondaryAddresses
map, indexed with the provided nickname, and sets its properties based on the entries in the editValue
map property:
MutableRepository repository = (MutableRepository) profile.getRepository(); Map secondaryAddresses = (Map) profile.getPropertyValue("secondaryAddresses"); // What address fields are we looking for String[] addressProperties = getAddressProperties(); // Get editValue map, containing the user form data HashMap newAddress = (HashMap) getEditValue(); String nickname = (String) newAddress.get("nickname"); // If we successfully collected all needed user input, create a new address try { // Create a repository item to be filled with stuff. MutableRepositoryItem address = repository.createItem("contactInfo"); // Copy values from the newAddress object Object property; for (int i = 0; i < addressProperties.length; i++) { property = newAddress.get(addressProperties[i]); if (property != null) address.setPropertyValue(addressProperties[i], property); } // Update adress into the db and insert into secondaryAddresses map repository.addItem(address); secondaryAddresses.put(nickname, address);