using Vector and Hashtable as a bean for dwr
-
28-09-2019 - |
Question
I want to write a DWR function that prepares a Vector of Hashtable's and returns to the javascript and there in javascript i need to iterate a Vector and print the content of Hashtable.
My DWR function is:
public Vector getEmployeeData() {
Vector employeeData = new Vector();
// some other content to prepare vector
return employeeData;
}
And i have configured dwr-servlet in web.xml properly and content in dwr.xml is:
<create creator="new" javascript="Employee" scope="script">
<param name="class" value="com.test.DWREmployeeManager"/>
</create>
<convert match="java.util.Hashtable" converter="bean"/>
<convert match="java.util.Vector" converter="bean"/>
But my problem is when i called this dwr method from javascrit i am getting empty array as a result to the dwr call-back function.
Can anybody suggest the solution for this problem..
Solution 2
Instead of using the two tags,
<convert match="java.util.Hashtable" converter="bean"/>
<convert match="java.util.Vector" converter="bean"/>
I used the below convert tag:
<convert converter="collection" match="java.util.Collection"/>
Now its working fine.
Thanx
OTHER TIPS
I suspect your problem here is that you're telling DWR to convert the Collection objects as beans. You shouldn't need to do this, nor do you want to -- they're not beans, they're collections.
See: http://directwebremoting.org/dwr/server/dwrxml/converters/collection.html
Moreover, as in my comment above, you probably want to use a List (Array or Linked), and a HashMap. If you're using Java 5 or newer, you probably should add type declarations.
Here's what I suggest: (You didn't specifically show your hashtable code, so I'm just assuming a map keyed off some string.)
public List<Map<String,EmployeeData>> getEmployeeData() {
List<Map<String,EmployeeData>> employeeData = new ArrayList<Map<String,EmployeeData>>();
// some other content to prepare list
return employeeData;
}
In dwr.xml, you need to remove the two converter lines, and add one:
<convert match="com.test.EmployeeData" converter="bean"/>
(and make sure there are proper getter and setter methods for that bean).