2013-01-04 62 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