How to parse arrays of objects returned by DWR?

DWR handles lists of primitives in a fairly straightforward manner. I couldn't find if the array of objects returned by the DWR method call represents a JSON object. Any hints? Or do I need to return a JSON string representing an array of objects back to the browser?

+1


source to share


1 answer


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

Good news: DWR also handles Java arrays and collections in the most direct way. Just return them, and on the client side, you get JavaScript Array objects. (In typical cases, such as primitives or strings within your array or collection, that is. If the content is anything more exotic, you may need to define converters in more detail below.)

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

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

  • All primitive types, boolean, int, double, etc.
  • Based on class version Boolean, Integer, etc.
  • java.lang.String
  • java.util.Date and 3 SQL derivatives
  • arrays above
  • Collections (lists, sets, maps, iterators, etc.) above.
  • DOM objects (like element and document) from DOM, XOM, JDOM, and DOM4J

So you definitely won't need JSON strings for them (although it might be a good option for more complex data structures).



In fact, you can return many more kinds of objects without doing a lot of manual work, because DWR comes with converters "for many typical uses. For example, to make your custom Java" beans "work on the JS client side, all you need to say in dwr.xml

, is that you want to use a bean :

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

      

Even if your method returns a list (or array) of these bean objects ...

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

      

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

+6


source







All Articles