autocompleter与动态列表:使用SJ自动完成的文本框 [英] Autocomplete a textbox using sj:autocompleter with a dynamic list
问题描述
我打算显示的建议列表,使用 SJ一个文本框:autocompleter
。当我辛苦code在jsp它工作正常的数据。
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>
在我的动作类,
public String execute() {
fruitslist= new ArrayList<String>();
fruitslist.add("Apple");
fruitslist.add("Banana");
fruitslist.add("Orange");
fruitslist.add("Apricot");
}
请帮忙。
推荐答案
确保您有一个getter:
Ensure you have a getter:
public List<String> getFruitslist() {
return fruitslist;
}
既然你发布你的struts.xml,现在要使用JSON的清晰,code必须改变。在previous答案是指从主要工作的标准阵列;在JSON动作的情况下,你需要指定在autocompleter的的href
属性的URL指向单独行动JSON:
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"
/>
然后,你需要设置你的JSON作为结果,你的根
对象阵列,像这样的:
<action name="ajaxAction" class="org.struts.action.AjaxJsonAction">
<result name="success" type="json">
<param name="root">
fruitslist
</param>
</result>
</action>
我强烈建议您阅读 Struts2的JSON的插件是如何工作的。
这篇关于autocompleter与动态列表:使用SJ自动完成的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!