我尝试使用自动完成jquery-ui脚本,但从文档中它解释了远程源必须返回json数据,它不是在谈论纯文本< /strong>回应,
我在 jsp/servlet 中开发我的应用程序,但我不知道如何创建 json 数据。
所以我发现了另一个 jquery 自动完成插件 --> java 自动完成功能
本教程和脚本效果很好,但脚本没有我需要的相同选项。
我尝试保留相同的 getdata.jsp 和 servlet 页面以适应 jquery-ui-autocomplete,只需更改脚本的链接,并且 firebug 告诉我对请求,但未显示!
Firebug 的屏幕截图
JavaScript 调用:
<script>
$(function() {
$( "#responsable" ).autocomplete({
source: "getdata.jsp",
minLength: 2
});
});
</script>
这里是 getdata.jsp 文件:
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="fr.myldap.model.*"%>
<%
PersonneDB db = new PersonneDB();
String query = request.getParameter("term");
List<Personne> personnes = db.getData(query);
Iterator<Personne> iterator = personnes.iterator();
while(iterator.hasNext()) {
String personne = (String)iterator.next().getNomComplet();
out.println(personne);
}
%>
这里是 PersonneDB 类返回人员名单
package fr.myldap.model;
import java.util.ArrayList;
import java.util.List;
public class PersonneDB {
private LDAPInterneDao piDao;
private LDAPExterneDao peDao;
public PersonneDB() {
ContextVar var= new ContextVar();
piDao = var.getPiDao();
peDao = var.getPeDao();
}
public List<Personne> getData(String query) {
List<Personne> matched = new ArrayList<Personne>(piDao.findByName(query));
matched.addAll(peDao.findByName(query));
return matched;
}
}
我希望任何人都可以帮助我
I try to use autocomplete jquery-ui script, but from the documentation it's explain that the remote source must return a json data, it's not talking about plain text response,
and I develop my application in jsp/servlet and I don't know how to create json data.
So I've discover an other jquery autocomplete plugin --> autocomplete feature with java
This tutorial and the script work great but the script has not same options that I need.
I try to keep the same getdata.jsp and servlet pages for adapt to jquery-ui-autocomplete just changing the link of script, and firebug say me the correct response to the request but that's not displayed!
ScreenShot of firebug
JavaScript call:
<script>
$(function() {
$( "#responsable" ).autocomplete({
source: "getdata.jsp",
minLength: 2
});
});
</script>
here is the getdata.jsp file:
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="fr.myldap.model.*"%>
<%
PersonneDB db = new PersonneDB();
String query = request.getParameter("term");
List<Personne> personnes = db.getData(query);
Iterator<Personne> iterator = personnes.iterator();
while(iterator.hasNext()) {
String personne = (String)iterator.next().getNomComplet();
out.println(personne);
}
%>
and here is the PersonneDB class wich return the person list
package fr.myldap.model;
import java.util.ArrayList;
import java.util.List;
public class PersonneDB {
private LDAPInterneDao piDao;
private LDAPExterneDao peDao;
public PersonneDB() {
ContextVar var= new ContextVar();
piDao = var.getPiDao();
peDao = var.getPeDao();
}
public List<Personne> getData(String query) {
List<Personne> matched = new ArrayList<Personne>(piDao.findByName(query));
matched.addAll(peDao.findByName(query));
return matched;
}
}
I hope anyone can help me
发布评论
评论(3)
首先从 此位置。
现在要返回 JSON 数据,您需要遵循其自己的格式,例如:
如上所示,json 数据可以有键:值对,对象可以存储在
{ }
中,数组可以是存储在[ ]
中,依此类推。现在要将您的响应转换为 JSON 对象,您需要在 jsp 文件中导入以下语句:(
它可能会根据您正在下载的库而变化,您可以浏览 javadoc 了解更多详细信息)
现在查看以下代码,以创建一个 json 对象并返回它:
它会自动将键:值对转换为正确的 JSON 对象...
更新:
所需的库:
您可以从 此处:
要将 arraylist 转换为 json,请使用以下示例代码:
First of all download the json library for java from this location.
Now to return the JSON data you need to follow its own format, something like :
As you can see above, json data can have key:value pair, object can be stored inside
{ }
, array can be store in[ ]
and so on.Now to convert your response into JSON object you need to import following statement in your jsp file :
(it may change depend on the lib that you are downloading, you can explore javadoc for more detail)
Now look at to the following code, to create a json object and return it :
It will automatically convert the key:value pair into correct JSON object...
UPDATE :
Required Libraries :
You can download all from here :
To convert arraylist to json, use following sample code :
了解如何创建 JSON。它正在取代 XML 作为信息交换媒介。
http://www.roseindia.net/tutorials/json/json-jsp -example.shtml
Learn how to create JSON. It's replacing XML as the information interchange medium.
http://www.roseindia.net/tutorials/json/json-jsp-example.shtml
您应该从 json.org 开始,然后决定是否要首先返回 JSON 数组或对象。
jQuery UI autocomplete 是一个非常灵活的插件,我认为最简单的解决方案是从您的JSP 以便利用该插件。
我发现 json-lib 非常容易运行。您需要下载它和依赖项(commons-collections,commons-lang, commons-logging,ezmorph,commons-beantils)并将它们添加到您的
WEB-INF\lib
目录中。然后,您可以使用像 JSONArray:
返回
["John","Paul","George","Ringo"]
jQuery UI 自动完成功能也可以与 JSONObject 如果你想返回
< ;key, value>
对代替。为了完整起见,我的
WEB-INF\lib
目录包含以下内容:编辑: 更新了示例 JSP
You should start with json.org and decide if you want to return a JSON array or object first.
The jQuery UI autocomplete is a very flexible plugin and I think the simplest solution would be to return JSON from your JSP in order to utilise the plugin.
I have found the json-lib very easy to get running. You will need to download that and the dependencies (commons-collections, commons-lang, commons-logging, ezmorph, commons-beantils) and add them to your
WEB-INF\lib
directory.You could then use something as simple as a JSONArray:
which returns
["John","Paul","George","Ringo"]
The jQuery UI autocomplete will also function with a JSONObject if you want to return a
<key, value>
pair instead.For completeness, my
WEB-INF\lib
directory contains the following:Edit: Updated example JSP