jquery-ui-custom autocomplete

jquery-ui-custom autocomplete

 //jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">
 
        <title>autocomplete</title>
 
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page"> 
  
        <link href="<%=basePath%>js/autocomplete/jquery-ui-1.10.3.custom.css" rel="stylesheet">
        <script src="<%=basePath%>js/jquery-1.7.2.min.js"></script>
        <script src="<%=basePath%>js/autocomplete/jquery-ui-1.10.3.custom.js"></script>
    <script>
        $(function(){
            $( "#autocomplete" ).autocomplete({
                minLength: 1,//最少输入1个字符才执行
                source: 'autocomplete.action'
            });
        })
    </script>
    </head>
    <style>
        body {
            font: 62.5% "Trebuchet MS", sans-serif;
            margin: 50px;
        }
    </style>
    <body>
        <h2 style="margin-top: 2em;">
            Autocomplete
        </h2>
        <div>
            <input id="autocomplete" title="type &quot;a&quot;">
        </div>
    </body>
</html>  
//struts.xml
<action name="autocomplete" class="com.test.action.testAction" method="autocomplete"></action> 

//java
public String autocomplete() throws Exception{
         
        String[] temp = "align,both,card,dream,error,fail,gson,hello,invaid,job,kill".split(",");
        String name = "[";
        for(int i=0;i<temp.length;i++){
            if(temp[i].indexOf(term)!=-1){
                name += "\""+temp[i]+"\",";
            }
        }
        if(name.length() > 1)
            name = name.substring(0,name.length()-1);
        name+="]";
         
        System.out.println(name);
         
        HttpServletResponse res = ServletActionContext.getResponse();
        PrintWriter pw = res.getWriter();
        pw.write(name);
        pw.flush();
        pw.close();
         
        return null;
    } 

你可能感兴趣的:(autocomplete)