1

I am intending to display a list of suggestions for a textbox using sj:autocompleter. When I hard code the data in the jsp it works fine.

<sj:autocompleter name="fruitNames" 
                  list="{'apple', 'banana', 'orange', 'apricot'}" 
                  label="Fruit Names">
</sj:autocompleter>

But I want to get the list of suggestions dynamically from the action class. I tried doing this but it is not getting the values.

<sj:autocompleter name="fruitNames" list="fruitslist"
     label="Fruit Names">
</sj:autocompleter>

In my action class,

public String execute() {
    fruitslist= new ArrayList<String>();
    fruitslist.add("Apple");
    fruitslist.add("Banana");
    fruitslist.add("Orange");
    fruitslist.add("Apricot");
}

Please help.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
AFW
  • 75
  • 1
  • 9

1 Answers1

1

Ensure you have a getter:

    public List<String> getFruitslist() {
        return fruitslist;
    }

Since you posted your struts.xml and now it's clear that you want to use JSON, the code must be changed. The previous answer was referring to a standard Array from the main Action; in case of a JSON action, you need to specify an url in href attribute of the autocompleter to point to the separate JSON Action:

    <s:url var="remoteurl" action="ajaxAction"/>
    <sj:autocompleter
        id="fruitslist"
        href="%{remoteurl}"
        delay="50"
        loadMinimumCount="2"
    />

Then you need to set your result as JSON, and your root object to your Array, like this:

    <action name="ajaxAction" class="org.struts.action.AjaxJsonAction"> 
        <result name="success" type="json">
            <param name="root">
                fruitslist
            </param>
        </result> 
    </action>

I strongly suggest you to read how the Struts2-JSON plugin works.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I tried doing this but still doesn't work. I don't know if I am missing something. Is there any other configuration you need to do in the struts.xml. – AFW Oct 23 '14 at 19:25
  • textboxAjax.jsp – AFW Oct 23 '14 at 20:09
  • Sorry the formatting is lost when I pasted the contents of the file. – AFW Oct 23 '14 at 20:11
  • Answer edited. Please, when you need to add code, edit your question instead of posting it into comments – Andrea Ligios Oct 23 '14 at 21:40
  • Thanks Andrea. That worked. Also, thanks for providing the link for a deeper explanation to Struts2-JSON. – AFW Oct 24 '14 at 16:03
  • Any reason to use array instead of list? – Aleksandr M Oct 24 '14 at 21:34
  • In the above example, if I do not want to use JSON how do I simply display the fruitslist in the web page? For example, if the criteria is "tropical", in my action class I need to query the database and return the list of tropical fruits to a different textbox in the jsp. One textbox to enter the criteria and the second to autocomplete based on the criteria entered in the first textbox. I don't have to use JSON to accomplish this. Correct? – AFW Nov 04 '14 at 19:35
  • Use an autocompleter. With json :) – Andrea Ligios Nov 04 '14 at 23:15