我正在尝试使用JQuery和Spring 2.5.6显示自动完成列表,但我正在将json带到前端,但我无法显示它。
$(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
};
}));
}
});
}
});
});
这是我的弹簧控制器代码:
@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);
}
任何人都可以告诉我如何显示自动填充列表。
先谢谢,
最诚挚的问候, 拉加。
答案 0 :(得分:0)
在Spring 3.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;
}
在我看来,您的响应被发送回具有错误内容类型的客户端,并且被解释为text / html而不是JSON。您可以设置响应内容类型并从控制器中编写JSON字符串吗?
String json = "{\"list\":[\"ernst\",\"ernst \"]}";
response.setContentType("application/json");
PrintWriter writer = response.getWriter();
response.setContentLength(json.length());
writer.write(json);
答案 1 :(得分:0)
最后我找到了下面显示的解决方案。
$(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
};
}));
}
});
}
});
});
并且在输入字段中,name和id必须与
相同 <input type="text" name="globalSearch" id="globalSearch"/>
答案 2 :(得分:0)
@RequestMapping(method = RequestMethod.GET,value =“/ autoSearch.htm”) public ModelAndView autoSearch( @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);
}
并且在views.properties文件视图中应该设置为
jsonView。(类)= org.springframework.web.servlet.view.json.JsonView
并且在javascript中,自动完成功能将使用以下代码:
$("#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
};
}));
}
});
}