I have a requirement that seems to be an extension of binding checkboxes.
Let's say I have a list of possible phone number types (home, business, cell). This list is to be displayed on a jsp page (in a specific order) where a user can enter 0 or more phone number values using input boxes.
I've searched through similar posts and have pieced together an initial solution to my problem. I have the jsp displaying the phone number types in order and have values being associated with the correct types. If I submit the form, no errors occur but nothing happens as well since I haven't implemented any binding logic.
I've looked at posts related to custom PropertyEditors but can't seem to wrap my head around the way Spring binds data yet. Am I correct in assuming that implementation of a PropertyEditor will handle re-populating the text boxes with entered values after a validation failure?
Am I on the right track? What should be my next step in terms of binding the data to my object model?
Any help is appreciated.
Here's what I have so far:
A basic phone number class which I reuse for both the available and current list:
In my command object I store the current phone numbers using a List:Code:public class PhoneNumber { String type = null; String description = null; String value = null; ... getters and setters }
In my controller's referenceData() I hardcode the available list:Code:public class User { List phoneNumbers = null; ... getters and setters }
In my controller's formBackingObject, I hardcode phone number values:Code:List phoneNumbers = new ArrayList(); PhoneNumber f = new PhoneNumber(); f.setType("C"); f.setDescription("Cellular"); phoneNumbers.add(f); PhoneNumber d = new PhoneNumber(); d.setType("H"); d.setDescription("Home"); phoneNumbers.add(d); PhoneNumber e = new PhoneNumber(); e.setType("B"); e.setDescription("Business"); phoneNumbers.add(e); data.put("phoneNumberList", phoneNumbers);
In my jsp I combine everything together. Not sure how the <spring:bind> tag is used in this case:Code:List phoneNumbers = new ArrayList(); PhoneNumber a = new PhoneNumber(); a.setType("H"); a.setValue("111 111-1111"); phoneNumbers.add(a); PhoneNumber b = new PhoneNumber(); b.setType("B"); b.setValue("222 222-2222"); phoneNumbers.add(b); form.setPhoneNumbers(phoneNumbers);
and finally when I view page source, i get what I expected:Code:<c:forEach items="${phoneNumberList}" var="phoneNumber"> <tr> <td>${phoneNumber.description}</td> <td> <spring:bind path="command.phoneNumbers"> <c:set var="found" value="false"/> <c:forEach items="${command.phoneNumbers}" var="phoneNumberValue"> <c:if test="${phoneNumberValue.type == phoneNumber.type}"> <c:set var="found" value="true"/> <input type="text" name="phone${phoneNumber.type}" value="${phoneNumberValue.value}" /> </c:if> </c:forEach> <c:if test="${found == 'false'}"> <input type="text" name="phone${phoneNumber.type}" value="" /> </c:if> </spring:bind> </td> </tr> </c:forEach>
Code:<tr> <td>Cellular</td> <td> <input type="text" name="phoneC" value="" /> </td> </tr> <tr> <td>Home</td> <td> <input type="text" name="phoneH" value="111 111-1111" /> </td> </tr> <tr> <td>Business</td> <td> <input type="text" name="phoneB" value="222 222-2222" /> </td> </tr>


Reply With Quote
. <spring:bind>, IMHO, actually doesn't directly relate to the binding process during form submission. <spring:bind> is a convenient tag to bind the value, name, and error messages (collectively referred to as "status") of a property to jsp scripting variables. It makes it a lot easier and less error-prone to render a form that will eventually come back to the server-side of the binding process, where "binding" as in "parsing request parameters into command properties."