Reputation: 1
I am new to Spring and still learning. I want to make some more advanced form handling.
Currently my problem is dynamic list binding.
I want to have one text box, one list and add button. What is scenario? User populates text box(with autocomplete) and cliks add button. After initiation add action, list gets populated without issuing request to server. User adds few more items to list, and then submits form to server.
What is problem?
I dont know how to bind list or pass dynamic data to server.
Currently I have managed to get JSON response from Controller with list for autocomplete.
Is Spring forms suitable for this task? What is the right way to implement this?
Upvotes: 0
Views: 2007
Reputation: 4754
Here's a stab at what I think you're trying to achieve. First: I'm assuming the issue isn't autocomplete/add to list, but rather what to do with the list in the MVC side. Let's say your command object has a property "employee names," defined as
List<String> getNames(){..}
void setNames(List<String>){..}
On the JSP side, you define the form list items like so:
<form:form>
<c:forEach items="${command.names}" var="name" varStatus="status">
<form:input path="names[${status.index}]" />
</c:forEach>
</form:form>
The real trick to making it "dynamic" with jQuery is to add to the form with the next increasing index. So somewhere you have:
<script type="text/javascript">
var count = ${fn:length(command.names)};
function addToList()
{
// add to form with name to "names[count]"
count++;
}
</script>
Putting it all together, you set the list in the controller formBackingObject to an AutoPopulatingList
That should be enough to get you started.
Upvotes: 1