3

I've used the Struts2 jQuery autocompleter for my Struts 2 application.

Here is my code:

JSP:

 <s:form id="frm_demo" theme="simple" action="ManagersAutoCompleter1">
        <s:url var="remoteurl" action="test" />
    <sj:autocompleter href="%{remoteurl}" id="echo3" name="echo"
        list="itemList" listKey="id" listValue="name" emptyOption="true"
        headerKey="-1" headerValue="Please Select a Language" selectBox="true" />

        <s:submit value="submit" />
    </s:form>

Struts.xml:

<action name="test" class="test.TestAction" method="populate">
  <result type="json">
  </result>
</action>

Action Class:

 public String populate() throws Exception {

        itemList = new ArrayList<ListValue>();
        itemList.add(new ListValue("Php", "Php"));
        itemList.add(new ListValue("Java", "Java"));
        itemList.add(new ListValue("Mysl", "Mysl"));
        return SUCCESS;
    } //getter setter for itemList

List Class:

public class ListValue {
    private String id;
    private String name;

    public ListValue(String id, String name) {
        this.id = id;
        this.name = name;
    } //getter setter methods

But this Struts2 jQuery autocompleter is not working. It doesn't populate any values.

Roman C
  • 49,761
  • 33
  • 66
  • 176
edaklij
  • 4,121
  • 11
  • 31
  • 43

3 Answers3

1

This is wrong:

<sj:autocompleter href="%{remoteurl}" id="lst" name="lst"
    list="itemList" listValue="name" listKey="id" selectBox="true" />

You are feeding the autocompleter with a Map, not with an custom object built by yourself.

An HashMap does not have any name nor id fields, instead it have keys and values fields.

Start by changing that and see if it works:

<sj:autocompleter href="%{remoteurl}" id="lst" name="lst"
    list="itemList" listValue="value" listKey="key" selectBox="true" />
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Ok thank you.I have edited my question. Now I am replace Map with Simple List. But It still Doesn't work. can u plz help me? – edaklij Jan 04 '13 at 09:57
1

You have typed wrong attribute that is not referenced.

<s:url id="remoteurl" action="test" />

should be

 <s:url var="remoteurl" action="test" />

Use the list item bean class

public class ListValue {
  private String id;
  private String name;
...
}

public String populate() throws Exception {
  itemList.add(new ListValue("Php", "Php"));
  itemList.add(new ListValue("Java","Java") );
  itemList.add(new ListValue("Mysl", "Mysl") );
  return SUCCESS;
}

assumed that constructor and mutators have been added.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The Struts2-Jquery autocompleter doesn't populate any values and it shows just like a text box.it does not have a dropdown functionalites.i used struts2-jquery-tree-plugin-3.5.0.jar and struts2-json-plugin-2.3.7.jar plugins. – edaklij Jan 04 '13 at 11:32
  • did you included json-plugin? – Roman C Jan 04 '13 at 11:36
  • But your list item doesn't have id, nor name attributes. – Roman C Jan 04 '13 at 11:43
  • I've used LabelValueBean for that purposes to render lists. – Roman C Jan 04 '13 at 11:51
  • LabelValueBean you could find in Apache projects. Check the edit. – Roman C Jan 04 '13 at 12:04
  • can You please check my code? .I have edited as you said,but still its not working. – edaklij Jan 04 '13 at 12:25
  • It doesn't throw any exceptions. The Struts2-Jquery autocompleter doesn't populate any values and it shows just like a text box.it does not have a dropdown icon. – edaklij Jan 04 '13 at 12:34
1

Do this one

<s:url id="remoteurl" action="test"/>
<sj:select 
     id="customersjsonlstid" 
     name="echo"
     label="Handle a List"
     href="%{remoteurl}" 
     list="itemList"
     listValue="name" 
     listKey="id" 
     autocomplete="true"  
     loadMinimumCount="2" 
     id="echo3"/>

Instead of this:

<sj:autocompleter href="%{remoteurl}" id="echo3" name="echo"
list="itemList" listKey="id" listValue="name" emptyOption="true"
headerKey="-1" headerValue="Please Select a Language" selectBox="true" />

And make sure you are returning the list from your action class. To check this, do it with your IDE debugger or System.out.print etc.

ex...


    -------------
    ------------
    itemList.add(new ListValue("Mysl", "Mysl") );
    System.out.println("Size of my list="+itemList.size());
    return SUCCESS;
}

And also you should define getter & setters in your action class

private List itemList; 
    public List getItemList() {
    return itemList;
} 

public void setItemList(List itemList) {
    this.itemList = itemList;
}
lmcanavals
  • 2,339
  • 1
  • 24
  • 35
Dan
  • 2,086
  • 11
  • 71
  • 137
  • awesome..awesome it works using json!!!, actually I'm quite new on JQuery, really thanks for your support – edaklij Jan 08 '13 at 04:18