ITeye博客

J2EE 0.7.2—用ajax实现像google那样的suggest-search功能(简化版)

cui09 · 2007-09-18 20:23 · J2EE系统更新档案 · 非技术 · Markdown

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

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 代码

  1. package org.perfect.struts.action;
  2. import java.util.List;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts.action.ActionForm;
  7. import org.apache.struts.action.ActionForward;
  8. import org.apache.struts.action.ActionMapping;
  9. 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 < 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 代码

  1. "http://www.w3.org/1999/xhtml">
  2. "text/css" media="screen">
  3. body {
  4. font: 11px arial;
  5. }
  6. .suggest_link {
  7. background-color: #FFFFFF;
  8. padding: 2px 6px 2px 6px;
  9. 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 代码

  1. <body onclick='hideSearchSuggest()'>
  2. <html:form action="/searchSuggestAction" method="POST">
  3. <bean:define id='form' name='searchSuggestForm' type='org.perfect.struts.form.SearchSuggestForm'>
  4. bean:define>
  5. <fieldset><table><tr>
  6. <td class="free_input"><input type='text' name="title" size='80' onkeyup="testPrototype();" autocomplete="off"/>td>
  7. <td><span style='font-size:12'>搜索span>td>
  8. tr>table>
  9. fieldset>
  10. <div id="search_suggest">div>
  11. html:form>
  12. body>
  13. html>

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

全靠谷老哥了。

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。