日期:2014-05-16  浏览次数:20724 次

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

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

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. ??
  3. import?java.util.List; ??
  4. import?java.util.Map; ??
  5. ??
  6. import?javax.servlet.http.HttpServletRequest; ??
  7. import?javax.servlet.http.HttpServletResponse; ??
  8. ??
  9. import?org.apache.struts.action.ActionForm; ??
  10. import?org.apache.struts.action.ActionForward; ??
  11. import?org.apache.struts.action.ActionMapping; ??
  12. import?org.perfect.struts.dao.SearchSuggestDao; ??
  13. import?org.perfect.struts.form.LoginForm; ??
  14. import?org.perfect.struts.form.SearchSuggestForm; ??
  15. ??
  16. public?class?SearchSuggestAction?extends?BasicDispatchAction?{ ??
  17. ??
  18. ????private?SearchSuggestDao?searchSuggestDao; ??
  19. ??
  20. ????public?void?setSearchSuggestDao(SearchSuggestDao?searchSuggestDao)?{ ??
  21. ????????this.searchSuggestDao?=?searchSuggestDao; ??
  22. ????} ??
  23. ??
  24. ????public?ActionForward?execute(ActionMapping?mapping,?ActionForm?form, ??
  25. ????????????HttpServletRequest?request,?HttpServletResponse?response) ??
  26. ????????????throws?Exception?{ ??
  27. ??
  28. ????????SearchSuggestForm?searchSuggestForm?=?(SearchSuggestForm)?form; ??
  29. ????????String?action?=?request.getParameter(