Javascript Jquery自动完成和Spring2.5.6的问题
我试图使用JQuery和Spring2.5.6显示自动完成列表,但是我将json放在前端,但是我无法显示它Javascript Jquery自动完成和Spring2.5.6的问题,javascript,jquery,json,spring-mvc,Javascript,Jquery,Json,Spring Mvc,我试图使用JQuery和Spring2.5.6显示自动完成列表,但是我将json放在前端,但是我无法显示它 $(function() { $("#globalSearch").autocomplete({ source: function(request, response) { $.ajax({ url: "${pageContext.request.contextPath}/autoSearch.htm",
$(function() {
$("#globalSearch").autocomplete({
source: function(request, response) {
$.ajax({
url: "${pageContext.request.contextPath}/autoSearch.htm",
data: {
term : request.term
},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
// this is the alert output its displaying:{"list":["ernst","ernst "]}
alert(JSON.stringify(data, null, 4));
response($.map(data, function(item) {
//its not alerting anything here
alert(JSON.stringify(item, null, 4));
return{
value: item.list
};
}));
}
});
}
});
});
这是我的spring控制器代码:
@RequestMapping(method = RequestMethod.GET, value = "/autoSearch.htm")
public ModelAndView autoSearch(
@RequestParam(value = "term", required = false) String term
) throws ParseException, IOException {
if (logger.isDebugEnabled()) {
logger.debug("inventoryHandler called");
}
Map<String, Object> model = new HashMap<String, Object>();
int i = 0;
model.put("list", getBaseModel().getSearchServiceBean().autoCompleter(term));
return new ModelAndView("jsonView", model);
}
@RequestMapping(method=RequestMethod.GET,value=“/autoSearch.htm”)
公共模型和视图自动搜索(
@RequestParam(value=“term”,required=false)字符串术语
)抛出ParseException,IOException{
if(logger.isDebugEnabled()){
debug(“调用inventoryHandler”);
}
映射模型=新的HashMap();
int i=0;
put(“list”,getBaseModel().getSearchServiceBean().autoCompleter(术语));
返回新的ModelAndView(“jsonView”,model);
}
谁能告诉我如何显示自动完成列表
提前感谢,
致以最良好的祝愿,
Raja.在Spring3.x以后的版本中,您将使用@ResponseBody注释,例如:
public @ResponseBody List<String> autocomplete(@RequestParam(value = "term", required = false) String term) {
//Go get your results for the autocomplete term
List<String> termResults = ...;
return termResults;
}
最后我找到了下面显示的解决方案
$(document).ready(function() {
$("#globalSearch").autocomplete({
source: function(request, response) {
$.ajax({
url: "${pageContext.request.contextPath}/autoSearch.htm",
data: {
globalSearch : request.term
},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
response($.map(data.list, function(value) {
return{
label: value,
value: value
};
}));
}
});
}
});
});
在输入字段中,名称和id必须相同
<input type="text" name="globalSearch" id="globalSearch"/>
@RequestMapping(method=RequestMethod.GET,value=“/autoSearch.htm”)
公共模型和视图自动搜索(
@RequestParam(value=“term”,required=false)字符串术语
)抛出ParseException,IOException{
if (logger.isDebugEnabled()) {
logger.debug("inventoryHandler called");
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("list", getBaseModel().getSearchServiceBean().searchAutoComplete(term));
return new ModelAndView("jsonView", model);
}
实际上,我在前端得到了json。只是不知道如何返回它。但最终找到了解决方案,它显示在下面。
if (logger.isDebugEnabled()) {
logger.debug("inventoryHandler called");
}
Map<String, Object> model = new HashMap<String, Object>();
model.put("list", getBaseModel().getSearchServiceBean().searchAutoComplete(term));
return new ModelAndView("jsonView", model);
}
$("#searchEmail").autocomplete({
source: function(request, response) {
ajaxCall(request, response);
},
select: function(event, ui) {
$("#searchEmail").val(ui.item.value);
$("#emailSearch-form").submit();
}
});
function ajaxCall(request, response) {
$.ajax({
url: "${pageContext.request.contextPath}/autoSearch.htm",
data: {
term : request.term
},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
response($.map(data.list, function(item) {
return{
label: item,
value: item
};
}));
}
});
}