addOptions problem in DWR

      6 Comments on addOptions problem in DWR

Have you ever wondered why the addOptions is not working in DWR even after reading the specs carefully. It happened to me recently. First, of all, the API or specs or documentation for DWR(addOptions Documentation) are not detailed and sufficient. Especially, for the new and impatient learners like me, who don’t want to go through each and every page of the spec before using the technology. Second, if you are impatient and new learner then probably you might have missed some of the part. Problem: I was trying to populate the Select box from a list of Objects using the addOptions method of DWR. I have followed the documentation fully but my drop down was not being populated. (It was my second day with DWR so I didn’t know some of the complexities which ultimately solved the problem)

Solution:- I will take one example to explain this Let your select field something like this

<select id="statesList" />

And the class whose list of Objects you are trying to add is something like this

package testPackage.dwr;
public class State{
private String stateId;
private String stateName;
//getter and setter methods

and you want to populate this using DWR after some user action. Make sure that your callback function looks like this

var setValuesInDWR = function(data){
var fieldId = 'statesList';
var valueText = 'stateId';
var nameText = 'stateName';
dwr.util.removeAllOptions(fieldId);// step 1
dwr.util.addOptions(fieldId, data, valueText, nameText);//step 2

All the above steps are important

Step 1 – Actually removes the already existing values from your select list. If you don’t do this. the addOptions method will simply append the list.

Step 2 – This will actually add the values to the drop-down list.

I forgot/missed/didn’t know that you have to add the following line in dwr.xml

<convert converter=”bean” match=”testPackage.dwr.State”/>

Reason is, unless you don’t explicitly specify in your dwr.xml the user defined beans will not be converted in DWR. If your bean consists of supported datatypes which DWR can automatically convert then you can just add this declaration. After following all these steps carefully the problem of Dropdown not getting populated in DWR should be solved.

6 thoughts on “addOptions problem in DWR

  1. Tobbi


    I have the same problem using addOptions. I have a java class containing a function that returns array of strings. I have no getter and setter methods. Is it possible for me to separate from this array the values and the texts that I will use in the select field? Do you have some java examples? In your example you initialized valueText to ‘stateId’ and nameText to ‘stateName’ and then you use it in addOptions. Why ‘stateId’ and ‘stateName’? Do they have to be the same as the java variables?
    Thank you very much.

  2. Paras

    Yes we have to use exactly the same name as the Java variables. Then only DWR would be able to know the getter and setter methods. For instance, in my example DWR will call getStateName() and setStateName(..) for the Name of the drop downs.
    The getter and setter methods are required only for the Array of objects. For array of Strings you have to use.

    For example,
    your method which returns string value is getStringArray()class ExampleClass. And the id of the dropdown in JSP is ‘dropDownId’. Your code will be something like this


    function setDropDownFunction(data){
    dwr.util.addOptions(‘dropDownId’, data);


    I have never tried this in my code. You try this and let me know the results

  3. Luke

    Thanks for this! I have been trying to get a DWR example to work for hours, your example finally got everything working for me.

  4. Pete van de Water

    Excellent post. I had exactly the same problem as you – didn’t know about adding the entry in the dwr.xml. The joys of picking a technology up by tweaking other’s existing code 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *