GWT QQ 群号:28310588
功能:
1。当加载页面时把数据库的表读入matchArr
2。每次按键都是matchArr的查询,查询方法用正则表达式
抱歉,没什么注释,给大家添麻烦了
有任何疑问请联系我
qq:259102567
MSN:
jorwen_fang@hotmail.com代码下载我不久前写的关于gwt文章另外在 《ajax in action》书中有提到更佳的算法,提高性能,以及打字过快的处理等功能。
本人这个程序对付300以下数据还是可以忍受等待时间的代码:
1.InputHint.java
package mypack.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
/** *//**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class InputHint implements EntryPoint {
private ArrayList matchArr;
private TextBox tb;
private Label label;
private ListBox list;
public void onModuleLoad() {
label = new Label("输入:");
tb = new TextBox();
IH_Service.Util.getInstance().getResult(
new AsyncCallback() {
public void onSuccess(Object result) {
matchArr = (ArrayList) result;
}
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
}
);
tb.addKeyboardListener(new KeyboardListener() {
public void onKeyDown(Widget sender, char keyCode, int modifiers) {
}
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
fillList(tb.getText());
}
});
tb.addFocusListener(new FocusListener() {
public void onFocus(Widget sender) {
list.clear();
if(tb.getText().equals("")){
for (int i = 0; i < matchArr.size(); i++) {
list.addItem((String) matchArr.get(i));
list.setVisible(true);
}
}else{
fillList(tb.getText());
list.setVisible(true);
}
}
public void onLostFocus(Widget sender) {
list.setVisible(false);
}
});
list = new ListBox();
list.addChangeListener(new ChangeListener() {
public void onChange(Widget sender) {
tb.setText(list.getItemText(list.getSelectedIndex()).trim());
}
});
list.setVisible(false);
list.setVisibleItemCount(10);
RootPanel.get("label").add(label);
RootPanel.get("tb").add(tb);
RootPanel.get("list").add(list);
}
private void fillList(String info) {
IH_Service.Util.getInstance().fillList(info, matchArr,
new AsyncCallback() {
public void onSuccess(Object result) {
ArrayList arr = (ArrayList) result;
list.clear();
if(arr == null || arr.size() == 0){
list.addItem("��ƥ���无匹配字符�");
list.setVisible(true);
}else{
for (int i = 0; i < arr.size(); i++) {
list.addItem((String) arr.get(i));
list.setVisible(true);
}
}
}
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
}
);
}
} 2.IH_Service.java
package mypack.client;
import java.util.ArrayList;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
public interface IH_Service extends RemoteService {
public static final String ENTRY_POINT="/IH_PRO/input_hint";
public static class Util{
public static IH_ServiceAsync getInstance(){
IH_ServiceAsync instance = (IH_ServiceAsync)GWT.create(IH_Service.class);
ServiceDefTarget target = (ServiceDefTarget)instance;
target.setServiceEntryPoint(ENTRY_POINT);
return instance;
}
}
public ArrayList getResult();
public ArrayList fillList(String info, ArrayList matchArr);
}
3.IH_ServiceAsync.java
package mypack.client;
import java.util.ArrayList;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface IH_ServiceAsync {
public void getResult(AsyncCallback callback);
public void fillList(String info, ArrayList matchArr, AsyncCallback callback);
}
4.IH_ServiceImpl.java
package mypack.server;
import java.sql.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mypack.client.IH_Service;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class IH_ServiceImpl extends RemoteServiceServlet implements IH_Service {
private static final long serialVersionUID = 1L;
Connection connection = null;
PreparedStatement pstm = null;
ResultSet rs = null;
public ArrayList getResult(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connection = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ajax",
"sa", "sa");
pstm = connection
.prepareStatement("select name from T_INPUT");
rs = pstm.executeQuery();
ArrayList arr = new ArrayList();
while(rs.next()){
arr.add(rs.getString("name"));
}
return arr;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(rs != null)
rs.close();
if(pstm != null)
pstm.close();
if(connection != null)
connection.close();
} catch (SQLException e) {}
}
return null;
}
public ArrayList fillList(String info, ArrayList matchArr){
Pattern p = Pattern.compile("^"+info+".");
Matcher m=null;
ArrayList arr = new ArrayList();
for(int i = 0; i < matchArr.size(); i++){
m = p.matcher((String)matchArr.get(i));
if(m.find()){
arr.add((String)matchArr.get(i));
}
}
return arr;
}
}
5.mycss.css
.sd{}{
width:150px;
background-color:cccccc;
border:1px solid red;
padding-left:2px;
overflow:visible;
} 6.InputHint.html
<!--<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Wrapper HTML for InputHint</title>
<!-- -->
<!-- Use normal html, such as style -->
<!-- -->
<style>
body,td,a,div,.p{}{font-family:arial,sans-serif}
div,td{}{color:#000000}
a:link,.w,.w a:link{}{color:#0000cc}
a:visited{}{color:#551a8b}
a:active{}{color:#ff0000}
</style>
<!-- -->
<!-- The module reference below is the link -->
<!-- between html and your Web Toolkit module -->
<!-- -->
<meta name='gwt:module' content='mypack.InputHint'>
<link rel='stylesheet' type='text/css' href='mycss.css'/>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic ui -->
<!-- -->
<body>
<!-- -->
<!-- This script is required bootstrap stuff. -->
<!-- You can put it in the HEAD, but startup -->
<!-- is slightly faster if you include it here. -->
<!-- -->
<script language="javascript" src="gwt.js"></script>
<!-- OPTIONAL: include this if you want history support -->
<iframe id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
<h1>InputHint</h1>
<p>
你好,输入前提示演示
</p>
<table align=center>
<tr>
<td id="label"></td><td id="tb"></td>
</tr>
<tr>
<td></td><td id="list" class="sd"></td>
</tr>
</table>
</body>
</html>
7.InputHint.gwt.xml
<module>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Specify the app entry point class. -->
<entry-point class='mypack.client.InputHint'/>
<servlet path='/IH_PRO/input_hint' class='mypack.server.IH_ServiceImpl'/>
</module>