首先,不要再使用Struts Ajax标签了,because:
Dojo 插件已弃用
Dojo 插件将在 Struts 2.1 上被弃用
如果您现在开始,您可以使用raw Dojo(今天是1.10 版本,而不是Struts2 附带的旧的、有缺陷的0.4.x 版本;但迁移到jQuery 可能会更容易,尤其是iwth Struts2-jQuery-plugin的帮助。
您可以在 Widgets 菜单下查看其 Autocompleter 的示例。
顺便说一句,如果出于某种原因您想坚持使用旧的集成 Dojo 版本(例如,在处理一个巨大的遗留项目时,只是为了进行最小的修正),请确保您正确使用它:返回一个有效的 Json 对象,发布和订阅正确的主题等,如the documentation的示例所示:
<sx:autocompleter name="autocompleter1" href="%{jsonList}"/>
<s:autocompleter name="test" list="{'apple','banana','grape','pear'}" autoComplete="false"/>
<sx:autocompleter name="mvc" href="%{jsonList}" loadOnTextChange="true" loadMinimumCount="3"/>
在自动完成器上输入的文本作为参数传递给“href”中指定的 url,如(文本为“struts”):http://host/example/myaction.do?mvc=struts
<form id="selectForm">
<sx:autocompleter name="select" list="{'fruits','colors'}" valueNotifyTopics="/changed" />
</form>
<sx:autocompleter href="%{jsonList}" formId="selectForm" listenTopics="/changed"/>
<sx:autocompleter href="%{jsonList}" id="auto"/>
<script type="text/javascript">
function getValues() {
var autoCompleter = dojo.widget.byId("auto");
//key (in the states example above, "AL")
var key = autoCompleter.getSelectedKey();
alert(key);
//value (in the states example above, "Alabama")
var value = autoCompleter.getSelectedValue();
alert(value);
//text currently on the textbox (anything the user typed)
var text = autoCompleter.getText();
alert(text);
}
function setValues() {
var autoCompleter = dojo.widget.byId("auto");
//key (key will be set to "AL" and value to "Alabama")
autoCompleter.setSelectedKey("AL");
//value (key will be set to "AL" and value to "Alabama")
autoCompleter.setAllValues("AL", "Alabama");
}
</script>
<script type="text/javascript">
dojo.event.topic.subscribe("/before", function(event, widget){
alert('inside a topic event. before request');
//event: set event.cancel = true, to cancel request
//widget: widget that published the topic
});
</script>
<sx:autocompleter beforeNotifyTopics="/before" href="%{#ajaxTest} />
<script type="text/javascript">
dojo.event.topic.subscribe("/after", function(data, request, widget){
alert('inside a topic event. after request');
//data : JavaScript object from parsing response
//request: XMLHttpRequest object
//widget: widget that published the topic
});
</script>
<sx:autocompleter afterNotifyTopics="/after" href="%{#ajaxTest}" />
<script type="text/javascript">
dojo.event.topic.subscribe("/error", function(error, request, widget){
alert('inside a topic event. on error');
//error : error object (error.message has the error message)
//request: XMLHttpRequest object
//widget: widget that published the topic
});
</script>
<sx:autocompleter errorNotifyTopics="/error" href="%{#ajaxTest}" />
<script type="text/javascript">
dojo.event.topic.subscribe("/value", function(value, key, text, widget){
alert('inside a topic event. after value changed');
//value : selected value (like "Florida" in example above)
//key: selected key (like "FL" in example above)
//text: text typed into textbox
//widget: widget that published the topic
});
</script>
<sx:autocompleter valueNotifyTopics="/value" href="%{#ajaxTest}" />