javascript 如何使用 JSP 和 servlet 创建类似 google 的即时搜索?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5471362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:18:45  来源:igfitidea点击:

How to create google like instant search using JSP and servlets?

javajavascriptjqueryajax

提问by Kavin

I am working on a basic instant search function that basically searches the database and displays the results instantly just like google instant. This here http://woorkup.com/2010/09/13/how-to-create-your-own-instant-search/looks promising but I want to know if there is a way to implement this using JSP, java/servlets.

我正在开发一个基本的即时搜索功能,它基本上搜索数据库并像谷歌即时一样立即显示结果。这在这里http://woorkup.com/2010/09/13/how-to-create-your-own-instant-search/看起来很有希望,但我想知道是否有办法使用 JSP、java/小服务程序。

回答by Bombe

Java and servlets alone will not be sufficient, you will need JavaScript on the client side. Basically you attach a listener to the input field and send an AJAX request to a JSP that does the search and returns the results which you then only have to format and display in a drop-down box below the input field.

仅靠 Java 和 servlet 是不够的,您需要在客户端使用 JavaScript。基本上,您将一个侦听器附加到输入字段并将 AJAX 请求发送到执行搜索并返回结果的 JSP,然后您只需将其格式化并显示在输入字段下方的下拉框中。

回答by Thorsten Niehues

This is also a very good tutorial about instant search: http://www.w3schools.com/php/php_ajax_livesearch.asp

这也是一个非常好的即时搜索教程:http: //www.w3schools.com/php/php_ajax_livesearch.asp

It uses Java Script and PHP. By reading / doing this tutorial you should get a idea how instant search works. So I hope this helps even if you want to use JSP.

它使用 Java Script 和 PHP。通过阅读/完成本教程,您应该了解即时搜索的工作原理。因此,即使您想使用 JSP,我也希望这会有所帮助。

回答by dolphy

You can do this using jQuery. The jQuery UI autocomplete is nice and easy to implement:

您可以使用 jQuery 执行此操作。jQuery UI 自动完成功能非常好且易于实现:

http://jqueryui.com/demos/autocomplete/

http://jqueryui.com/demos/autocomplete/

回答by Fergie

As previous posters have pointed out, you will have to use JavaScript to do this. The least painful way to use JavaScript here is to use JQuery UI

正如之前的海报所指出的,你必须使用 JavaScript 来做到这一点。在这里使用 JavaScript 最不痛苦的方式是使用 JQuery UI

There is a fairly straightforward walkthrough here: http://blog.comperiosearch.com/2012/06/make-an-instant-search-application-using-json-ajax-and-jquery/

这里有一个相当简单的演练:http: //blog.comperiosearch.com/2012/06/make-an-instant-search-application-using-json-ajax-and-jquery/

回答by Chris

This is an oldie-but-goodie:

这是一个老掉牙的好东西:

http://lab.abhinayrathore.com/autocomplete/

http://lab.abhinayrathore.com/autocomplete/

Combines Google,Bing,Yahoo,Wiki,Amazon, etc. all in 1 instant autocomplete. Allows you to easily add/remove websites.

将 Google、Bing、Yahoo、Wiki、Amazon 等都结合在 1 次即时自动完成中。允许您轻松添加/删除网站。