简体   繁体   中英

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

I'm gong to making an autocompleter using 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,

<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. You need to send a HTTP request from client to the server to get your list of tags. You can do this by AJAX. jQuery has a good support for AJAX.

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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