jquery入力ボックスブラーマッチング


備考説明:①index.jspはホームページ面で、入力を提供する
②getdata.jspは業務処理部であり、actionまたはservlet形式に置き換えることができる
③DummyDB.javaはデータマッチング処理クラス
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
    <link href="jquery.autocomplete.css" type=text/css rel=stylesheet>
    <script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="jquery.autocomplete.js"></script>
	<style>  
         input {   
             font-size: 120%;   
        }   
    </style>  
  </head>
  
  <body>
     <h3>Country</h3>  
     <input type="text" id="country" name="country"/>  
  
.     <script>  
        $("#country").autocomplete("getdata.jsp");   
     </script> 
  </body>
</html>

 
 
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.hzp.DummyDB"%>
<%
	DummyDB db = new DummyDB();

	String query = request.getParameter("q");

	List<String> countries = db.getData(query);

	Iterator<String> iterator = countries.iterator();
	while (iterator.hasNext()) {
		String country = (String) iterator.next();
		out.println(country);
	}
%>

 
 
 
 
package com.hzp;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class DummyDB {
	private int totalCountries;
	private String data = "Afghanistan, Albania, bmw, Zimbabwe";
	private List<String> countries;

	public DummyDB() {
		countries = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(data, ",");

		while (st.hasMoreTokens()) {
			countries.add(st.nextToken().trim());
		}
		totalCountries = countries.size();
	}

	public List<String> getData(String query) {
		String country = null;
		query = query.toLowerCase();
		List<String> matched = new ArrayList<String>();
		for (int i = 0; i < totalCountries; i++) {
			country = countries.get(i).toLowerCase();
			if (country.startsWith(query)) {
				matched.add(countries.get(i));
			}
		}
		return matched;
	}
}