简体   繁体   English

如何从javascript中的动作类获取ArrayList数据?

[英]How to get ArrayList data from action class inside a javascript?

I'm gong to making an autocompleter using jquery autocompleter. 我很乐意使用jquery autocompleter创建一个自动完成器。 In my case I need to load some data from a method. 就我而言,我需要从方法中加载一些数据。 That method(return a list) has a parameter and I need to pass the textfield input as the method argument. 该方法(返回列表)具有一个参数,我需要将文本字段输入作为方法参数传递。 Is this possible? 这可能吗? If it is how can I do this? 如果是我该怎么办?

Method is, 方法是

public List<Item> getSuggestedData(String def) {
    EntityManager em = getEntityManager();
    try {
        Query q = em.createQuery("select o from Item o WHERE o.itemName like :def");
        q.setParameter("def", def + "%");
        return q.getResultList();
    } finally {
        em.close();
    }
}

index.jsp, index.jsp,

<script>
     $(function() {             

         var availableTags = [/*I need to load data to here*/];
         $( "#tags" ).autocomplete({                         
            source: availableTags
         });
     });
</script>
<div class="ui-widget">            
   <s:textfield id="tags"/> 
</div>

jQuery is a client side script and java code is on server side. jQuery是客户端脚本,而Java代码在服务器端。 You need to send a HTTP request from client to the server to get your list of tags. 您需要从客户端向服务器发送HTTP请求以获取标签列表。 You can do this by AJAX. 您可以通过AJAX执行此操作。 jQuery has a good support for AJAX. jQuery对AJAX有很好的支持。

try this 尝试这个

$.ajax({
  async:settings.Async,url:Url,cache:false,type:'POST',data:$("#tags").val()
 }).done(function (result) {
     $( "#tags" ).autocomplete({                         
        source: result
     });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM