Sending an array to a server-side object

3 views
Skip to first unread message

javarunner

unread,
May 22, 2008, 1:41:07 PM5/22/08
to jabsorb-user
I have a simple array that is set up like this:

var calOptions = {};
calOptions.values = {
"location" : "value1",
"dept" : "value2",
"type" : "value3",
"resc" : "value4"
}

I was thinking about sending it like this:

jsonrpc.objectnameref.getOptions(callback, calOptions);

Is this the optimal way to do this? If not, how should I do it?
Also, how do I process this in the server-side object - as a String
array, or as a Java collection in which I have a list of 'key/value'
pairs.
Thx...
For background I cannot send this as a form submission.

Arthur Blake

unread,
May 22, 2008, 9:13:45 PM5/22/08
to jabsorb-user
Hello, javarunner. There are different ways to accomplish this. But
in general, if you expect the object (calOptions) to serialize into a
valid Java object on the server side, you need to make sure that the
type of class set up to receive the object on the Java side has all
the fields that the client object will be sending. Just to follow
your example, if your server side method was defined with a method
like this:

public void getOptions(MyOptions options)

you would need to define the MyOptions class like this:

public class MyOptions
{
public MyOptions() {}
private MyValues values;
public getValues() {
return values;
}
public setValues(MyValues values) {
this.values = values;
}
}

and the MyValues class like this:

public class MyValues
{
public MyValues() {}
private String location;
private String dept;
private String type;
private String resc;

// include public getters and setters for above 4 fields here
....
}

Now this is just one way to do it. Whether it's the best way or not
depends on what you need to do. You could also just take the easy way
out and instead define your Java side method like this:

public void getOptions(JSONObject options)

In this case, your object would get serialized directly into a
JSONObject on the server side which has methods for getting at the
data in the JSONObject. The advantage of this, is that you can send
any kind of data and not be locked into only the fields that your Java
objects define.

You can also use Maps, Sets and Lists... If you want to use these, you
have to structure your data in the way that Jabsorb is expecting for
those types. See the manual (http://jabsorb.org/Manual) section 3.1.2
for more information about this.

Good Luck on getting it working.

javarunner

unread,
May 22, 2008, 9:37:04 PM5/22/08
to jabsorb-user
Thanks for the advice. I've been working on delivering a Collection
to the servlet/POJO and that is what I think I'll do.
Now the next problem is the reverse. Building an object on the server
side and processing within my jsp.

javarunner

unread,
May 23, 2008, 2:56:33 PM5/23/08
to jabsorb-user
This is good stuff. Now, I have retrieved DB content and assigned the
data to a holder class that is encapsulated in an ArrayList. In other
words my arraylist contains instances of my holder class.
In my JSP I call an accessor method that returns this ArrayList. I
assign it to a JS Array initialized to, say, 5 empty elements.
The size of this array is 5 (obviously) begore the call, but after
it's undefined.
How should I deal with this ArrayList on the client side, or should I
perhaps build an XML (DOM) string and return that to the client?
What do you think?

javarunner

unread,
May 23, 2008, 7:36:13 PM5/23/08
to jabsorb-user
I decided for expediency to go with the XML approach; as a result
everything's functional.
Appreciated dialoging with you.
Keep up the good efforts.
Reply all
Reply to author
Forward
0 new messages