It is highly recommended to avoid using Struts Ajax tags moving forward, as there are valid reasons for this change:
The Dojo plugin is now deprecated
Starting from Struts 2.1, the usage of the Dojo plugin will be deprecated.
If you are beginning now, consider utilizing raw Dojo (currently at version 1.10
) instead of the outdated 0.4.x
version bundled with Struts2. Alternatively, migrating to jQuery, particularly with the assistance of Struts2-jQuery-plugin, may prove to be a smoother transition.
An example of the Autocompleter functionality can be found under the Widgets menu.
If, for some specific reason, you prefer to continue using the integrated Dojo version (e.g., in scenarios where minor corrections are needed in a large legacy project), ensure its correct implementation: return a valid Json object, subscribe and publish to the appropriate topics, etc., as detailed in 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"/>
The entered text on the autocompleter becomes a parameter passed to the specified url in "href", as demonstrated by (if "struts" is the text):
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}" />