2013-10-18 58 views
0

我想顯示供應商名稱,而用戶點擊文本框。爲此,我使用jquery.autocomplete.js。我下面this tutorial自動完成不調用動作類

我的代碼是在JSP

<head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
     <link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" /> 
     <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" /> 
     <script src="js/jquery.autocomplete.js"></script> 
     <script src="js/jquery-1.9.1.js"></script> 
     <script src="js/jquery-ui.js"></script> 

     <style> 
      input { 
       font-size: 120%; 
      } 
     </style> 
    </head> 
<body> 
<span class="label">Supplier Name</span> 
    <span class="ib"> <input type="text" name="supplierId" id="supplierId"/></span> 
    <script> 
$("#supplierId").autocomplete("autoCompleteSupplier"); 
</script> 
</body> 
</html> 

在struts.xml中

<action name="autoCompleteSupplier" class="iland.supplier.SupplierAction" method="autoComplete"> 
      <result name="success">/pages/supplier/suggestion.jsp</result> 
      <result name="input">/pages/supplier/suggestion.jsp</result> 
      <result name="login">/pages/login.jsp</result> 
     </action> 

在動作類

public class SupplierAction extends ActionSupport { 
private String q;//getter and setter method 

public String autoComplete() { 

      System.out.println("->SupplierAction autoComplete()"); 
      System.out.println(getQ()); 
      SupplierBusiness cb = new SupplierBusiness(); 
      Map data = cb.autoComplete(getQ()); 
       setSupplierList((ArrayList) data.get("supplierList")); 
       return SUCCESS; 
    } 
    } 

的autocompte應顯示如下的JSP頁面

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@taglib uri="/struts-tags" prefix="s" %> 
<s:iterator value="supplierList" var="suplst"> 
    <s:property value="supplierId"/> 
    <s:property value="supplierName"/> 
</s:iterator> 

雖然與克羅姆檢查它顯示了以下錯誤

Uncaught SyntaxError: Unexpected end of input jquery.autocomplete.js:462 
Uncaught Error: cannot call methods on autocomplete prior to initialization; 
attempted to call method 'autoCompleteSupplier' jquery-1.9.1.js:507 

回答

0

兩件事情:

  1. 將調用autocomplete在DOM就緒功能,例如,在HTML結束。使用source屬性設置操作。使用完整的網址,例如從<s:url>標籤。
<script> 
    $(function() { 
    source: "<s:url action='autoCompleteSupplier'/>" 
    }); 
</script> 

請注意,此功能在Struts 2 jQuery plugin包裹起來。