Struts2的Autocompleter - 点击提交按钮时,输入丢失按钮、Autocompleter

2023-09-11 00:37:15 作者:神秘嘉宾

我使用的:

http://struts.apache.org/release/2.1。 X /文档/ autocompleter.html

和问题是:

Struts2

当键入:

自动完成发现气球

和在文本框我看到气球,并在下面还有气球的名单。当我点击任何地方的监控器上的文本框显示气球。但是,当我点击提交,我从文本框显示球。为什么?而如何?

问候, Rhodarus

解决方案

首先,不使用Struts阿贾克斯标签了,的,因为:

  

道场插件是德precated

     pcated Struts的2.1

道场插件将被取消$ P $

如果你现在开始,您可以使用生道场(今日 1.10 的版本,而不是旧的,车 0.4.x 的版本带有Struts2的,但它可能会更容易迁移到jQuery的,特别是iwth的帮助 Struts2的-的jQuery插件。

您可以看到在其Autocompleter的例子​​中,小工具菜单。

顺便说一句,如果你想与旧集成道场版本的坚持(例如,当工作在一个巨大的和旧的项目只为一个最小校正)某种原因,确保正确使用它:返​​回一个有效的JSON对象,发布和订阅的权利的主题等作为从文档:

 < SX:autocompleter名=autocompleter1的href =%{jsonList}/>



 < S:autocompleter名称=测试列表={苹果,香蕉,葡萄,鸭梨'}自动完成=FALSE/>

 < SX:autocompleter名=MVC的href =%{jsonList}loadOnTextChange =真正的loadMinimumCount =3/>
 

  

进入上autocompleter的文本作为参数传递到的href中指定的URL,像(文本为支柱):的http://host/example/myaction.do? MVC =支柱

 <形式ID =selectForm>
      < SX:autocompleter名=选择列表={'水果','颜色'}valueNotifyTopics =/更改/>
 < /形式GT;
 < SX:autocompleter HREF =%{jsonList}formId =selectFormlistenTopics =/更改/>



 < SX:autocompleter HREF =%{jsonList}ID =自动/>
 <脚本类型=文/ JavaScript的>
   功能的GetValues​​(){
      VAR autoCompleter = dojo.widget.byId(自动);

      //键(在美国上面的例子中,AL)
      VAR键= autoCompleter.getSelectedKey();
      警报(关键);

      //值(在上面的状态例如,亚拉巴马号)
      VAR值= autoCompleter.getSelectedValue();
      警报(值);

      //目前的文本框(任何用户输入)的文本
      VAR文本= autoCompleter.getText();
      警报(文本);
   }

   功能setValues​​方法(){
      VAR autoCompleter = dojo.widget.byId(自动);

      //键(键将被设置为AL和值亚拉巴马)
      autoCompleter.setSelectedKey(AL);

      //值(标号将被设置为AL和值亚拉巴马)
      autoCompleter.setAllValues​​(AL,亚拉巴马号);
   }
 < / SCRIPT>



 <脚本类型=文/ JavaScript的>
 dojo.event.topic.subscribe(/前功能(事件,小部件){
     警报('一个话题事件中之前请求。');
     //事件:设置event.cancel = TRUE,取消请求
     //小部件:小部件发布的话题
 });
 < / SCRIPT>

 < SX:autocompleter beforeNotifyTopics =/前的href =%{#ajaxTest} />



 <脚本类型=文/ JavaScript的>
 dojo.event.topic.subscribe(/后,功能(数据,要求,窗口小部件){
     警报(主题活动后里面的要求。');
     //数据:从的JavaScript解析响应对象
     //要求:XMLHtt prequest对象
     //小部件:小部件发布的话题
 });
 < / SCRIPT>

 < SX:autocompleter afterNotifyTopics =/后的href =%{#ajaxTest}/>



 <脚本类型=文/ JavaScript的>
 dojo.event.topic.subscribe(/错误功能(错误,要求,窗口小部件){
     警报(主题活动上的错误里面。');
     //错误:错误对象(有返回Error.message错误消息)
     //要求:XMLHtt prequest对象
     //小部件:小部件发布的话题
 });
 < / SCRIPT>

 < SX:autocompleter errorNotifyTopics =/错误的href =%{#ajaxTest}/>



 <脚本类型=文/ JavaScript的>
 dojo.event.topic.subscribe(/值功能(值,键,文本,窗口小部件){
     警报(主题活动后里面的价值变化。');
     //值:(例如在上面像佛罗里达)选定的值
     //重点:(例如像FL以上)选择的关键
     //文字:文字输入到文本框
     //小部件:小部件发布的话题
 });
 < / SCRIPT>

 < SX:autocompleter valueNotifyTopics =/值的href =%{#ajaxTest}/>
 

I use:

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

and the problem is:

When typing:

"Ball"

Autocomplete finds "Balloon"

and in the textfield I see "Balloon" and in the list below also "Balloon". When I click anywhere else on the monitor it displays "Balloon" in the textfield. But when I click submit, I reads "Ball" from the textfield. Why? And how?

Greetings, Rhodarus

解决方案

First of all, do not use Struts Ajax tags anymore, because:

Dojo plugin is deprecated

The Dojo plugin will be deprecated on Struts 2.1

If you are starting now, you can use raw Dojo (today at 1.10 version, instead of the old, buggy 0.4.x version shipped with Struts2; but it would probably be easier to migrate to jQuery, especially iwth the help of Struts2-jQuery-plugin.

You can see the example of its Autocompleter under the Widgets menu.

BTW, if for some reason you want to stick with the old integrated Dojo version (for example while working on a huge and legacy project only for a minimal correction), ensure you are using it correctly: return a valid Json object, publish and subscribe the right topics, etc. as shown in the example from 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 text entered on the autocompleter is passed as a parameter to the url specified in "href", like (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}" />