A fairly common use case occurs where there is a list of Java objects, from which selections can be made on a web form - usually you'd use the primary key of the object as the value so that the controller could either do a lookup, or just bind the key to whichever object is created/updated.
My problem is that the list to choose from are not persistent, keyed objects, they are business models from a service which have no reasonable way to retrieve them based on the data contained. Below is some psuedo code where a list of Foo's are given to the page, and we can easily communicate to the controller onSubmit the name of Foo, but what if there are other fields of Foo that need to be submitted?
controller:
referenceData() {
...
List foos = fooService.getFoosForBar( bar )
return { 'foos', foos }
}
jsp:
<form>
...
<spring:bind path="formData.foo">
<select name="<c:out value="${status.expression}" />">
<c:forEach items="${foos}" var="foo">
<option value="<c:out value="${foo.name}"/>">
<c:out value="${foo.name}"/>
</option>
</c:forEach>
</select>
</spring:bind>
...
</form>
Some example solutions would be to use hidden fields to submit Foo's other properties and keep them in sync as the selection is changed, but I prefer not to use JavaScript in a situation like this where it will likely add confusion. There are certainly other ways to accomplish this too.
My question is does there exist any standard practice for accomplishing this? Or should I just come up with my own way of doing so? I'd rather not re-invent wheels if possible, and this is so seemingly common that just winging it may not be the best approach.
Based on your limitations, you must encode the other data memebers of foos as the value of the option.
<option label="${foo.name}" value="${foo.encodedValues}"/>
The encodedValues
method might look something like this:
private String SEPARATOR = ",";
public String getEncodedValues()
{
final StringBuffer returnValue = new StringBuffer();
returnValue.append(field1);
returnValue.append(SEPARATOR);
returnValue.append(field2);
returnValue.append(SEPARATOR);
returnValue.append(field3);
return returnValue.toString();
}
If you have a number of selects that need to have encoded values, you may want to create a class that does the encoding and decoding of these values to centralize the code.
You can use the index of the element in the list to get it back in the POST request.
<spring:bind path="formData.fooIndex">
<select name="<c:out value="${status.expression}" />">
<c:forEach items="${foos}" var="foo" varStatus="i">
<option value="<c:out value="${i.index}"/>">
<c:out value="${foo.name}"/>
</option>
</c:forEach>
</select>
</spring:bind>
In your POST handler, use foos.get(formData.getFooIndex())
If foos can change between the GET and POST requests, you need to put foos
in session so that you definitely reference the same object in your POST handler as you did in the GET handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With