---
title: "J2EE 0.7.2—用ajax实现像google那样的suggest-search功能(简化版) "
author: "cui09"
date: "2007-09-18T20:23:21Z"
tags: []
categories: ["非技术"]
slug: "j2ee-0-7-2-ajax-google-suggest-search"
draft: false
lastmod: "2007-09-18T20:23:21Z"
id: 125104
url: "https://cui.iteye.com/blog/125104"
---

首先是需要建一个库。考虑可以这样。建立一个拼音的字段对应汉字的，用户打简拼即可查出汉字

sql 代码

1. create table \`j2ee\`.\`suggest\`(
2. \`SUGGEST\_ID\` int not null auto\_increment,
3. \`TITLE\` varchar(255),
4. primary key (\`SUGGEST\_ID\`)
5. );

当然这里为了方便没有这样做。直接查title这个字段了。

其次是action了

java 代码

01. package org.perfect.struts.action;
02. import java.util.List;
03. import java.util.Map;
04. import javax.servlet.http.HttpServletRequest;
05. import javax.servlet.http.HttpServletResponse;
06. import org.apache.struts.action.ActionForm;
07. import org.apache.struts.action.ActionForward;
08. import org.apache.struts.action.ActionMapping;
09. import org.perfect.struts.dao.SearchSuggestDao;
10. import org.perfect.struts.form.LoginForm;
11. import org.perfect.struts.form.SearchSuggestForm;
12. public class SearchSuggestAction extends BasicDispatchAction {
13. private SearchSuggestDao searchSuggestDao;
14. public void setSearchSuggestDao(SearchSuggestDao searchSuggestDao) {
15. this.searchSuggestDao = searchSuggestDao;
16. }
17. public ActionForward execute(ActionMapping mapping, ActionForm form,
18. HttpServletRequest request, HttpServletResponse response)
19. throws Exception {
20. SearchSuggestForm searchSuggestForm = (SearchSuggestForm) form;
21. String action = request.getParameter("action");
22. action = (action == null || "".equals(action)) ? "init" : action;
23. return this.dispatchMethod(mapping, form, request, response, action);
24. }
25. public ActionForward init(ActionMapping mapping, ActionForm form,
26. HttpServletRequest request, HttpServletResponse response) {
27. SearchSuggestForm searchSuggestForm = (SearchSuggestForm) form;
28. return mapping.getInputForward();
29. }
30. //重点在这里
31. public ActionForward login(ActionMapping mapping, ActionForm form,
32. HttpServletRequest request, HttpServletResponse response)
33. throws Exception {
34. SearchSuggestForm searchSuggestForm = (SearchSuggestForm) form;
35. //由于我的Ajax水平比较初级，所以中文的问题搞了两个多小时，实现英文版的是很快的。
36. //这里必须得这样写。没有为什么。老外才不会管中国人的问题呢。
37. response.setCharacterEncoding("GBK");
38. response.setContentType("text/html");
39. response.setHeader("Cache-Control", "no-cache");
40. // new String(s.getBytes("iso8859-1"),"gbk");
41. String user\_no = new String(request.getParameter("title").getBytes(
42. "iso8859-1"), "gbk");
43. System.out.println("user\_no=" + user\_no);
44. try {
45. if (!"".equals(user\_no) && user\_no != null) {
46. // String msg = "";
47. // select title from suggest where title like '%bc%'
48. //取到后拼个sql.用searchSuggestDao方法取出值放入LIST。
49. //searchSuggestDao直接继承自BaseDaoImp.里面没有代码，就不写了。
50. String sql = "select title from suggest where title like '%"
51. + user\_no + "%'";
52. System.out.println("sql=" + sql);
53. List list = searchSuggestDao.findForListBySql(sql);
54. StringBuffer sb = new StringBuffer();
55. for (int i = 0; i &lt; list.size(); i++) {
56. Map map = (Map) list.get(i);
57. String key = map.get("title").toString();
58. //用"\\n"分隔
59. sb.append(key + "\\n");
60. }
61. System.out.println(sb.toString());
62. //传给页面，搞定了。
63. response.getWriter().write(sb.toString());
64. }
65. } catch (Exception e) {
66. e.printStackTrace();
67. }
68. return null;
69. }
70. }

 

最后是jsp了。有一点点儿麻烦

由于我的css同样很烂。所以这里就没考虑太多。为达到效果不则手段了。以后会改的好看一点。

合理一点。

css 代码

01. "http://www.w3.org/1999/xhtml"&gt;
02. "text/css" media="screen"&gt;
03. body {
04. font: 11px arial;
05. }
06. .suggest\_link {
07. background-color: #FFFFFF;
08. padding: 2px 6px 2px 6px;
09. width: 573px;
10. }
11. .suggest\_link\_over {
12. background-color: #E8F2FE;
13. padding: 2px 6px 2px 6px;
14. width: 573px;
15. }
16. #search\_suggest {
17. position: absolute;
18. background-color: #FFFFFF;
19. text-align: left;
20. border: 0px solid #8AABD5;
21. width: 575px;
22. cursor: hand;
23. }

不知道是我写的js烂，还是javaeye的功能有点小毛病。js发了四五回还是乱七八糟的。

真接发附件了。

xml 代码

01. &lt;body onclick='hideSearchSuggest()'&gt;
02. &lt;html:form action="/searchSuggestAction" method="POST"&gt;
03. &lt;bean:define id='form' name='searchSuggestForm' type='org.perfect.struts.form.SearchSuggestForm'&gt;
04. bean:define&gt;
05. &lt;fieldset&gt;&lt;table&gt;&lt;tr&gt;
06. &lt;td class="free\_input"&gt;&lt;input type='text' name="title" size='80' onkeyup="testPrototype();" autocomplete="off"/&gt;td&gt;
07. &lt;td&gt;&lt;span style='font-size:12'&gt;搜索span&gt;td&gt;
08. tr&gt;table&gt;
09. fieldset&gt;
10. &lt;div id="search\_suggest"&gt;div&gt;
11. html:form&gt;
12. body&gt;
13. html&gt;

没啥东西。基本上很简单。不过也搞了三四个小时才搞得定。

全靠谷老哥了。

不晓得没了他我能不能自己独立写点东西。