1

I'm working on a Spring MVC app, which should let user alter contents of a list.

The list consists of Book-objects with simple properties like name and author. The view is a JSP page that displays the list of books and lets the user alter the contents.

Altering the list can mean adding books, removing them or changing the order of the books in the list.

Question is, how do I get the altered list back to the server? I can write JavaScript to control the list, but how do I post it to the Spring controller? On the other hand I can write a JSP form for altering the model which would be trivial to submit back to the server, but am I then limited to basic text fields in form input?

EDIT: In JSP it is very easy to alter a single model's properties using a form like

<form:form action="myaction" method="post" commandName="mybook">

but if your model is a (ordered) list of objects then how do you edit it?

In Javascript I can get the list of objects from the response and change it as needed, but how do I submit it back to the server? Something like

$.post("/modifybook.do",{ name: "Spring in Action", author: "Graig Walls" } );

works, but only for single objects.

2
  • There's any number of ways this could be handled. JSP itself places no limitations on your options, but using Spring form tags might--but those end up being plain HTML anyway, and can be manipulated the same way anything else could. Commented Jun 14, 2012 at 12:37
  • Can you post an example of your JSP? Commented Jun 14, 2012 at 12:38

2 Answers 2

1

Having never used Spring this may well not be appropriate but could you not convert the list into XML and then post that back to Java and then work on it retaining all intricacies and changes?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I already tried JSON and I was posting about it earlier: stackoverflow.com/questions/11015611/… The problem is submitting it back to Spring, I got it working for single objects but not for arrays
1

You should avoid manipulating the whole list, period. I can't see a scenario where populating entire list of items and sending it back to the server is desired, when only one list element is being changed (edited, added or removed).

What I usually do in my app is I create a handler (controller) for returning a whole list of objects in one go and then add another handlers for adding, editing and removing single entries within that list. I also try to stick to REST in such scenarios, so that I have a clean API which represents server resources and the frontend (AJAX + jQuery) makes use of it.

This solution works really well for me so I suggest sticking to it as well.

1 Comment

Thanks for the reply, your strategy sounds great but I'm not sure if it applies to every situation: What if the list is ordered? Then adding or deleting (or just changing) on element will affect all the elements? See my earlier post for an example: stackoverflow.com/questions/11008876/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.