2013-01-04 59 views
3

我已经为我的Struts 2应用程序使用了Struts2 jQuery autocompleterStruts2 jQuery Autocompleter与选择框

这里是我的代码:

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类:

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 

列表类:

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

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

但这Struts2的jQuery的autocompleter不工作。它不会填充任何值。

回答

1

做的一个

<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"/> 

取而代之的是

<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" /> 

并确保您从动作类返回列表。 要检查这一点,请使用IDE调试器或System.out.print等进行检查

ex... 


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

而且还你应该在你的动作类

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

public void setItemList(List itemList) { 
    this.itemList = itemList; 
} 
+0

真棒..真棒它使用JSON !!!,实际上我对JQuery很新,真的非常感谢您的支持 – edaklij

1

这是错误的:

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

你是喂养的autocompleter与地图,而不是与自己建立了一个自定义对象。

HashMap没有任何name也不id领域,而是有key S和value S场。

开始改变这一点,看看它是否工作:

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

OK定义的getter &制定者谢谢你。我已经编辑我的问题。现在我用简单列表替换地图。但它仍然不起作用。你可以帮我吗? – edaklij

1

您键入未被引用错误的属性。

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

应该

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

使用列表项bean类

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; 
} 

假设,构造和修改器已被添加。

+0

现在我改变了..但仍然不起作用。 – edaklij

+0

什么不是工作主题? –

+0

Struts2-Jquery autocompleter没有填充任何值,它显示的就像是一个文本框。它没有下拉functionalites.i使用struts2-jquery-tree-plugin-3.5.0.jar和struts2-json-plugin -2.3.7.jar插件。 – edaklij