views:

240

answers:

3

I have a spring MVC application using JSP as my view technologies with Jquery for AJAX. I have a table such as the following:

<table>
  <tr>
    <td>name1</td>
    <td>value1</td>
    <td>setting1</td>
  </tr>
  <tr>
    <td>name2</td>
    <td>value2</td>
    <td>setting2</td>
  </tr>
</table>

I need to serialize this table so that it can later be bound to an object in my controller. However the jquery serialize() method only works on form fields. What would be the best approach to get the table data into the HTTP request so that I can later bind it to a java object?

EDIT:

I have a java object that has a collection so

class MyOject {
   private List<AnotherObject> items = new ArrayList<AnotherObject>();

   // standard getters and setters
}

class AnotherObject {
   private name;
   private value;
   private setting;

   // getters and setters
}

In the screen the user is creating new items on the fly. When the user is done, they submit the form and then I need to process all the items in the list and instantiate a new collection with those items.

For display purposes I am creating a new table row when an item is created.

+1  A: 

Use the Spring Data Binding and Validation API to bind it into a Java object of your own design. That documentation is web-agnostic; check out the later chapter to see how the web tier leverages it.

You'll want an abstraction beyond a table, I presume.

duffymo
I guess to clarify: how do I get my table information into the http request?
predhme
Put it into form fields. That's how the web works, no sense reinventing the wheel? You could copy it all into the query string too I guess.
Affe
Definatley I wouldn't want to reinvent the wheel. So I guess I can just add form fields inside the <td> with a name="item?" where ? is some kind of counter. So then in the controller I can parse through them all?
predhme
Absolutely - put them in a form. Spring will be binding parameters from the HTTP request. That's an assumption going in.
duffymo
A: 

in order to stick your table information into a java object, you will first need to send it to the server.. for that you will need to either send it via XHR or in a form.

in order to serialize the object you will need to write some javascript/jquery. i could write it for you, but your requirements are somewhat vague when it comes to how your table will look, nor do i want to guess about what the java object you want to add your data to looks like.

mkoryak
I would assume you would use the Jquery.data() for this purpose? I guess I could attach a submit callback Jquery.submit("#myForm", function(){}); to process the items in the table to serialize them and manually add them to the request?
predhme
+1  A: 

The <Form> tag is how you tell the browser "Put this stuff in the web request." That's how you get object binding in Spring. What is your reason for not using a Form? You don't necessarily have to put it in a form in the page, you could give your table elements IDs and fetch their contents in the javascript if you really needed to.

Edit: I think maybe it's hard to answer because it's not clear why you want the browser to give you back things that you gave it in the first place. Maybe what you really need is the @SessionAttributes() annotation on your controller so that you can preserve State of the original page shown to the user?

More Edit:

kk, see now. If what you want is Spring web data binding then create a form in parallel as you add more table rows. e.g.,

<form id="myObject" action="whateverYouNeedHere.htm" method="post">
<input type="hidden" id="items[0].name" name="items[0].name" value="foo"/>
<input type="hidden" id="items[0].value" name="items[0].value" value="bar"/>
<input type="hidden" id="items[0].setting" name="items[0].setting" value="buzz"/>
<input type="hidden" id="items[1].name" name="items[1].name" value="foo"/>
<input type="hidden" id="items[1].value" name="items[1].value" value="bar"/>
....

Then just submit that and it will bind right on for you. If you did mean to handle the content yourself, then you probably could use XHR as someone else mentioned.

Affe
It doesn't have to be a table per sai, however, in the current screen they are adding items to the screen "ajaxly". Then when they submit, the new items need to be added to a collection in the java object. object.getCollection.add(item) i.e.
predhme
Well I am not sending the same information back to the server that was given to the browser in the first place. In fact, the user is creating a new MyObject with a collection of AnotherObject (see above edits). I need to send the "list" (represented in the html table) in the request to bind to a MyObject.
predhme
Ahh perfect. Thanks a lot.
predhme