Struts2 Autocompleter - 单击提交按钮时输入丢失

时间:2014-09-22 19:23:22

标签: javascript ajax apache struts2 autocomplete

我用:

http://struts.apache.org/release/2.1.x/docs/autocompleter.html

问题是:

输入时:

“球”

自动填充查找“气球”

在文本区域中我看到“气球”,在下面的列表中也是“气球”。当我单击显示器上的任何其他位置时,它会在文本字段中显示“气球”。但是当我点击提交时,我会从文本字段中读取“Ball”。为什么?怎么样?

问候, Rhodarus

1 个答案:

答案 0 :(得分:2)

首先,不要再使用Struts Ajax标记,because

  

不推荐使用Dojo插件

     

在Struts 2.1

上将弃用Dojo插件

如果您现在就开始使用,可以使用raw Dojo(今天在1.10版本,而不是Struts2附带的旧版0.4.x版本;但它可能会更容易迁移到jQuery,特别是在Struts2-jQuery-plugin的帮助下。

您可以在 Widgets 菜单下看到其自动填充器的示例。

顺便说一下,如果由于某种原因你想要坚持使用旧的集成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,如(text is“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}" />