Question

DWR handles lists of primitives quite straight forward. I could not find whether array of objects returned by a DWR method call represent a JSON object. Any clues? Or do I have to return a JSON string representing the array of objects back to the browser?

Was it helpful?

Solution

This answer is a little late, but here goes :)

Good news: DWR also handles Java arrays and Collections in a really straight-forward way. Just return them and on client side you'll get JavaScript Array objects. (In typical cases like primitives or Strings inside your array or Collection, that is. If the contents is something more exotic, you may need to define converters; more below.)

Here's a quote from DWR documentation (emphasis mine):

By default all of the following are converted for you without further declaration:

  • All primitive types, boolean, int, double, etc.
  • The Class based versions of the these Boolean, Integer, etc.
  • java.lang.String
  • java.util.Date and the 3 SQL derivatives
  • arrays of the above
  • Collections (Lists, Sets, Maps, Iterators, etc) of the above
  • DOM objects (like Element and Document) from DOM, XOM, JDOM and DOM4J

So you definitely won't need JSON strings for these (although that may be a good option for more complicated data structures).

You can actually return many more kinds of objects without doing a lot of manual work because DWR comes with "converters" for lots of typical uses. For example, to make your custom "bean" style Java objects work in client-side JS, all you need to say in dwr.xml is that you want to use the bean converter:

<convert converter="bean" match="com.company.YourBean" />

Even if your method returns a List (or array) of those bean objects...

public static List<YourBean> getData(){ ... }

... the above configuration suffices, which is pretty nice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top